| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
APIKit 2.0 introduces several breaking changes to add functionality and to improve modeling of web API.
Errors cases of Session.sendRequest(_:handler:) is reduced to 3 cases listed below:
public enum SessionTaskError: ErrorType {
/// Error of networking backend such as `NSURLSession`.
case ConnectionError(ErrorType)
/// Error while creating `NSURLRequest` from `Request`.
case RequestError(ErrorType)
/// Error while creating `RequestType.Response` from `(NSData, NSURLResponse)`.
case ResponseError(ErrorType)
}These error cases describes where the error occurred, not what is the error. You can throw any kind of error while building NSURLRequest and converting NSData to Response. Session catches the error you threw and wrap it into one of the cases defined in SessionTaskError. For example, if you throw SomeError in responseFromObject(_:URLResponse:), the closure of Session.sendRequest(_:handler:) receives .Failure(.ResponseError(SomeError)).
In 1.x, Session checks if the actual status code is contained in RequestType.acceptableStatusCodes. If it is not, Session calls errorFromObject() to obtain custom error from response object. In 2.x, Session always call interceptObject() before calling responseFromObject(), so you can validate AnyObject and NSHTTPURLResponse in interceptObject() and throw error initialized with them.
For example, the code below checks HTTP status code, and if the status code is not 2xx, it throws an error initialized with error JSON GitHub API returns.
func interceptObject(object: AnyObject, URLResponse: NSHTTPURLResponse) throws -> AnyObject {
guard 200..<300 ~= URLResponse.statusCode else {
// https://developer.github.com/v3/#client-errors
throw GitHubError(object: object)
}
return object
}To satisfy both ease and accuracy, parameters property is separated into 1 convenience property and 2 actual properties. If you implement convenience parameters only, 2 actual parameters are computed by default implementation of RequestType.
Related types:
APIKit provides 3 parameters types that conform to BodyParametersType:
Related types:
configureURLRequest() in 1.x is renamed to interceptURLRequest() for the consistency with interceptObject().
| Back | FazBrowse Home | New Git URL |