Skip to content

Instantly share code, notes, and snippets.

@SwiftyAlex
Last active September 22, 2019 16:14
Show Gist options
  • Save SwiftyAlex/3e016bb6d971c03757ae9939d92c76b9 to your computer and use it in GitHub Desktop.
Save SwiftyAlex/3e016bb6d971c03757ae9939d92c76b9 to your computer and use it in GitHub Desktop.
A Router
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