Convert to async/await.

This commit is contained in:
Brent Simmons committed 2025-11-07 17:14:02 -08:00
1 parent 427f688f04
commit 3869bf4531
4 files changed
+53 -58

No files matched your search

@@ -99,27 +99,17 @@ final class FeedbinAPICaller {
}
}
func retrieveOPMLImportResult(importID: Int, completion: @escaping (Result<FeedbinImportResult?, Error>) -> 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) {
@@ -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, Error>) -> Void) {
DispatchQueue.main.async {
Timer.scheduledTimer(withTimeInterval: 15, repeats: true) { timer in
@MainActor func checkImportResult(opmlImportResultID: Int, completion: @escaping (Result<Void, Error>) -> 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 {
@@ -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, Error>) -> 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.")
}
@@ -10,6 +10,15 @@ import Foundation
extension Transport {
/// Send an HTTP GET and return JSON object(s)
public func send<R: Decodable & Sendable>(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)
*/