Skip to content

Instantly share code, notes, and snippets.

@Juanpe
Last active March 26, 2019 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Juanpe/58439c60e7d63d605ad06427a6fa0caf to your computer and use it in GitHub Desktop.
Save Juanpe/58439c60e7d63d605ad06427a6fa0caf to your computer and use it in GitHub Desktop.
class APIClient {
// Rest of code
func buildURLRequest<T: APIRequest>(for request: T) throws -> URLRequest {
do {
var urlRequest = URLRequest(url: request.baseURL.appendingPathComponent(request.path),
cachePolicy: .reloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 100)
urlRequest.httpMethod = request.method.rawValue
request.headers?.forEach {
urlRequest.addValue($0.value as! String, forHTTPHeaderField: $0.key)
}
urlRequest = try buildRequestParams(urlRequest, params: request.parameters)
return urlRequest
} catch {
throw APIError.requestBuilderFailed
}
}
fileprivate func buildRequestParams(_ urlRequest: URLRequest, params: RequestParams) throws -> URLRequest {
var urlRequest = urlRequest
urlRequest = try params.encoder.encode(urlRequest,
with: params.parameters)
return urlRequest
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment