CocoaPods trunk is moving to be read-only. Read more on the blog, there are 14 months to go.
| TestsTested | ✗ |
| LangLanguage | SwiftSwift |
| License | MIT |
| ReleasedLast Release | Mar 2016 |
| SPMSupports SPM | ✗ |
Maintained by Piotr Sochalewski, Sunshinejr.
To run the example project, clone the repo, and run pod install from the Example directory first.
Swift 2.x
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"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 ObjectMapperTo make it works well, first, import the module.
import SwiftyRouterThen 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)
}
}Droids on Roids LLC
Piotr Sochalewski, [email protected]
Łukasz Mróz, [email protected]
SwiftyRouter is available under the MIT license. See the LICENSE file for more info.