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 {
try await withCheckedThrowingContinuation { continuation in
refreshProgress.addTask()
defer {
refreshProgress.completeTask()
}
self.createFolder(for: account, name: name) { result in
switch result {
case .success(let folder):
continuation.resume(returning: folder)
case .failure(let error):
continuation.resume(throwing: error)
}
}
let collection = try await caller.createCollection(named: name)
if let folder = account.ensureFolder(with: collection.label) {
folder.externalID = collection.id
return folder
} 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 {
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 {
return DispatchQueue.main.async {
completion(.failure(TransportError.suspended))
}
throw TransportError.suspended
}
guard let accessToken = credentials?.secret else {
return DispatchQueue.main.async {
completion(.failure(CredentialsError.incompleteCredentials))
}
throw CredentialsError.incompleteCredentials
}
var components = baseUrlComponents
components.path = "/v3/collections"
guard let url = components.url else {
fatalError("\(components) does not produce a valid URL.")
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
do {
struct CreateCollectionBody: Encodable {
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))
}
struct CreateCollectionBody: Encodable {
var label: String
}
send(request: request, resultType: [FeedlyCollection].self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
switch result {
case .success(let (httpResponse, collections)):
if httpResponse.statusCode == 200, let collection = collections?.first {
completion(.success(collection))
} else {
completion(.failure(URLError(.cannotDecodeContentData)))
}
case .failure(let error):
completion(.failure(error))
}
let encoder = JSONEncoder()
let data = try encoder.encode(CreateCollectionBody(label: label))
request.httpBody = data
let (httpResponse, collections) = try await send(request: request, resultType: [FeedlyCollection].self)
guard let collection = collections?.first, httpResponse.statusCode == HTTPResponseCode.OK else {
throw URLError(.cannotDecodeContentData)
}
return collection
}
func renameCollection(with id: String, to name: String, completion: @escaping (Result<FeedlyCollection, Error>) -> ()) {