AlamofireRouter 1.0.0

AlamofireRouter 1.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Oct 2016
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Alex Corlatti.



AlamofireRouter

AlamofireRouter is a simple Alamofire router. If you need swift 2.3 version, use the 0.1.0 pod version

Example

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

Requirements

  • iOS 9.0+ / Mac OS X 10.9+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 8.0+

Usage

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 endpoint
  • method: 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 [[:]]

Create your router API

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"]])

        }

    }

}

Use it with Alamofire

Alamofire.request(MyAPI.myRequest(myParameter: "aValue")).response { resp in 

    // Do some stuff

}

Use it with AlamofireUIManager

let netManager = AlamofireUIManager.sharedInstance

netManager.request(MyAPI.myRequest(myParameter: "aValue"), completionHandler: { json in

    // Do some stuff

})

Author

Alex Corlatti, [email protected]

License

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