Convert createFolder to async await.

This commit is contained in:
Brent Simmons 2024-04-22 23:02:10 -07:00
parent d912bcbe79
commit cdfdf294be
2 changed files with 31 additions and 67 deletions

View File

@ -282,43 +282,22 @@ final class FeedlyAccountDelegate: AccountDelegate {
func createFolder(for account: Account, name: String) async throws -> Folder { func createFolder(for account: Account, name: String) async throws -> Folder {
try await withCheckedThrowingContinuation { continuation in refreshProgress.addTask()
defer {
refreshProgress.completeTask()
}
self.createFolder(for: account, name: name) { result in let collection = try await caller.createCollection(named: name)
switch result {
case .success(let folder): if let folder = account.ensureFolder(with: collection.label) {
continuation.resume(returning: folder) folder.externalID = collection.id
case .failure(let error): return folder
continuation.resume(throwing: error) } else {
} // Is the name empty? Or one of the global resource names?
} throw FeedlyAccountDelegateError.unableToAddFolder(name)
} }
} }
private func createFolder(for account: Account, name: String, completion: @escaping (Result<Folder, Error>) -> Void) {
let progress = refreshProgress
progress.addToNumberOfTasksAndRemaining(1)
caller.createCollection(named: name) { result in
progress.completeTask()
switch result {
case .success(let collection):
if let folder = account.ensureFolder(with: collection.label) {
folder.externalID = collection.id
completion(.success(folder))
} else {
// Is the name empty? Or one of the global resource names?
completion(.failure(FeedlyAccountDelegateError.unableToAddFolder(name)))
}
case .failure(let error):
completion(.failure(error))
}
}
}
func renameFolder(for account: Account, with folder: Folder, to name: String) async throws { func renameFolder(for account: Account, with folder: Folder, to name: String) async throws {
try await withCheckedThrowingContinuation { continuation in try await withCheckedThrowingContinuation { continuation in

View File

@ -180,56 +180,41 @@ protocol FeedlyAPICallerDelegate: AnyObject {
} }
} }
func createCollection(named label: String, completion: @escaping (Result<FeedlyCollection, Error>) -> ()) { func createCollection(named label: String) async throws -> FeedlyCollection {
guard !isSuspended else { guard !isSuspended else {
return DispatchQueue.main.async { throw TransportError.suspended
completion(.failure(TransportError.suspended))
}
} }
guard let accessToken = credentials?.secret else { guard let accessToken = credentials?.secret else {
return DispatchQueue.main.async { throw CredentialsError.incompleteCredentials
completion(.failure(CredentialsError.incompleteCredentials))
}
} }
var components = baseUrlComponents var components = baseUrlComponents
components.path = "/v3/collections" components.path = "/v3/collections"
guard let url = components.url else { guard let url = components.url else {
fatalError("\(components) does not produce a valid URL.") fatalError("\(components) does not produce a valid URL.")
} }
var request = URLRequest(url: url) var request = URLRequest(url: url)
request.httpMethod = "POST" request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType) request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
request.addValue("application/json", forHTTPHeaderField: "Accept-Type") request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization) request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
do { struct CreateCollectionBody: Encodable {
struct CreateCollectionBody: Encodable { var label: String
var label: String
}
let encoder = JSONEncoder()
let data = try encoder.encode(CreateCollectionBody(label: label))
request.httpBody = data
} catch {
return DispatchQueue.main.async {
completion(.failure(error))
}
} }
let encoder = JSONEncoder()
send(request: request, resultType: [FeedlyCollection].self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in let data = try encoder.encode(CreateCollectionBody(label: label))
switch result { request.httpBody = data
case .success(let (httpResponse, collections)):
if httpResponse.statusCode == 200, let collection = collections?.first { let (httpResponse, collections) = try await send(request: request, resultType: [FeedlyCollection].self)
completion(.success(collection))
} else { guard let collection = collections?.first, httpResponse.statusCode == HTTPResponseCode.OK else {
completion(.failure(URLError(.cannotDecodeContentData))) throw URLError(.cannotDecodeContentData)
}
case .failure(let error):
completion(.failure(error))
}
} }
return collection
} }
func renameCollection(with id: String, to name: String, completion: @escaping (Result<FeedlyCollection, Error>) -> ()) { func renameCollection(with id: String, to name: String, completion: @escaping (Result<FeedlyCollection, Error>) -> ()) {