Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
An Alamofire router example used in a post, https://chaione.com/blog/rethinking-routers-swift-protocol-oriented-programming-part-1, to show how a router with multiple paths would look like.
// Router.swift
import Alamofire
enum Router: URLRequestConvertible {
static let baseURLString = "https://private-85a46-routable.apiary-mock.com/"
case readUsers
case createUser(parameters: Parameters)
case readUser(id: String)
case updateUser(id: String, parameters: Parameters)
case destroyUser(id: String)
case readPosts
case createPost(parameters: Parameters)
case readPost(id: String)
case updatePost(id: String, parameters: Parameters)
case destroyPost(id: String)
var method: HTTPMethod {
switch self {
case .readUsers,
.readUser,
.readPosts,
.readPost:
return .get
case .createUser,
.createPost:
return .post
case .updateUser,
.updatePost:
return .put
case .destroyUser,
.destroyPost:
return .delete
}
}
var path: String {
switch self {
case .readUsers:
return "users"
case .createUser:
return "users"
case .readUser(let username):
return "users/\(username)"
case .updateUser(let username, _):
return "users/\(username)"
case .destroyUser(let username):
return "users/\(username)"
case .readPosts:
return "posts"
case .createPost:
return "posts"
case .readPost(let id):
return "posts/\(id)"
case .updatePost(let id, _):
return "posts/\(id)"
case .destroyPost(let id):
return "posts/\(id)"
}
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment