TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Oct 2016 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Alex Corlatti.
AlamofireRouter is a simple Alamofire router. If you need swift 2.3 version, use the 0.1.0 pod version
To run the example project, clone the repo, and run pod install
from the Example directory first.
You have to create an enum
that conform RouterProtocol
, so you need variable router
and function URLRequest
are mandatories.
In router
you have to set your API base URL.
In URLRequest
you can create your endpoints by function router.endPoint
.
The function router.endPoint
has four parameters:
path: String
the path for the endpointmethod: Alamofire.HTTPMethod
the method of the request, by default is .get
parameters: [String : Any]
the parameters if necessary, by default is [ : ]
encoding: ParameterEncoding
the encoding type, by default is URLEncoding.default
headers: [[String: String]]
the header parameters if necessary, by default is [[:]]
import AlamofireRouter
enum MyAPI: RouterProtocol {
case myRequest(myParameter: String)
case otherRequest(otherParameter: [String: Any])
case allPersonalizedRequest(param: String)
var router: Router { return Router(baseURL: "http://YOUR_API_BASE_URL.com/") }
public func asURLRequest() throws -> URLRequest {
switch self {
case .myRequest(let myParameter):
return router.endPoint(path: "mypath/\(myParameter)")
case .otherRequest(let otherParameter):
return router.endPoint(path: "otherPath", method: .post, parameters: otherParameter)
case .allPersonalizedRequest(let param):
return router.endPoint(path: "myPath", method: .patch, parameters: ["par" : param], encoding: JSONEncoding.prettyPrinted, headers: [["Content-type": "json"]])
}
}
}
Alamofire.request(MyAPI.myRequest(myParameter: "aValue")).response { resp in
// Do some stuff
}
let netManager = AlamofireUIManager.sharedInstance
netManager.request(MyAPI.myRequest(myParameter: "aValue"), completionHandler: { json in
// Do some stuff
})
Alex Corlatti, [email protected]
AlamofireRouter is available under the MIT license. See the LICENSE file for more info.