mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
convert saveURL to async
This commit is contained in:
parent
822a38a863
commit
def0fc7678
3 changed files with 25 additions and 50 deletions
|
|
@ -219,14 +219,3 @@ public extension DataService {
|
|||
.eraseToAnyPublisher()
|
||||
}
|
||||
}
|
||||
|
||||
private extension SaveArticleError {
|
||||
static func make(from httpError: HttpError) -> SaveArticleError {
|
||||
switch httpError {
|
||||
case .network, .timeout:
|
||||
return .network
|
||||
case .badpayload, .badURL, .badstatus, .cancelled:
|
||||
return .unknown(description: httpError.localizedDescription)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ public extension DataService {
|
|||
}
|
||||
}
|
||||
|
||||
private extension SaveArticleError {
|
||||
extension SaveArticleError {
|
||||
static func make(from httpError: HttpError) -> SaveArticleError {
|
||||
switch httpError {
|
||||
case .network, .timeout:
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
import Combine
|
||||
import Foundation
|
||||
import Models
|
||||
import SwiftGraphQL
|
||||
|
||||
public extension DataService {
|
||||
// swiftlint:disable:next line_length
|
||||
func saveUrlPublisher(pageScrapePayload: PageScrapePayload, requestId: String) -> AnyPublisher<Void, SaveArticleError> {
|
||||
func saveURL(pageScrapePayload: PageScrapePayload, requestId: String) async throws {
|
||||
enum MutationResult {
|
||||
case saved(requestId: String, url: String)
|
||||
case error(errorCode: Enums.SaveErrorCode)
|
||||
|
|
@ -31,44 +29,32 @@ public extension DataService {
|
|||
let path = appEnvironment.graphqlPath
|
||||
let headers = networker.defaultHeaders
|
||||
|
||||
return Deferred {
|
||||
Future { promise in
|
||||
send(mutation, to: path, headers: headers) { result in
|
||||
switch result {
|
||||
case let .success(payload):
|
||||
if let graphqlError = payload.errors {
|
||||
promise(.failure(.unknown(description: graphqlError.first.debugDescription)))
|
||||
}
|
||||
|
||||
switch payload.data {
|
||||
case .saved:
|
||||
promise(.success(()))
|
||||
case let .error(errorCode: errorCode):
|
||||
switch errorCode {
|
||||
case .unauthorized:
|
||||
promise(.failure(.unauthorized))
|
||||
default:
|
||||
promise(.failure(.unknown(description: errorCode.rawValue)))
|
||||
}
|
||||
}
|
||||
case let .failure(error):
|
||||
promise(.failure(SaveError.make(from: error)))
|
||||
return try await withCheckedThrowingContinuation { continuation in
|
||||
send(mutation, to: path, headers: headers) { result in
|
||||
switch result {
|
||||
case let .success(payload):
|
||||
if let graphqlError = payload.errors {
|
||||
continuation.resume(
|
||||
throwing: SaveArticleError.unknown(description: graphqlError.first.debugDescription)
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
switch payload.data {
|
||||
case .saved:
|
||||
continuation.resume()
|
||||
case let .error(errorCode: errorCode):
|
||||
switch errorCode {
|
||||
case .unauthorized:
|
||||
continuation.resume(throwing: SaveArticleError.unauthorized)
|
||||
default:
|
||||
continuation.resume(throwing: SaveArticleError.unknown(description: errorCode.rawValue))
|
||||
}
|
||||
}
|
||||
case let .failure(error):
|
||||
continuation.resume(throwing: SaveArticleError.make(from: error))
|
||||
}
|
||||
}
|
||||
}
|
||||
.receive(on: DispatchQueue.main)
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
}
|
||||
|
||||
private extension SaveError {
|
||||
static func make(from httpError: HttpError) -> SaveArticleError {
|
||||
switch httpError {
|
||||
case .network, .timeout:
|
||||
return .network
|
||||
case .badpayload, .badURL, .badstatus, .cancelled:
|
||||
return .unknown(description: httpError.localizedDescription)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue