Repository navigation
2.0.0
🎉
APIKit 2 introduces 3 major features below:
- New error handling model
- Convenience parameters and actual parameters
- Abstraction of networking backend
See the migration guide and following summary for more details.
New error handling model
The error type of Session.sendRequest(_:) is changed to SessionTaskError:
public enum SessionTaskError: ErrorType {
case ConnectionError(ErrorType)
case RequestError(ErrorType)
case ResponseError(ErrorType)
}These error cases describes where the error occurred, not what is the error. You can throw any kind of error to notify what is happened in the following methods:
public protocol RequestType {
...
// The error thrown here will be the associated value of SessionTaskError.RequestError
func interceptURLRequest(URLRequest: NSMutableURLRequest) throws -> NSMutableURLRequest
// The error thrown here will be the associated value of SessionTaskError.ResponseError
func interceptObject(object: AnyObject, URLResponse: NSHTTPURLResponse) throws -> AnyObject
}Convenience parameters and actual parameters
Usually, you can specify request parameters in dictionary-literal or array-literal like below:
struct SomeRequest: RequestType {
...
var parameters: AnyObject? {
return ["q": "Swift"]
}
}var parameters is the convenience parameters. It is define as below:
public protocol RequestType {
...
var parameters: AnyObject? { get }
}Actually, we have to translate the literal into HTTP/HTTPS request. There are 2 places to express parameters, URL query and body. RequestType has interface to express them, var queryParameters: [String: AnyObject]? and var bodyParameters: BodyParametersType?. Those are the actual parameters.
public protocol RequestType {
...
var queryParameters: [String: AnyObject]? { get }
var bodyParameters: BodyParametersType? { get }
}If you implement convenience parameters only, the actual parameters are computed from the convenience parameters depending on HTTP method.
APIKit provides 3 types that conforms to BodyParametersType:
| Name | Parameters Type |
|---|---|
JSONBodyParameters |
AnyObject |
FormURLEncodedBodyParameters |
[String: AnyObject] |
MultipartFormDataBodyParameters |
[MultipartFormDataBodyParameters.Part] |
Abstraction of networking backend
APIKit uses NSURLSession as networking backend by default. Since Session in APIKit 2 has abstraction layer of backend called SessionAdapterType, you can change the backend of Session like below:
- Third party HTTP client like Alamofire
- Mock backend like
TestSessionAdapter NSURLSessionwith custom configuration and delegate
Demo implementation of Alamofire adapter is available here.