Here's an example of an Alamofire router
import Foundation
import Alamofire
enum PostRouter: URLRequestConvertible {
static let baseURLString = "http://jsonplaceholder.typicode.com/"
case Get(Int)
case Create([String: AnyObject])
case Delete(Int)
var URLRequest: NSMutableURLRequest {
var method: Alamofire.Method {
switch self {
case .Get:
return .GET
case .Create:
return .POST
case .Delete:
return .DELETE
}
}
let params: ([String: AnyObject]?) = {
switch self {
case .Get, .Delete:
return (nil)
case .Create(let newPost):
return (newPost)
}
}()
let url:NSURL = {
// build up and return the URL for each endpoint
let relativePath:String?
switch self {
case .Get(let postNumber):
relativePath = "posts/\(postNumber)"
case .Create:
relativePath = "posts"
case .Delete(let postNumber):
relativePath = "posts/\(postNumber)"
}
var URL = NSURL(string: PostRouter.baseURLString)!
if let relativePath = relativePath {
URL = URL.URLByAppendingPathComponent(relativePath)
}
return URL
}()
let URLRequest = NSMutableURLRequest(URL: url)
let encoding = Alamofire.ParameterEncoding.JSON
let (encodedRequest, _) = encoding.encode(URLRequest, parameters: params)
encodedRequest.HTTPMethod = method.rawValue
return encodedRequest
}
}
What this basically does is create,delete or get Posts. This code looks clean for a simple app that does just this. What about an app that has large amount of endpoints and models ? Let's just say we have to do different operations on entities like Users,Companies, Payments, Comments, Channels, etc. I'm talking about large number of endpoints and methods.
In this case is it necessary to create one router for each endpoint ? It doesn't feel like , is it a good practice to use same router class for all these endpoints.
A similar question has been asked here : Proper usage of the Alamofire's URLRequestConvertible
But I'm still not satisfied with the answers to above question since accepted answer suggests to use same router for all endpoints. What is the best practice here?