|
| 1 | +# APIKit 2 Migration Guide |
| 2 | + |
| 3 | +APIKit 2.0 introduces several breaking changes to add functionality and to improve modeling of web API. |
| 4 | + |
| 5 | +- Abstraction of backend |
| 6 | +- Improved error handling modeling |
| 7 | +- Separation of convenience parameters and type-safe parameters |
| 8 | + |
| 9 | +## Errors |
| 10 | + |
| 11 | +- [**Deleted**] `APIError` |
| 12 | +- [**Added**] `SessionTaskError` |
| 13 | + |
| 14 | +Errors cases of `Session.sendRequest(_:handler:)` is reduced to 3 cases listed below: |
| 15 | + |
| 16 | +```swift |
| 17 | +public enum SessionTaskError: ErrorType { |
| 18 | + /// Error of networking backend such as `NSURLSession`. |
| 19 | + case ConnectionError(NSError) |
| 20 | + |
| 21 | + /// Error while creating `NSURLRequest` from `Request`. |
| 22 | + case RequestError(ErrorType) |
| 23 | + |
| 24 | + /// Error while creating `RequestType.Response` from `(NSData, NSURLResponse)`. |
| 25 | + case ResponseError(ErrorType) |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +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))`. |
| 30 | + |
| 31 | +## RequestType |
| 32 | + |
| 33 | +### Converting AnyObject to Response |
| 34 | + |
| 35 | +- [**Deleted**] `func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> Response?` |
| 36 | +- [**Added**] `func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) throws -> Response` |
| 37 | + |
| 38 | +### Handling response errors |
| 39 | + |
| 40 | +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. |
| 41 | + |
| 42 | +- [**Deleted**] `var acceptableStatusCodes: Set<Int> { get }` |
| 43 | +- [**Deleted**] `func errorFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> ErrorType?` |
| 44 | +- [**Added**] `func interceptObject(object: AnyObject, URLResponse: NSHTTPURLResponse) throws -> AnyObject` |
| 45 | + |
| 46 | +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. |
| 47 | + |
| 48 | +```swift |
| 49 | +func interceptObject(object: AnyObject, URLResponse: NSHTTPURLResponse) throws -> AnyObject { |
| 50 | + guard (200..<300).contains(URLResponse.statusCode) else { |
| 51 | + // https://developer.github.com/v3/#client-errors |
| 52 | + throw GitHubError(object: object) |
| 53 | + } |
| 54 | + |
| 55 | + return object |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +### Parameters |
| 60 | + |
| 61 | +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`. |
| 62 | + |
| 63 | +- [**Deleted**] `var parameters: [String: AnyObject]` |
| 64 | +- [**Deleted**] `var objectParameters: AnyObject` |
| 65 | +- [**Deleted**] `var requestBodyBuilder: RequestBodyBuilder` |
| 66 | +- [**Added**] `var parameters: AnyObject?` (convenience property) |
| 67 | +- [**Added**] `var bodyParameters: BodyParametersType?` (actual property) |
| 68 | +- [**Added**] `var queryParameters: [String: AnyObject]?` (actual property) |
| 69 | + |
| 70 | +Related types: |
| 71 | + |
| 72 | +- [**Deleted**] `enum RequestBodyBuilder` |
| 73 | +- [**Added**] `protocol BodyParametersType` |
| 74 | + |
| 75 | +APIKit provides 3 parameters types that conform to `BodyParametersType`: |
| 76 | + |
| 77 | +- [**Added**] `class JSONBodyParameters` |
| 78 | +- [**Added**] `class FormURLEncodedBodyParameters` |
| 79 | +- [**Added**] `class MultipartFormDataBodyParameters` |
| 80 | + |
| 81 | +### Data parsers |
| 82 | + |
| 83 | +- [**Deleted**] `var responseBodyParser: ResponseBodyParser` |
| 84 | +- [**Added**] `var dataParser: DataParserType` |
| 85 | + |
| 86 | +Related types: |
| 87 | + |
| 88 | +- [**Deleted**] `enum ResponseBodyParser` |
| 89 | +- [**Added**] `protocol DataParserType` |
| 90 | +- [**Added**] `class JSONDataParser` |
| 91 | +- [**Added**] `class FormURLEncodedDataParser` |
| 92 | +- [**Added**] `class StringDataParser` |
| 93 | + |
| 94 | +### Configuring NSURLRequest |
| 95 | + |
| 96 | +`configureURLRequest()` in 1.x is renamed to `interceptURLRequest()` for the consistency with `interceptObject()`. |
| 97 | + |
| 98 | +- [**Deleted**] `func configureURLRequest(URLRequest: NSMutableURLRequest) -> NSMutableURLRequest` |
| 99 | +- [**Added**] `func interceptURLRequest(URLRequest: NSMutableURLRequest) throws -> NSMutableURLRequest` |
| 100 | + |
| 101 | +## NSURLSession |
| 102 | + |
| 103 | +- [**Deleted**] `class URLSessionDelegate` |
| 104 | +- [**Added**] `protocol SessionTaskType` |
| 105 | +- [**Added**] `protocol SessionAdapterType` |
| 106 | +- [**Added**] `class NSURLSessionAdapter` |
0 commit comments