From 34a9831b608575ed08a9a6a70d9c1d3824c851bd Mon Sep 17 00:00:00 2001 From: Philippe Weidmann Date: Thu, 30 Jan 2025 14:20:29 +0100 Subject: [PATCH] chore: Use updated core apifetcher Signed-off-by: Philippe Weidmann --- .../Data/Api/DriveApiFetcher+Upload.swift | 8 ++-- kDriveCore/Data/Api/DriveApiFetcher.swift | 38 +++++++++++-------- .../Data/Api/PublicShareApiFetcher.swift | 5 ++- .../Operation/UploadOperation.swift | 5 ++- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/kDriveCore/Data/Api/DriveApiFetcher+Upload.swift b/kDriveCore/Data/Api/DriveApiFetcher+Upload.swift index d979af8d7..98fcb2ab6 100644 --- a/kDriveCore/Data/Api/DriveApiFetcher+Upload.swift +++ b/kDriveCore/Data/Api/DriveApiFetcher+Upload.swift @@ -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 @@ -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 diff --git a/kDriveCore/Data/Api/DriveApiFetcher.swift b/kDriveCore/Data/Api/DriveApiFetcher.swift index bcfc4407e..7b2d6f508 100644 --- a/kDriveCore/Data/Api/DriveApiFetcher.swift +++ b/kDriveCore/Data/Api/DriveApiFetcher.swift @@ -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? @@ -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(request: DataRequest, - decoder: JSONDecoder = ApiFetcher.decoder) async throws -> ValidServerResponse { + overrideDecoder: JSONDecoder? = nil) async throws -> ValidServerResponse { do { return try await super.perform(request: request) } catch InfomaniakError.apiError(let apiError) { @@ -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 { @@ -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)) } @@ -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)) } @@ -511,7 +517,7 @@ public class DriveApiFetcher: ApiFetcher { password: String? = nil) async throws -> ValidServerResponse { 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 diff --git a/kDriveCore/Data/Api/PublicShareApiFetcher.swift b/kDriveCore/Data/Api/PublicShareApiFetcher.swift index b97c52e47..2323ef776 100644 --- a/kDriveCore/Data/Api/PublicShareApiFetcher.swift +++ b/kDriveCore/Data/Api/PublicShareApiFetcher.swift @@ -30,7 +30,7 @@ public enum PublicShareLimitation: String { } public class PublicShareApiFetcher: ApiFetcher { - override public init() { + public init() { super.init() } @@ -38,7 +38,8 @@ public class PublicShareApiFetcher: ApiFetcher { private static var handledHttpStatus = Set(200 ... 500) override public func perform(request: DataRequest, - decoder: JSONDecoder = ApiFetcher.decoder) async throws -> ValidServerResponse { + overrideDecoder: JSONDecoder? = nil) async throws -> ValidServerResponse { + let decoder = overrideDecoder ?? self.decoder let validatedRequest = request.validate(statusCode: PublicShareApiFetcher.handledHttpStatus) let dataResponse = await validatedRequest.serializingDecodable(ApiResponse.self, automaticallyCancelling: true, diff --git a/kDriveCore/Data/UploadQueue/Operation/UploadOperation.swift b/kDriveCore/Data/UploadQueue/Operation/UploadOperation.swift index 69f00cec7..3b7d3c60a 100644 --- a/kDriveCore/Data/UploadQueue/Operation/UploadOperation.swift +++ b/kDriveCore/Data/UploadQueue/Operation/UploadOperation.swift @@ -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.self, from: data).data else { + + guard let uploadedChunk = try? DriveApiFetcher.decoder.decode(ApiResponse.self, from: data).data else { Log.uploadOperation("parsing error:\(String(describing: error)) ufid:\(uploadFileId)", level: .error) throw ErrorDomain.parseError } @@ -460,7 +461,7 @@ public final class UploadOperation: AsynchronousOperation, UploadOperationable { var driveError = DriveError.serverError if let data, - let apiError = try? ApiFetcher.decoder.decode(ApiResponse.self, from: data).error { + let apiError = try? DriveApiFetcher.decoder.decode(ApiResponse.self, from: data).error { driveError = DriveError(apiError: apiError) }