Created
May 4, 2020 05:50
-
-
Save Dimillian/88fe977f052863529278917d449fa026 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Combine | |
public struct NookPlazaAPIService { | |
private static let decoder = JSONDecoder() | |
private static let apiQueue = DispatchQueue(label: "nookazon_api", | |
qos: .userInitiated, | |
attributes: .concurrent) | |
public static func fetch<T: Codable>(endpoint: Category) -> AnyPublisher<T ,APIError> { | |
Result(catching: { | |
guard let url = Bundle.main.url(forResource: endpoint.rawValue, withExtension: nil) else { | |
throw APIError.message(reason: "Error while loading local ressource") | |
} | |
return try Data(contentsOf: url) | |
}) | |
.publisher | |
.decode(type: T.self, decoder: Self.decoder) | |
.mapError{ APIError.parseError(reason: $0.localizedDescription) } | |
.subscribe(on: Self.apiQueue) | |
.eraseToAnyPublisher() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment