Latest 0.1.3.1
Homepage https://github.com/DroidsOnRoids/SwiftyRouter
License MIT
Platforms ios 8.0, osx 10.9, watchos 2.0, tvos 9.0, requires ARC
Frameworks Foundation
Authors ,

Version
License
Platform

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Swift 2.x

Installation

SwiftyRouter is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "SwiftyRouter"

If you want to parse response to objects, and you are using ObjectMapper, ModelMapper or SwiftyJSON we got you covered. Simply use one of the lines below:

pod "SwiftyRouter/ObjectMapper"
pod "SwiftyRouter/ModelMapper"
pod "SwiftyRouter/SwiftyJSON"

Usage

SwiftyRouter lets you get easily raw NSData, parsed JSON or mapped model received from API request this way:

MyAPI.Authenticate().request(_:)
MyAPI.List().request().parseJSON(_:)
MyAPI.UserInfo(username: "trickyusername").request().parseObject(_:) // via ModelMapper or ObjectMapper

Example

To make it works well, first, import the module.

import SwiftyRouter

Then create enum for selected API service, i.e. GitHub API. Remember to implement Endpointable protocol with all required properties (meaning baseUrl and endpoint). In this case we cover two endpoints: user repos, and user info.

enum Github: Endpointable {

    case Repos(String)
    case UserInfo(String)

    var baseUrl: String { return "https://api.github.com" }

    var endpoint: Subendpointable {
        switch self {
        case .Repos(let username):
            return ReposEndpoint(username: username)
        case .UserInfo(let username):
            return UserInfoEndpoint(username: username)
        }
    }

}

Then implement previously defined endpoints like structs or classes with Subendpointable protocol. One more time fill all required properties (path, method, parameters and headers). We’ve got here quite convenient init with username being part of URL.

struct ReposEndpoint: Subendpointable {

    // We specify the parameters
    let username: String!

    // Required methods/parameters
    var path: String { return "/users/(username)/repos" }
    var method: EndpointMethod { return .GET }
    var parameters: [String: AnyObject]? { return nil }
    var headers: [String : String]? { return nil }

    init(username: String) {
        self.username = username
    }

}

struct UserInfoEndpoint: Subendpointable {

    // We specify the parameters
    let username: String!

    // Required methods/parameters
    var path: String { return "/users/(username)" }
    var method: EndpointMethod { return .GET }
    var parameters: [String: AnyObject]? { return nil }
    var headers: [String : String]? { return nil }

    init(username: String) {
        self.username = username
    }

}

After that everything is done. You can easily receive needed data this way.

Github.UserInfo("DroidsOnRoids").request { result in
    switch result {
    case .Success(let data):
        print("Just NSData: (data)")
    case .Failure(let error):
        print(error)
    }
}

Need more than just clear NSData? Try parseJSON().

Github.Repos("DroidsOnRoids").request().parseJSON { result in
    switch result {
    case .Success(let json):
        print("What a beautiful dict!n(json)")
    case .Failure(let error):
        print(error)
    }
}

You can also map the response to objects using ObjectMapper or ModelMapper (needs additional pod specified in Installation).

Github.Repos("DroidsOnRoids").request().parseArray(Repository.self) { result in
    switch result {
    case .Success(let object):
        print(object)
    case .Failure(let error):
        print(error)
    }
}

Author

Droids on Roids LLC
Piotr Sochalewski, [email protected]
Łukasz Mróz, [email protected]

License

SwiftyRouter is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "SwiftyRouter",
    "version": "0.1.3.1",
    "summary": "Easy networking in Swift",
    "description": "SwiftyRouter makes it easy to deal with network connection in Swift",
    "homepage": "https://github.com/DroidsOnRoids/SwiftyRouter",
    "license": "MIT",
    "authors": {
        "Piotr Sochalewski": "[email protected]",
        "u0141ukasz Mrou0301z": "[email protected]"
    },
    "source": {
        "git": "https://github.com/DroidsOnRoids/SwiftyRouter.git",
        "tag": "0.1.3.1"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "requires_arc": true,
    "frameworks": "Foundation",
    "default_subspecs": "Core",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "SwiftyRouter.swift",
            "dependencies": {
                "Alamofire": [
                    "~> 3.2"
                ]
            }
        },
        {
            "name": "ObjectMapper",
            "source_files": "ObjectMapper/SwiftyRouter+ObjectMapper.swift",
            "dependencies": {
                "ObjectMapper": [
                    "~> 1.1"
                ],
                "SwiftyRouter/Core": []
            }
        },
        {
            "name": "SwiftyJSON",
            "source_files": "SwiftyJSON/SwiftyRouter+SwiftyJSON.swift",
            "dependencies": {
                "SwiftyJSON": [
                    "~> 2.3"
                ],
                "SwiftyRouter/Core": []
            }
        },
        {
            "name": "ModelMapper",
            "ios": {
                "source_files": "ModelMapper/SwiftyRouter+ModelMapper.swift"
            },
            "dependencies": {
                "ModelMapper": [
                    "~> 1.0"
                ],
                "SwiftyRouter/Core": []
            },
            "platforms": {
                "ios": "8.0",
                "osx": "10.10",
                "watchos": "2.0",
                "tvos": "9.0"
            }
        }
    ]
}

Pin It on Pinterest

Share This