diff --git a/Modules/Account/Sources/Account/Feedbin/FeedbinAPICaller.swift b/Modules/Account/Sources/Account/Feedbin/FeedbinAPICaller.swift index 170741a4f..fdbe27282 100644 --- a/Modules/Account/Sources/Account/Feedbin/FeedbinAPICaller.swift +++ b/Modules/Account/Sources/Account/Feedbin/FeedbinAPICaller.swift @@ -99,27 +99,17 @@ final class FeedbinAPICaller { } } - func retrieveOPMLImportResult(importID: Int, completion: @escaping (Result) -> Void) { - + func retrieveOPMLImportResult(importID: Int) async throws -> FeedbinImportResult? { let callURL = feedbinBaseURL.appendingPathComponent("imports/\(importID).json") let request = URLRequest(url: callURL, credentials: credentials) - transport.send(request: request, resultType: FeedbinImportResult.self) { result in - - if self.suspended { - completion(.failure(TransportError.suspended)) - return - } - - switch result { - case .success(let (_, importResult)): - completion(.success(importResult)) - case .failure(let error): - completion(.failure(error)) - } + let (_, importResult) = try await transport.send(request: request, resultType: FeedbinImportResult.self) + guard !suspended else { + throw TransportError.suspended } + return importResult } func retrieveTags(completion: @escaping (Result<[FeedbinTag]?, Error>) -> Void) { diff --git a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index a306682d0..be9487aa7 100644 --- a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -197,29 +197,28 @@ final class FeedbinAccountDelegate: AccountDelegate { refreshProgress.addToNumberOfTasksAndRemaining(1) caller.importOPML(opmlData: opmlData) { result in - switch result { - case .success(let importResult): - if importResult.complete { - Self.logger.info("Feedbin: Finished importing OPML") + Task { @MainActor in + switch result { + case .success(let importResult): + if importResult.complete { + Self.logger.info("Feedbin: Finished importing OPML") + self.refreshProgress.completeTask() + self.isOPMLImportInProgress = false + DispatchQueue.main.async { + completion(.success(())) + } + } else { + self.checkImportResult(opmlImportResultID: importResult.importResultID, completion: completion) + } + case .failure(let error): + Self.logger.info("Feedbin: OPML import failed: \(error.localizedDescription)") self.refreshProgress.completeTask() self.isOPMLImportInProgress = false - DispatchQueue.main.async { - completion(.success(())) - } - } else { - self.checkImportResult(opmlImportResultID: importResult.importResultID, completion: completion) - } - case .failure(let error): - Self.logger.info("Feedbin: OPML import failed: \(error.localizedDescription)") - self.refreshProgress.completeTask() - self.isOPMLImportInProgress = false - DispatchQueue.main.async { let wrappedError = AccountError.wrappedError(error: error, account: account) completion(.failure(wrappedError)) } } } - } @MainActor func createFolder(for account: Account, name: String) async throws -> Folder { @@ -605,41 +604,31 @@ final class FeedbinAccountDelegate: AccountDelegate { private extension FeedbinAccountDelegate { - func checkImportResult(opmlImportResultID: Int, completion: @escaping (Result) -> Void) { - - DispatchQueue.main.async { - - Timer.scheduledTimer(withTimeInterval: 15, repeats: true) { timer in + @MainActor func checkImportResult(opmlImportResultID: Int, completion: @escaping (Result) -> Void) { + Task { @MainActor in + while true { + try? await Task.sleep(for: .seconds(15)) Self.logger.info("Feedbin: Checking status of OPML import") + do { + let importResult = try await self.caller.retrieveOPMLImportResult(importID: opmlImportResultID) - self.caller.retrieveOPMLImportResult(importID: opmlImportResultID) { result in - switch result { - case .success(let importResult): - if let result = importResult, result.complete { - Self.logger.info("Feedbin: Checking status of OPML import finished successfully") - timer.invalidate() - self.refreshProgress.completeTask() - self.isOPMLImportInProgress = false - DispatchQueue.main.async { - completion(.success(())) - } - } - case .failure(let error): - Self.logger.info("Feedbin: Import OPML check failed: \(error.localizedDescription)") - timer.invalidate() + if let result = importResult, result.complete { + Self.logger.info("Feedbin: Checking status of OPML import finished successfully") self.refreshProgress.completeTask() self.isOPMLImportInProgress = false - DispatchQueue.main.async { - completion(.failure(error)) - } + completion(.success(())) + break } + } catch { + Self.logger.info("Feedbin: Import OPML check failed: \(error.localizedDescription)") + self.refreshProgress.completeTask() + self.isOPMLImportInProgress = false + completion(.failure(error)) + break } - } - } - } func refreshAccount(_ account: Account) async throws { diff --git a/Modules/Account/Tests/AccountTests/TestTransport.swift b/Modules/Account/Tests/AccountTests/TestTransport.swift index 8f7bdf010..ee8c334ff 100644 --- a/Modules/Account/Tests/AccountTests/TestTransport.swift +++ b/Modules/Account/Tests/AccountTests/TestTransport.swift @@ -15,7 +15,6 @@ protocol TestTransportMockResponseProviding: AnyObject { } final class TestTransport: Transport { - enum TestTransportError: String, Error { case invalidState = "The test wasn't set up correctly." } @@ -86,10 +85,18 @@ final class TestTransport: Transport { } } + func send(request: URLRequest, method: String) async throws { + fatalError("Unimplemented.") + } + func send(request: URLRequest, method: String, completion: @escaping (Result) -> Void) { fatalError("Unimplemented.") } + func send(request: URLRequest, method: String, payload: Data) async throws -> (HTTPURLResponse, Data?) { + fatalError("Unimplemented.") + } + func send(request: URLRequest, method: String, payload: Data, completion: @escaping (Result<(HTTPURLResponse, Data?), Error>) -> Void) { fatalError("Unimplemented.") } diff --git a/Modules/RSWeb/Sources/RSWeb/WebServices/TransportJSON.swift b/Modules/RSWeb/Sources/RSWeb/WebServices/TransportJSON.swift index ec780a118..6d019c6ca 100644 --- a/Modules/RSWeb/Sources/RSWeb/WebServices/TransportJSON.swift +++ b/Modules/RSWeb/Sources/RSWeb/WebServices/TransportJSON.swift @@ -10,6 +10,15 @@ import Foundation extension Transport { + /// Send an HTTP GET and return JSON object(s) + public func send(request: URLRequest, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> (HTTPURLResponse, R?) { + try await withCheckedThrowingContinuation { continuation in + send(request: request, resultType: resultType, dateDecoding: dateDecoding, keyDecoding: keyDecoding) { result in + continuation.resume(with: result) + } + } + } + /** Sends an HTTP get and returns JSON object(s) */