Gryphon icon indicating copy to clipboard operation
Gryphon copied to clipboard

Gryphon is a API client kit written in Swift.

Gryphon (REST API kit for Swift)

CI Status Version Platform [Language LICENSE

Gryphon is a REST API kit that's type safe and convenient for Swift :yum:

Figure

Usage


```swift
API
    .Messages
    .getMessage()
    .retry(max: 3) // It will retry the API request if that is timeout or failed.
    .interval(milliseconds: 500) // Specify the interval time of retry.
    .response{ result in
        /*
        * You can use `result` without nil checking.
        * The type of `result` is automatically inferred to your designation type.
        * e.g. This case of `result` is a type of `String`
        */

        switch result {
        case let .response(message):
            // Do something
        case let .error(error):
            // Do something
        }
    }

How to use this?:eyes:

First of all, Create an API class.

e.g.


final class API {
    ....
}

Next step , Implement your request in compliance with Requestable protocol.

e.g.

extension API {
    final class Messages: Requestable {
    
    // required `Requestable`
    static var baseURL: String {
        return "http://rinov.jp/"
    }

    // required `Requestable`
    static var path: String {
        return "Gryphon-Tutorial.php"
    }

    // Returns message(String) from server or error reason(Error).
    class func getMessage() -> Task<String, Error> {
        let task = Task<String, Error> { result in
            let url = URL(string: baseURL + path)!
            var request = URLRequest(url: url)
            request.httpMethod = "GET"
            let session = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
                guard let data = data,
                    let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [AnyObject],
                    let message = json?[0]["result"] as? String else {
                        result(.error(ResponseError.unexceptedResponse(error as AnyObject)))
                        return
                }
                result(.response(message))
            })
            session.resume()
            }
            return task
        }
    }
}

After that you can use it like this.


API
    .Messages
    .getMessage()
    .retry(max: 3) // It will retry the API request if that is timeout or failed.
    .interval(milliseconds: 500) // Specify the interval time of retry.
    .response{ result in
        switch result {
        case let .response(message):
            // Do something
        case let .error(error):
            // Do something
        }
    }

Requirements

Swift4 XCode9

Installation

In your Podfile:

use_frameworks!

target 'YOUR_TARGET_NAME' do
  pod 'Gryphon', '~> 3.1'
end

and

$ pod install

License

Gryphon is released under the MIT license.

https://github.com/rinov/Gryphon/blob/master/LICENSE