CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.

Gryphon 3.1.0

Gryphon 3.1.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Nov 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Ryo Ishikawa.



Gryphon 3.1.0

  • By
  • Ryo Ishikawa

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 😋

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?👀

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