Skip to content

Instantly share code, notes, and snippets.

@Dimillian
Created May 4, 2020 05:50
Show Gist options
  • Save Dimillian/88fe977f052863529278917d449fa026 to your computer and use it in GitHub Desktop.
Save Dimillian/88fe977f052863529278917d449fa026 to your computer and use it in GitHub Desktop.
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