Releases: ishkawa/APIKit
1.0.0 beta3
- Support Swift 2 beta 5.
- Fix URL query encoding issue #59.
1.0.0 beta2
- Follow Swift 2 beta 4.
1.0.0 beta1
Redesign for Swift 2
This release contains significant breaking change.
See the following for how to migrate 0.x to 1.x.
Remove boxing
0.x
let request = SomeRequest()
SomeAPI.sendRequest(request) { result in
switch result {
case .Success(let box):
let response = box.value // response is a T.Response
case .Faikure(let box):
let error = box.value // error is a NSError
}
}
1.x
let request = SomeRequest()
SomeAPI.sendRequest(request) { result in
switch result {
case .Success(let response): // response is a T.Response
case .Failure(let error): // error is a NSError
}
}
Replace URLRequest
with method
, path
and parameters
0.x
struct SearchRepositories: Request {
var URLRequest: NSURLRequest? {
return GitHub.URLRequest(
method: .GET,
path: "/search/repositories",
parameters: ["q": query, "sort": sort.rawValue, "order": order.rawValue]
)
}
...
}
1.x
struct SearchRepositories: GitHubRequest {
var method: HTTPMethod {
return .GET
}
var path: String {
return "/search/repositories"
}
var parameters: [String: AnyObject] {
return ["q": query, "sort": sort.rawValue, "order": order.rawValue]
}
func configureURLRequest(URLRequest: NSMutableURLRequest) throws -> NSMutableURLRequest {
// You can add any configurations here
}
...
}
You can omit implementing parameters
and configureURLRequest
because they have default implementations.
Move properties in API
class to Request
protocol
In 1.x or later, creating abstract protocol like GitHubRequest
is required to provide common configurations
such as baseURL
, requestBodyBuilder
, responseBodyParser
and acceptableStatusCodes
.
0.x
class GitHubAPI: API {
override class var baseURL: NSURL {
return NSURL(string: "https://api.github.com")!
}
override class var requestBodyBuilder: RequestBodyBuilder {
return .JSON(writingOptions: nil)
}
override class var responseBodyParser: ResponseBodyParser {
return .JSON(readingOptions: nil)
}
override class var acceptableStatusCodes: Set<Int> {
return Set(200..<300)
}
override class func responseErrorFromObject(object: AnyObject) -> NSError {
...
}
...
}
1.x
protocol GitHubRequest: Request {
var baseURL: NSURL {
return NSURL(string: "https://api.github.com")!
}
var requestBodyBuilder: RequestBodyBuilder {
return .JSON(writingOptions: [])
}
var responseBodyParser: ResponseBodyParser {
return .JSON(readingOptions: [])
}
var acceptableStatusCodes: Set<Int> {
return Set(200..<300)
}
func errorFromObject(object: AnyObject) -> ErrorType? {
...
}
...
}
You can omit implementing requestBodyBuilder
, responseBodyParser
, acceptableStatusCodes
and errorFromObject()
because they have default implementations.
Add URLResponse
to responseFromObject()
and errorFromObject()
in Request
0.x
class func responseFromObject(object: AnyObject) -> Response? {
...
}
class func responseErrorFromObject(object: AnyObject) -> Response? {
...
}
1.x
func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> Response? {
...
}
func errorFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> ErrorType? {
...
}
Replace Method
with HTTPMethod
0.x
public enum Method: String {
case GET = "GET"
case POST = "POST"
...
}
1.x
public enum HTTPMethod: String {
case GET = "GET"
case POST = "POST"
...
}
Introdoce APIError
Errors in handler of sendRequest()
are now detailed as below:
public enum APIError: ErrorType {
case ConnectionError(NSError)
// request error
case InvalidBaseURL(NSURL)
case ConfigurationError(ErrorType)
case RequestBodySerializationError(ErrorType)
case FailedToCreateURLSessionTask
// response error
case UnacceptableStatusCode(Int, ErrorType)
case ResponseBodyDeserializationError(ErrorType)
case InvalidResponseStructure(AnyObject)
case NotHTTPURLResponse(NSURLResponse?)
}
0.8.2
0.8.1
0.8.0
This release contains 2 large changes
-
class func URLRequest(_:_:_:)
is deprecated to improve readability and flexibility.- Old:
class func URLRequest(_:_:_:)
- New:
class func URLRequest(method:path:parameters:requestBodyBuilder)
You can use the alternative function like below. The last 2 arguments,
parameters
andrequestBodyBuilder
, in new method have default values, so you can omit them.var URLRequest: NSURLRequest? { return GitHub.URLRequest( method: .GET, path: "/search/repositories", parameters: ["q": query, "sort": sort.rawValue, "order": order.rawValue] ) }
- Old:
-
LlamaKit is replaced by Result. Interface of
Box
type is changed due to this dependency replacement.- Old:
box.unbox
- New:
box.value
- Old:
0.7.2
0.7.1
0.7.0
- Swift 1.2 support
baseURL
,requestBodyBuilder
andResponseBodyParser
are changed to class var.
override class var baseURL: NSURL {
return NSURL(string: "https://api.github.com")!
}
override class var requestBodyBuilder: RequestBodyBuilder {
return .JSON(writingOptions: nil)
}
override class var responseBodyParser: ResponseBodyParser {
return .JSON(readingOptions: nil)
}