Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

Mapping of BothamAPIError is done in NSHTTPClient and HTTPClient #30

Open
davideme opened this issue Jan 7, 2016 · 4 comments
Open
Assignees
Milestone

Comments

@davideme
Copy link
Member

davideme commented Jan 7, 2016

We should review this code and maybe unify it inside NSHTTPClient.

@davideme davideme added this to the Release 1.0.0 milestone Jan 7, 2016
@pedrovgs
Copy link
Contributor

pedrovgs commented Jan 7, 2016

I don't understand you. Right now the implementation is just in HTTPClient using an extension.

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 NSHTTPClient uses this method here:

 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()
    }

@davideme
Copy link
Member Author

davideme commented Jan 7, 2016

Sorry my bad.
I meant BothamAPIClient and NSHTTPClient/HTTPClient.

///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))
            }
    }

@pedrovgs
Copy link
Contributor

pedrovgs commented Jan 7, 2016

I'm not sure about this one. Inside an HTTPClient implementation a response with a 401 is not an error. I mean, the request was sent to the server and the server responded with an error. But, we've changed HTTPClient api to use BothamAPIClientError. I'm going to prepare a PR with the change, so we can discuss with some code :)

@davideme
Copy link
Member Author

davideme commented Jan 7, 2016

No, I don't want a PR just yet. Just to discuss if it make sense to have
this 2 layer.

On Thu, Jan 7, 2016 at 11:32 AM, Pedro Vicente Gómez Sánchez <
[email protected]> wrote:

I'm not sure about this one. Inside an HTTPClient implementation a
response with a 401 is not an error. I mean, the request was sent to the
server and the server responded with an error. But, we've changed
HTTPClient api to use BothamAPIClientError. I'm going to prepare a PR
with the change, so we can discuss with some code :)


Reply to this email directly or view it on GitHub
#30 (comment)
.

@pedrovgs pedrovgs modified the milestones: post-1.0.0, Release 1.0.0 Feb 9, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants