Skip to content

Instantly share code, notes, and snippets.

@Plnda
Last active January 22, 2020 20:19
Show Gist options
  • Save Plnda/eb689a6caa001d8af1da050c61d156c0 to your computer and use it in GitHub Desktop.
Save Plnda/eb689a6caa001d8af1da050c61d156c0 to your computer and use it in GitHub Desktop.
import Foundation
import Combine
protocol Network {
var decoder: JSONDecoder { get set }
var enviroment: NetworkEnviroment { get set }
}
extension Network {
func fetch<T: Decodable>(route: NetworkRoute) -> AnyPublisher<T, Error> {
let request = route.create(for: enviroment)
return URLSession.shared
.dataTaskPublisher(for: request)
.tryCompactMap { result in
try self.decoder.decode(T.self, from: result.data)
}
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment