Upgrade to Pro — share decks privately, control downloads, hide ads and more …

APIKit

 APIKit

関西モバイルアプリ研究会 #2 - connpass
http://kanmoba.connpass.com/event/14252/

USAMI Kosuke

May 20, 2015
Tweet

More Decks by USAMI Kosuke

Other Decks in Programming

Transcript

  1. API$Λ࢖͏ଆͷίʔυ let request = HogeApi.Endpoint.Login(user: "hoge", pass: "hoge") HogeApi.sendRequest(request) {

    response in switch response { case .Success(let box): let loginResponse = box.value // ਖ਼ৗϨεϙϯε ... case .Failure(let box): let error = box.value // NSError ...
  2. API$Λ࣮૷͢Δଆͷίʔυ$(1) class HogeApi: API { override class var baseURL: NSURL

    { return NSURL(string: "https://hogehoge/api")! } class Endpoint { // ͜ͷதʹ֤ΤϯυϙΠϯτͷΫϥεΛ࣮૷͠·͢ // ࠓճ͸ผϑΝΠϧʹ෼͚ͯ extension Ͱ࣮૷͠·ͨ͠ } }
  3. API$Λ࣮૷͢Δଆͷίʔυ$(2) extension HogeApi.Endpoint { class Login: Request { let user:

    String let pass: String var URLRequest: NSURLRequest? { return HogeApi.URLRequest(method: .GET, path: "login", parameters: ["user": user, "pass": pass]) } typealias Response = LoginResponse static func responseFromObject(object: AnyObject) -> Response? { return LoginResponse(object) } ... }
  4. API$Λ࣮૷͢Δଆͷίʔυ$(3) extension HogeApi.Endpoint { ... class LoginResponse { let json:

    JSON // SwiftyJSON let token: String init (_ object: AnyObject) { let json = JSON(object) self.json = json self.token = json["token"].stringValue } } }
  5. APIKit'ͷίʔυʢ࠶ܝʣ let request = HogeApi.Endpoint.Login(user: "hoge", pass: "hoge") HogeApi.sendRequest(request) {

    response in switch response { case .Success(let box): let loginResponse = box.value // LoginResponse ... case .Failure(let box): let error = box.value // NSError ...
  6. an#typical/Result/ͷ׆༻ enum Result<T, E> { case Success(Box<T>) case Failure(Box<E>) }

    • response"͸"Result<T.Response, NSError>"ܕ • .Success"ͷͱ͖"LoginResponse"ܕ • .Failure"ͷͱ͖"NSError"ܕ