Modeling API Endpoints

Episode #194 | 6 minutes | published on October 29, 2015
Subscribers Only
In this episode we talk about modeling API Endpoints as first class types, rather than relying on strings and string interpolation scattered across your application. For this we'll leverage Swift enums with associated values.

Episode Links

let baseURL = NSURL(string: "http://httpbin.org")!

enum Endpoint {
    case UserAgent
    case Links(Int)

    func url() -> NSURL {
        switch self {
        case .UserAgent:
            return baseURL.URLByAppendingPathComponent("/user-agent")
        case .Links(let n):
            return baseURL.URLByAppendingPathComponent("/links/\(n)")
        }
    }
}

request(Endpoint.UserAgent.url())
request(Endpoint.Links(5).url())
blog comments powered by Disqus
岛国av无码免费无禁网站_岛国在线无码免费观_岛国一区二区三区视频在线