Skip to content

1.0.0 beta1

Pre-release
Pre-release
Compare
Choose a tag to compare
@ishkawa ishkawa released this 30 Jun 01:13
· 709 commits to master since this release

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?)
}