mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2025-01-27 09:21:21 +01:00
Convert createFolder to async await.
This commit is contained in:
parent
d912bcbe79
commit
cdfdf294be
@ -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
|
||||
|
@ -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>) -> ()) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user