Releases: ishkawa/APIKit
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.
2.0.0-beta.7
- [Fixed] Use the return value of interceptURLRequest(_:).
1.4.1: Use the return value of configureURLRequest(_:)
2.0.0-beta.6
Update podspec
2.0.0-beta.5
- [Renamed]
HTTPHeaderFieldsinRequestTypeis renamed toheaderFields. - [Renamed]
resumedTaskWithURLRequest(_:)is renamed tocreateTaskWithURLRequest(_:). - [Added]
resume()is added toSessionTaskType. - [Added]
StringDataParseris added for unformatted response body. - [Changed] Exposed
RequestError. - [Changed] Type of associated value of
SessionTaskError.ConnectionErroris changed fromNSErrortoErrorType.
2.0.0-beta.4
1.4.0: Extensible shared session
Session.sharedSessionis nowclassproperty so that you can override it. #158
2.0.0-beta.3
- Migrate to Swift 2.2 syntax.
1.3.0: Swift 2.2
- Migrate to Swift 2.2 syntax.
2.0.0-beta.2
- Fixed CocoaPods spec.
NOTE: Since APIKit 2 is beta software, breaking changes will continue to occur.