Latest 3.1.0
Homepage https://github.com/rinov/Gryphon
License MIT
Platforms ios 8.3
Authors

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


API.Endpoint.request()
        .retry(max: 3)
        .interval(milliseconds: 500)

        .success { response in
            // Do something
        }

        .failure { error in
            // Do someting 
        }

How to use this?:eyes: (Example for retrieving a message)

Example of endpoint:

Sending request to http://rinov.jp/Gryphon-Tutorial by GET will return

[
    {
        "result": "Hello World!"
    }
]

Sending request to http://rinov.jp/Gryphon-Tutorial by POST will return

[
    {
        "result": "Sent a message"
    }
]

First of all, Create an API class.

e.g.


final class API {

    static let url = "http://rinov.jp/"

}

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

e.g. Using Alamofire with swift3.0.


extension API {

  final class Messages: Requestable {

        // required `Requestable`.
        static var baseURL: String {

            return API.url

        }

        // required `Requestable`.
        static var path: String {

            return baseURL + "Gryphon-Tutorial.php"

        }

        class func getMessage() -> Task<Message, Error> {

            let task = Task<Message, Error> { success, failure in

                Alamofire.request(path, method: .get, encoding: JSONEncoding.default)
                    .responseJSON(completionHandler: { response in

                        // Object mapping in your favorite way.
                        let result = (response.result.value as! [AnyObject])[0]["result"] as! String

                        let yourOwnCheck = true

                        if yourOwnCheck {

                            let message = Message(message: result)

                            success(message)

                        }else{

                            failure(ResponseError.unexceptedResponse(response.result.description))

                        }

                    })

            }

            return task

        }

        class func postMessage() -> Task<Int, Error> {

            let task = Task<Int, Error> { success, failure in

                Alamofire.request(path, method: .post, encoding: JSONEncoding.default)
                    .responseJSON(completionHandler: { response in

                        let yourOwnCheck = true

                        if yourOwnCheck {

                            success(response.response!.statusCode)

                        }else{

                            failure(ResponseError.unexceptedResponse(response.result.description))

                        }

                    })

            }

            return task

        }

   }

}

After that you can use it like this.


API.Messages.getMessage()

        // It will retry the request if that is timeout or failed.
        .retry(max: 3)

        // Specify the interval time of retry.
        .interval(milliseconds: 500)

        // If the response is valid, This will be called.
        .success { response in

            /*
            * You can use `response` without nil checking.
            * The type of `response` is automatically inferred to your Response class.
            * e.g. your Model class is `Message`
            */

            let message: Message = response // This is ok because response is NOT optional type.
            print(response.result) // This is ok because response has already object mapping.

        }

        // If the response is INVALID, this will be called.
        .failure { error in

            // Check the reason of error.
            print(error)

        }

Requirements

Swift2.3(iOS8+) or Swift3.0(iOS9+)

Installation

Swift3.0

In your Podfile:

platform :ios,'10.0'
use_frameworks!

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

and

$ pod install

CocoaPods 1.1.0+ is required to build Gryphon 2.0.0+.

Swift2.3

In your Podfile:

platform :ios,'8.0'
use_frameworks!

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

and

$ pod install

In case of XCode8+, Make Use Legacy Swift Language Version to YES in Build settings.

License

Gryphon is released under the MIT license.

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

Latest podspec

{
    "name": "Gryphon",
    "version": "3.1.0",
    "summary": "Type safe REST API kit for Swift.",
    "description": "Gryphon is an HTTP client kit that's type safe and convenient.",
    "homepage": "https://github.com/rinov/Gryphon",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Ryo Ishikawa": "[email protected]"
    },
    "source": {
        "git": "https://github.com/rinov/Gryphon.git",
        "tag": "3.1.0"
    },
    "platforms": {
        "ios": "8.3"
    },
    "source_files": "Gryphon/**/*",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This