-
Notifications
You must be signed in to change notification settings - Fork 2
Mapping of BothamAPIError is done in NSHTTPClient and HTTPClient #30
Comments
I don't understand you. Right now the implementation is just in extension HTTPClient {
public var timeout: NSTimeInterval {
get {
return 10
}
}
public func hasValidScheme(request: HTTPRequest) -> Bool {
return request.url.hasPrefix("http") || request.url.hasPrefix("https")
}
public func isValidResponse(response: HTTPResponse) -> Bool {
return 200..<300 ~= response.statusCode
}
public func mapNSErrorToBothamError(error: NSError) -> BothamAPIClientError {
let connectionErrors = [NSURLErrorCancelled,
NSURLErrorTimedOut,
NSURLErrorCannotConnectToHost,
NSURLErrorNetworkConnectionLost,
NSURLErrorNotConnectedToInternet,
NSURLErrorRequestBodyStreamExhausted
]
if connectionErrors.contains(error.code) {
return .NetworkError
} else {
return .HTTPClientError(error: error)
}
}
} And public func send(httpRequest: HTTPRequest, completion: (Result<HTTPResponse, BothamAPIClientError>) -> ()) {
let request = mapHTTPRequestToNSURLRequest(httpRequest)
let session = NSURLSession.sharedSession()
session.configuration.timeoutIntervalForRequest = timeout
session.configuration.timeoutIntervalForResource = timeout
session.dataTaskWithRequest(request) { data, response, error in
if let error = error {
let bothamError = self.mapNSErrorToBothamError(error)
completion(Result.Failure(bothamError))
} else if let response = response as? NSHTTPURLResponse, let data = data {
let response = self.mapNSHTTPURlResponseToHTTPResponse(response, data: data)
completion(Result.Success(response))
}
}.resume()
} |
Sorry my bad. ///HTTPClient/NSHTTPClient
public func mapNSErrorToBothamError(error: NSError) -> BothamAPIClientError {
let connectionErrors = [NSURLErrorCancelled,
NSURLErrorTimedOut,
NSURLErrorCannotConnectToHost,
NSURLErrorNetworkConnectionLost,
NSURLErrorNotConnectedToInternet,
NSURLErrorRequestBodyStreamExhausted
]
if connectionErrors.contains(error.code) {
return .NetworkError
} else {
return .HTTPClientError(error: error)
}
}
//BothamAPIClient
private func mapHTTPResponseToBothamAPIClientError(httpResponse: HTTPResponse)
-> Result<HTTPResponse, BothamAPIClientError> {
if isValidResponse(httpResponse) {
return Result.Success(httpResponse)
} else {
let statusCode = httpResponse.statusCode
let body = httpResponse.body
return Result.Failure(.HTTPResponseError(statusCode: statusCode, body: body))
}
} |
I'm not sure about this one. Inside an |
No, I don't want a PR just yet. Just to discuss if it make sense to have On Thu, Jan 7, 2016 at 11:32 AM, Pedro Vicente Gómez Sánchez <
|
We should review this code and maybe unify it inside NSHTTPClient.
The text was updated successfully, but these errors were encountered: