Last active
January 22, 2020 20:19
-
-
Save Plnda/eb689a6caa001d8af1da050c61d156c0 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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