Skip to content

Commit

Permalink
chore: Use updated core apifetcher
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Weidmann <[email protected]>
  • Loading branch information
PhilippeWeidmann committed Feb 6, 2025
1 parent 6730b17 commit 34a9831
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
8 changes: 4 additions & 4 deletions kDriveCore/Data/Api/DriveApiFetcher+Upload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ enum APIUploadParameter: String {
}

public extension DriveApiFetcher {
internal typealias APIParameters = [APIUploadParameter: Any?]
internal typealias APIParameters = [APIUploadParameter: Encodable?]
/// Starts a session to upload a file in multiple parts
///
/// https://developer.infomaniak.com/docs/api/post/2/drive/%7Bdrive_id%7D/upload/session/start
Expand Down Expand Up @@ -242,9 +242,9 @@ public extension DriveApiFetcher {
}
}

extension [APIUploadParameter: Any?] {
func toParameters() -> Parameters {
var parameters = Parameters()
extension [APIUploadParameter: Encodable?] {
func toParameters() -> EncodableParameters {
var parameters = EncodableParameters()
for rawParameter in self {
if let nonNilValue = rawParameter.value {
parameters[rawParameter.key.rawValue] = nonNilValue
Expand Down
38 changes: 22 additions & 16 deletions kDriveCore/Data/Api/DriveApiFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ import Kingfisher
import Sentry
import UIKit

public extension ApiFetcher {
convenience init(token: ApiToken, delegate: RefreshTokenDelegate) {
self.init()
createAuthenticatedSession(token,
authenticator: SyncedAuthenticator(refreshTokenDelegate: delegate),
additionalAdapters: [RequestContextIdAdaptor(), UserAgentAdapter()])
}
}

public class AuthenticatedImageRequestModifier: ImageDownloadRequestModifier {
weak var apiFetcher: ApiFetcher?

Expand All @@ -53,18 +44,33 @@ public class AuthenticatedImageRequestModifier: ImageDownloadRequestModifier {
}

public class DriveApiFetcher: ApiFetcher {
public static var decoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970
decoder.keyDecodingStrategy = .convertFromSnakeCase
return decoder
}()

@LazyInjectService var accountManager: AccountManageable
@LazyInjectService var tokenable: InfomaniakNetworkLoginable

public var authenticatedKF: AuthenticatedImageRequestModifier!

override public init() {
super.init()
public init() {
super.init(decoder: Self.decoder)
authenticatedKF = AuthenticatedImageRequestModifier(apiFetcher: self)
}

public convenience init(token: ApiToken, delegate: RefreshTokenDelegate) {
self.init()
createAuthenticatedSession(token,
authenticator: SyncedAuthenticator(refreshTokenDelegate: delegate),
additionalAdapters: [RequestContextIdAdaptor(), UserAgentAdapter()])
authenticatedKF = AuthenticatedImageRequestModifier(apiFetcher: self)
}

override public func perform<T: Decodable>(request: DataRequest,
decoder: JSONDecoder = ApiFetcher.decoder) async throws -> ValidServerResponse<T> {
overrideDecoder: JSONDecoder? = nil) async throws -> ValidServerResponse<T> {
do {
return try await super.perform(request: request)
} catch InfomaniakError.apiError(let apiError) {
Expand Down Expand Up @@ -395,7 +401,7 @@ public class DriveApiFetcher: ApiFetcher {
}

public func restore(file: ProxyFile, in directory: ProxyFile? = nil) async throws -> CancelableResponse {
let parameters: Parameters?
let parameters: EncodableParameters?
if let directory {
parameters = ["destination_directory_id": directory.id]
} else {
Expand Down Expand Up @@ -433,7 +439,7 @@ public class DriveApiFetcher: ApiFetcher {
}

public func add(drive: AbstractDrive, category: Category, to files: [ProxyFile]) async throws -> [CategoryResponse] {
let parameters: Parameters = ["file_ids": files.map(\.id)]
let parameters: EncodableParameters = ["file_ids": files.map(\.id)]
return try await perform(request: authenticatedRequest(.fileCategory(drive: drive, category: category), method: .post,
parameters: parameters))
}
Expand All @@ -443,7 +449,7 @@ public class DriveApiFetcher: ApiFetcher {
}

public func remove(drive: AbstractDrive, category: Category, from files: [ProxyFile]) async throws -> [CategoryResponse] {
let parameters: Parameters = ["file_ids": files.map(\.id)]
let parameters: EncodableParameters = ["file_ids": files.map(\.id)]
return try await perform(request: authenticatedRequest(.fileCategory(drive: drive, category: category), method: .delete,
parameters: parameters))
}
Expand Down Expand Up @@ -511,7 +517,7 @@ public class DriveApiFetcher: ApiFetcher {
password: String? = nil) async throws -> ValidServerResponse<FileExternalImport> {
let destinationDrive = ProxyDrive(id: destinationDriveId)
let importShareLinkFiles = Endpoint.importShareLinkFiles(destinationDrive: destinationDrive)
var requestParameters: Parameters = [
var requestParameters: EncodableParameters = [
PublicShareAPIParameters.sourceDriveId: sourceDriveId,
PublicShareAPIParameters.destinationFolderId: destinationFolderId,
PublicShareAPIParameters.sharelinkUuid: sharelinkUuid
Expand Down
5 changes: 3 additions & 2 deletions kDriveCore/Data/Api/PublicShareApiFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ public enum PublicShareLimitation: String {
}

public class PublicShareApiFetcher: ApiFetcher {
override public init() {
public init() {
super.init()
}

/// All status including 401 are handled by our code. A locked public share will 401, therefore we need to support it.
private static var handledHttpStatus = Set(200 ... 500)

override public func perform<T: Decodable>(request: DataRequest,
decoder: JSONDecoder = ApiFetcher.decoder) async throws -> ValidServerResponse<T> {
overrideDecoder: JSONDecoder? = nil) async throws -> ValidServerResponse<T> {
let decoder = overrideDecoder ?? self.decoder
let validatedRequest = request.validate(statusCode: PublicShareApiFetcher.handledHttpStatus)
let dataResponse = await validatedRequest.serializingDecodable(ApiResponse<T>.self,
automaticallyCancelling: true,
Expand Down
5 changes: 3 additions & 2 deletions kDriveCore/Data/UploadQueue/Operation/UploadOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ public final class UploadOperation: AsynchronousOperation, UploadOperationable {

private func uploadCompletionSuccess(data: Data, response: URLResponse?, error: Error?) async throws {
Log.uploadOperation("completion successful \(uploadFileId)")
guard let uploadedChunk = try? ApiFetcher.decoder.decode(ApiResponse<UploadedChunk>.self, from: data).data else {

guard let uploadedChunk = try? DriveApiFetcher.decoder.decode(ApiResponse<UploadedChunk>.self, from: data).data else {
Log.uploadOperation("parsing error:\(String(describing: error)) ufid:\(uploadFileId)", level: .error)
throw ErrorDomain.parseError
}
Expand Down Expand Up @@ -460,7 +461,7 @@ public final class UploadOperation: AsynchronousOperation, UploadOperationable {

var driveError = DriveError.serverError
if let data,
let apiError = try? ApiFetcher.decoder.decode(ApiResponse<Empty>.self, from: data).error {
let apiError = try? DriveApiFetcher.decoder.decode(ApiResponse<Empty>.self, from: data).error {
driveError = DriveError(apiError: apiError)
}

Expand Down

0 comments on commit 34a9831

Please sign in to comment.