Last active
September 22, 2019 16:14
-
-
Save SwiftyAlex/3e016bb6d971c03757ae9939d92c76b9 to your computer and use it in GitHub Desktop.
A Router
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
public class Router { | |
static var baseUrl = APIConfig.baseURL | |
@available(iOS 13.0, *) | |
public static func performRequest<R: Route>(_ route: R) -> AnyPublisher<R.ResponseType, Error> { | |
return URLSession.shared.dataTaskPublisher(for: route.toRequest()) | |
.mapError { NetworkError(from: $0) } | |
.map(\.data) | |
.decode(type: R.ResponseType.self, decoder: JSONDecoder()) | |
.mapError { _ in NetworkError.decodingFailed } | |
.eraseToAnyPublisher() | |
} | |
@available(iOS 13.0, *) | |
public static func performRequest<R: Route>(_ route: R) -> AnyPublisher<URLResponse, Error> where R.ResponseType == Empty { | |
return URLSession.shared.dataTaskPublisher(for: route.toRequest()) | |
.map(\.response) | |
.mapError { NetworkError(from: $0) } | |
.eraseToAnyPublisher() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment