1

Following the Alamofire Documentation, the way to create and use a Router for CRUD and Authorization is

    enum Router: URLRequestConvertible {
        static let baseURLString = "http://example.com"
        static var OAuthToken: String?

        case CreateUser([String: AnyObject])
        case ReadUser(String)
        case UpdateUser(String, [String: AnyObject])
        case DestroyUser(String)

        var method: Alamofire.Method {
            switch self {
            case .CreateUser:
                return .POST
            case .ReadUser:
                return .GET
            case .UpdateUser:
                return .PUT
            case .DestroyUser:
                return .DELETE
            }
        }

        var path: String {
            switch self {
            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)"
            }
        }

        // MARK: URLRequestConvertible

        var URLRequest: NSURLRequest {
            let URL = NSURL(string: Router.baseURLString)!
            let mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(path))
            mutableURLRequest.HTTPMethod = method.rawValue

            if let token = Router.OAuthToken {
                mutableURLRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
            }

            switch self {
            case .CreateUser(let parameters):
                return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
            case .UpdateUser(_, let parameters):
                return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
            default:
                return mutableURLRequest
            }
        }
    }

Alamofire.request(Router.ReadUser("mattt")) // GET /users/mattt

but this Router only works with the user model, and I'm wondering if is there any way to use this Router with more than one model.

Edu
  • 460
  • 12
  • 34

2 Answers2

1

look at here: Proper usage of the Alamofire's URLRequestConvertible

they are trying to do the same, but they think that making it bigger could get hard to handle

Community
  • 1
  • 1
1

Take a look at the Sweet router. It's type-safe, declarative and has some nice features like IP type.

Your Router would become something like:

struct Api: EndpointType {
    enum Route: RouteType {
        case createUser([String: AnyObject])
        case readUser(String)
        case updateUser(String, [String: AnyObject])
        case destroyUser(String)

        var defaultPath: URL.Path {
            return URL.Path("users")
        }

        var route: URL.Route {
            switch self {
            case .createUser(_):
                return .init(path: [])
            case let .readUser(username), let .updateUser(username, _), let .destroyUser(username):
                return .init(at: username)
            }
        }
    }

    static let current = URL.Environment(.http, "example.com")
}

And then you can use it like this:

Alamofire.request(Router<Api>(at: .readUser("username"))
Noobass
  • 1,974
  • 24
  • 26