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

RxSwiftで作るAPIラッパーのパターン

 RxSwiftで作るAPIラッパーのパターン

第2回RxSwift勉強会 @ Sansan
http://connpass.com/event/35014/

Yusuke Murata

August 19, 2016
Tweet

More Decks by Yusuke Murata

Other Decks in Programming

Transcript

  1.  ੒ޭPSࣦഊ͚ͩΘ͔Ε͹͍͍ func signIn(email: String, password: String) -> Driver<Bool> {

    let params = ["user[email]": email, "user[password]": password] return rx_responseJSON(.POST, endPoint: .SignIn, parameters: params) .map { success, _ in success } .asDriver(onErrorJustReturn: false) } ྫɿϩάΠϯ"1*
  2.  ੒ޭ͚ͨ࣌ͩ͠஋͕ཉ͍͠ private func rx_responseJSON(method: Alamofire.Method, url: String, parameters: [String:

    AnyObject]? = nil, encoding: ParameterEncoding = .URL, headers: [String: String]? = nil) -> Observable<(Bool, AnyObject?)> { return manager .rx_responseJSON(method, url, parameters: parameters, ɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹencoding: encoding, headers: headers) .map { response, json in if 200 ..< 300 ~= response.statusCode { return (true, json) } else { return (false, json) } } } SY@SFTQPOTF+40/ͷத਎
  3.  ੒ޭPSࣦഊ͚ͩΘ͔Ε͹͍͍ func signIn(email: String, password: String) -> Driver<Bool> {

    let params = ["user[email]": email, "user[password]": password] return rx_responseJSON(.POST, endPoint: .SignIn, parameters: params) .map { success, _ in success } .asDriver(onErrorJustReturn: false) } ྫɿϩάΠϯ"1* ɾجຊ%SJWFSΛฦ͢ ɹˠඇಉظͷ6*ૢ࡞ʹ΋͍ͬͯ͜ ɹɹ$BO`UFSSPSPVU ɹɹ0CTFSWFPONBJOTDIFEVMFS ɹɹ4IBSJOHTJEFFGGFDUT ɹɹIUUQTHJUIVCDPN3FBDUJWF93Y4XJGUCMPCNBTUFS%PDVNFOUBUJPO6OJUTNEESJWFSVOJU
  4.  ੒ޭPSࣦഊ͚ͩΘ͔Ε͹͍͍ func signIn(email: String, password: String) -> Driver<Bool> {

    let params = ["user[email]": email, "user[password]": password] return rx_responseJSON(.POST, endPoint: .SignIn, parameters: params) .map { success, _ in success } .asDriver(onErrorJustReturn: false) } ྫɿϩάΠϯ"1* ɾܕ͸#PPMʹ͢Δ ɹˠ੒ޭ͔ࣦഊ͔ͷ୒ ɹˠ0QUJPOBMͰ͋Δඞཁ΋ͳ͍
  5.  ੒ޭPSࣦഊ͚ͩΘ͔Ε͹͍͍ func signIn(email: String, password: String) -> Driver<Bool> {

    let params = ["user[email]": email, "user[password]": password] return rx_responseJSON(.POST, endPoint: .SignIn, parameters: params) .map { success, _ in success } .asDriver(onErrorJustReturn: false) } ྫɿϩάΠϯ"1* ɾͳͷͰࣦഊͨ͠ΒGBMTFฦ͠ͱ͚͹͍͍
  6.  ੒ޭPSࣦഊ͚ͩΘ͔Ε͹͍͍ let input = Driver.combineLatest(email, password) { ($0, $1)

    } loginButton.rx_tap.asDriver() .withLatestFrom(input) .flatMapLatest { mail, password in api.signIn(mail, password: password) } .driveNext { [unowned self] success in if success { self.dismissViewControllerAnimated(true, completion: nil) } else { // show error message } } .addDisposableTo(disposeBag) ྫɿϩάΠϯ"1* ɾ࢖͏ଆʢ6*7JFXªPOUSPMMFSʣ͸͜Μͳײ͡
  7.  ੒ޭ͚ͨ࣌ͩ͠஋͕ཉ͍͠ func fetchUserInfo() -> Driver<User?> { return rx_responseJSON(.GET, endPoint:

    .Users) .map { success, json in if success { guard let json = json as? [String: AnyObject] else { throw apiError("User parse error") } return try User(json: json) } else { return nil } } .asDriver(onErrorJustReturn: nil) } ྫɿϢʔβʔ৘ใ
  8.  ੒ޭ͚ͨ࣌ͩ͠஋͕ཉ͍͠ func fetchUserInfo() -> Driver<User?> { return rx_responseJSON(.GET, endPoint:

    .Users) .map { success, json in if success { guard let json = json as? [String: AnyObject] else { throw apiError("User parse error") } return try User(json: json) } else { return nil } } .asDriver(onErrorJustReturn: nil) } ྫɿϢʔβʔ৘ใ ɾཉ͍͠σʔλͷܕΛࢦఆ
  9.  ੒ޭ͚ͨ࣌ͩ͠஋͕ཉ͍͠ func fetchUserInfo() -> Driver<User?> { return rx_responseJSON(.GET, endPoint:

    .Users) .map { success, json in if success { guard let json = json as? [String: AnyObject] else { throw apiError("User parse error") } return try User(json: json) } else { return nil } } .asDriver(onErrorJustReturn: nil) } ྫɿϢʔβʔ৘ใ ɾ੒ޭ࣌͸ཉ͍͠ܕͷσʔλΛฦ͢
  10.  ੒ޭ͚ͨ࣌ͩ͠஋͕ཉ͍͠ func fetchUserInfo() -> Driver<User?> { return rx_responseJSON(.GET, endPoint:

    .Users) .map { success, json in if success { guard let json = json as? [String: AnyObject] else { throw apiError("User parse error") } return try User(json: json) } else { return nil } } .asDriver(onErrorJustReturn: nil) } ྫɿϢʔβʔ৘ใ ɾ0QUJPOBMʹͯ͠ɺࣦഊͳΒOJMΛฦ͢
  11.  ੒ޭ͚ͨ࣌ͩ͠஋͕ཉ͍͠ api.fetchUserInfo() .driveNext { [unowned self] (user: User?) in

    self.user = user } .addDisposableTo(disposeBag) ྫɿϢʔβʔ৘ใ ɾ࢖͏ଆʢ6*7JFXªPOUSPMMFSʣ͸͜Μͳײ͡
  12.  ࣦഊ͚ͨ࣌ͩ͠Τϥʔ಺༰͕ཉ͍͠ func signUp(email: String, password: String, passwordConfirm: String, firstName:

    String, lastName: String) -> Driver<(Bool, [String])> { let params = [ "user[email]": email, "user[password]": password, "user[password_confirmation]": passwordConfirm, "user[first_name]": firstName, "user[last_name]": lastName ] return rx_responseJSON(.POST, endPoint: .Users, parameters: params) .map { success, json in return (success, parseJSON(json)) } .asDriver(onErrorJustReturn: (false, [])) } ྫɿϢʔβʔొ࿥
  13.  ࣦഊ͚ͨ࣌ͩ͠Τϥʔ಺༰͕ཉ͍͠ func signUp(email: String, password: String, passwordConfirm: String, firstName:

    String, lastName: String) -> Driver<(Bool, [String])> { let params = [ "user[email]": email, "user[password]": password, "user[password_confirmation]": passwordConfirm, "user[first_name]": firstName, "user[last_name]": lastName ] return rx_responseJSON(.POST, endPoint: .Users, parameters: params) .map { success, json in return (success, parseJSON(json)) } .asDriver(onErrorJustReturn: (false, [])) } ྫɿϢʔβʔొ࿥ ɾ੒ޭͨ͠ΒUSVF͚ͩฦͬͯ͘Ε͹͍͍
  14.  ࣦഊ͚ͨ࣌ͩ͠Τϥʔ಺༰͕ཉ͍͠ func signUp(email: String, password: String, passwordConfirm: String, firstName:

    String, lastName: String) -> Driver<(Bool, [String])> { let params = [ "user[email]": email, "user[password]": password, "user[password_confirmation]": passwordConfirm, "user[first_name]": firstName, "user[last_name]": lastName ] return rx_responseJSON(.POST, endPoint: .Users, parameters: params) .map { success, json in return (success, parseJSON(json)) } .asDriver(onErrorJustReturn: (false, [])) } ྫɿϢʔβʔొ࿥ ɾࣦഊͷ಺༰͸จࣈྻͷ഑ྻͰฦ͢ ɹˠϝʔϧΞυϨεΛೖྗ͍ͯͩ͘͠͞ ɹɹύεϫʔυΛೖྗ͍ͯͩ͘͠͞FUD
  15.  ࣦഊ͚ͨ࣌ͩ͠Τϥʔ಺༰͕ཉ͍͠ signupButton.rx_tap.asDriver() .withLatestFrom(input) .flatMapLatest { api.signUp($0.0, password: $0.1, passwordConfirm:

    $0.2, firstName: $0.3, lastName: $0.4) } .driveNext { [unowned self] success, errors in self.signupErrorLabel.hidden = success self.signupErrorLabel.text = errors.joinWithSeparator("\n") } .addDisposableTo(disposeBag) ྫɿϢʔβʔొ࿥ ɾ࢖͏ଆʢ6*7JFXªPOUSPMMFSʣ͸͜Μͳײ͡
  16.  ੒ޭͨ͠Β஋͕ɺࣦഊͨ͠ΒΤϥʔ಺༰͕ཉ͍͠ func hoge(fuga: String) -> Driver<(Piyo?, [String])> { let

    params = ["fuga": fuga] return rx_responseJSON(.POST, endPoint: .Hoge, parameters: params) .map { success, json in if success { return (Piyo.parseJSON(json), []) } else { return (nil, parseJSON(json)) } } .asDriver(onErrorJustReturn: (nil, [])) } ྫɿʁʁʁ
  17.  ੒ޭͨ͠Β஋͕ɺࣦഊͨ͠ΒΤϥʔ಺༰͕ཉ͍͠ func hoge(fuga: String) -> Driver<(Piyo?, [String])> { let

    params = ["fuga": fuga] return rx_responseJSON(.POST, endPoint: .Hoge, parameters: params) .map { success, json in if success { return (Piyo.parseJSON(json), []) } else { return (nil, parseJSON(json)) } } .asDriver(onErrorJustReturn: (nil, [])) } ྫɿʁʁʁ ɾͱͷ૊Έ߹Θͤ
  18.  ੒ޭͨ͠Β஋͕ɺࣦഊͨ͠ΒΤϥʔ಺༰͕ཉ͍͠ func hoge(fuga: String) -> Driver<(Piyo?, [String])> { let

    params = ["fuga": fuga] return rx_responseJSON(.POST, endPoint: .Hoge, parameters: params) .map { success, json in if success { return (Piyo.parseJSON(json), []) } else { return (nil, parseJSON(json)) } } .asDriver(onErrorJustReturn: (nil, [])) } ྫɿʁʁʁ ɾCPEZͷKTPO͸ɺ੒ޭͯͨ͠Βཉ͔ͬͨ͠஋
  19.  ੒ޭͨ͠Β஋͕ɺࣦഊͨ͠ΒΤϥʔ಺༰͕ཉ͍͠ func hoge(fuga: String) -> Driver<(Piyo?, [String])> { let

    params = ["fuga": fuga] return rx_responseJSON(.POST, endPoint: .Hoge, parameters: params) .map { success, json in if success { return (Piyo.parseJSON(json), []) } else { return (nil, parseJSON(json)) } } .asDriver(onErrorJustReturn: (nil, [])) } ྫɿʁʁʁ ɾCPEZͷKTPO͸ɺࣦഊͯͨ͠ΒΤϥʔ಺༰