LINEとLINE Creators Studio iOSアプリのサーバーAPI呼び出しアーキテクチャをリファクタリングした話 / Refactor API invocation archtecture in LINE & LINE Creators Studio

LINEとLINE Creators Studio iOSアプリのサーバーAPI呼び出しアーキテクチャをリファクタリングした話 / Refactor API invocation archtecture in LINE & LINE Creators Studio

2019/2/13に開催されたLINE Developer Meetup #50 in Fukuokaでの登壇資料です

53850955f15249a1a9dc49df6113e400?s=128

LINE Developers

February 13, 2019
Tweet

Transcript

  1. LINE Fukuoka ։ൃ1ࣨ ։ൃCνʔϜ ͸Δ;(@_ha1f) LINEͱLINE Creators Studio iOSΞϓϦͷ αʔόʔAPIݺͼग़͠

    ΞʔΩςΫνϟΛ ϦϑΝΫλϦϯάͨ͠࿩ 2019/2/13 LINE Developer Meetup #50 in Fukuoka
  2. ͸Δ; • iOS Developer of LINE Fukuoka (New graduate of

    2017) • LINE Creators Studio -> LINE SHOP part (October, 2018)
  3. Agenda • LINE Creators StudioͷLINEϩάΠϯप ΓΛSDKΞοϓσʔτͷ͍ͭͰʹ࢓૊Έ ͔Β୯७Խͯ͠ɺObservableͰָʹѻ͑ ΔΑ͏ʹͨ͠࿩ • LINE

    SHOPύʔτͷLINEελϯϓͷϦ ιʔε؅ཧΛେن໛Β͘͠ΑΓ੾Γ෼͚ Δ͜ͱͰ੔ཧͨ͠࿩
  4. Agenda • LINE Creators StudioͷLINEϩάΠϯप ΓΛSDKΞοϓσʔτͷ͍ͭͰʹ࢓૊Έ ͔Β୯७Խͯ͠ɺObservableͰָʹѻ͑ ΔΑ͏ʹͨ͠࿩ • LINE

    SHOPύʔτͷLINEελϯϓͷϦ ιʔε؅ཧΛେن໛Β͘͠ΑΓ੾Γ෼͚ Δ͜ͱͰ੔ཧͨ͠࿩
  5. LINE Creators Studio • 2017೥ϦϦʔεɺΊͬͪΌRxSwift • LINEϩάΠϯͰϢʔβʔΛ؅ཧʢLINE SDKΛར༻ʣ • LINE

    SDK v1 → v2΁ͷΞοϓσʔτͷඞཁɻɹɹɹɹɹɹɹɹɹ ͍ͭͰʹೝূɺτʔΫϯͷ؅ཧΛϦϑΝΫλϦϯάͨ͠
  6. OAuth ैདྷͷೝূʢInitialʣ LINE Auth Server Creators Studio Server ᶃCredential ᶄLINE

    token/refresh token ᶅLINE token/refresh token ᶆLINE Token ᶇvalid/invalid ᶈToken/refresh token • OAuthΛ༻͍ͨೝূ
  7. OAuth ैདྷͷೝূʢInitialʣ LINE Auth Server Creators Studio Server ᶃCredential ᶄLINE

    token/refresh token ᶅLINE token/refresh token ᶆLINE Token ᶇvalid/invalid ᶈToken/refresh token • OAuthΛ༻͍ͨೝূ
  8. OAuth ैདྷͷೝূʢInitialʣ LINE Auth Server Creators Studio Server ᶃCredential ᶄLINE

    token/refresh token ᶅLINE token/refresh token ᶆLINE Token ᶇvalid/invalid ᶈToken/refresh token • OAuthΛ༻͍ͨೝূ
  9. ैདྷͷೝূʢRefreshʣ LINE Auth Server Creators Studio Server ᶃrefresh token ᶃLINE

    refreshToken ᶄLINE token/refresh token ᶄToken/refresh token • LINEτʔΫϯΛCreators StudioαʔόʔͰอ࣋ͯ͠ɺແݶʹϦϑϨογϡͯ͠active֬ೝ • ΫϥΠΞϯτ͸Creators StudioαʔόʔͷτʔΫϯͷΈ؅ཧ
  10. LINE SDK v2΁ͷΞοϓσʔτ • ΫϥΠΞϯτଆͰRefresh token͕औಘෆՄʹͳͬͨʢAPIͷݺͼग़͠͸Ͱ͖Δʣ • αʔόʔͰ࣋ͯΔͷ͸ظݶͷ୹͍tokenͷΈ • ʢࣾ಺͔ͩΒSDKॻ͖׵͑Ε͹औΕΔ͚Ͳʣͦ΋ͦ΋αʔόʔ͚ͩͰϩάΠϯঢ়ଶ

    ΛӬଓԽͰ͖Δͷ͸໰୊ͳͷͰ͸ʁ →طଘͷ࢓૊ΈΛద༻ෆՄೳ
  11. ࢓૊ΈΛม͑Α͏ʂ

  12. ैདྷͷೝূͰͷRefresh໰୊ LINE Auth Server Creators Studio Server ᶃrefresh token ᶄLINE

    refreshToken ᶅLINE token/refresh token ᶆToken/refresh token • token͕ࣦޮ͍ͯͨ͠৔߹ʹ֬ೝ͢Δखஈ͕ͳ͍
  13. Ҋ LINE Auth Server Creators Studio Server ᶅrefresh token, LINE

    token ᶆLINE token ᶇvalid/invalid ᶈToken/refresh token • ClientͰrefreshͯ͠ɺLINE tokenΛ΋͏ҰճૹΕ͹͍͍ʁ ᶄLINE token/refresh token ᶃLINE refresh token
  14. Ҋ LINE Auth Server Creators Studio Server ᶅrefresh token, LINE

    token ᶆLINE token ᶇvalid/invalid ᶈToken/refresh token • ϩάΠϯͱಉ͡ʂ ᶄLINE token/refresh token ᶃLINE refresh token
  15. Ҋ LINE Auth Server Creators Studio Server ᶅLINE token ᶆLINE

    token ᶇvalid/invalid ᶈToken • Refresh tokenͦ΋ͦ΋͍Βͳ͍ʂʁʂʁ • ΋͏ҰճϩάΠϯ͢Ε͹͍͍ʂ ᶄLINE token/refresh token ᶃLINE refresh token
  16. ৽͍͠ೝূ LINE Auth Server Creators Studio Server ᶄLINE token/refresh token

    ᶅLINE token ᶆLINE Token ᶈToken • ಠࣗͷϦϑϨογϡͷ࢓૊ΈΛͳͨ͘͠ • ৽͍͠LINEͷτʔΫϯͰ࠶ϩάΠϯ͢ΔΠϝʔδ ᶃCredential or LINE refresh token ᶇvalid/invalid
  17. ݁Ռʁ • ΤϯυϙΠϯτ͕ݮͬͯ࢓૊Έ͕୯७Խʂ • Ͱ΋ɺ͍͍͜ͱ͚ͩʁ

  18. ৽ͨͳ໰୊ ΫϥΠΞϯτͷ ঢ়ଶ؅ཧ

  19. 401ͳΒࣗಈͰ ϦϑϨογϡͯ͠ ϦτϥΠ

  20. Observable<T> • ඇಉظॲཧͷϑϨʔϜϫʔΫɺReactiveXͷجૅ֓೦ • onNext, onCompleted, onErrorͷࡾछྨͷΠϕϯτΛൃੜͤ͞Δ • ৭ʑͳΠϕϯτΛObservable<T>ͷܗͰද͢͜ͱ͕Ͱ͖Δ Subscribe

    Observable<T> onNext onCompleted onError Observer
  21. Observable<Response> • ௨৴͢Δͱ͖ͷObservable Subscribe Caller onNext(response) onCompleted Observable<Response> ϦΫΤετ Ϩεϙϯε

  22. Observable<Data> • σίʔυΛڬΜͰ΋ΠϯλʔϑΣʔε͸ಉ͡ Subscribe Caller Observable<Response> ϦΫΤετ Ϩεϙϯε σίʔυ onNext(response)

    onCompleted
  23. Observable<Response> • λΠϜΞ΢τΤϥʔ Subscribe Caller onError Observable<Response> ϦΫΤετ λΠϜΞ΢τ Τϥʔ

    ܭଌ։࢝
  24. Observable<Data> • Τϥʔ͕ҧͬͯ΋ΠϯλʔϑΣʔε͸ಉ͡ Subscribe Caller Observable<Response> ϦΫΤετ Ϩεϙϯε σίʔυ onError

    σίʔυΤϥʔ
  25. Observable<Token> • ϩάΠϯΛObservableͱͯ͠ଊ͑Δͱ Subscribe Observable<Token> ϦΫΤετ Ϩεϙϯε ϩάΠϯը໘ Cancel Done

    Caller onNext(token) onCompleted
  26. Observable<Token> • ϩάΠϯΩϟϯηϧ Subscribe Observable<Token> ϩάΠϯը໘ Cancel Done Caller onError

  27. Observable<Token> • ·ͱΊΔͱ͜Μͳײ͡ Subscribe Observable<Token> ϦΫΤετ Ϩεϙϯε ϩάΠϯը໘ Cancel Done

    Caller onNext(token) onCompleted onError
  28. Observable<Data> • ·ͱΊΔͱ͜Μͳײ͡ Subscribe Caller Observable<Data> ϦΫΤετ Ϩεϙϯε σίʔυ onError

    σίʔυΤϥʔ λΠϜΞ΢τΤϥʔ onNext(response) onCompleted
  29. Observable<Result> • ෳࡶͳϩδοΫΛӅṭͰ͖Δ onNext onError Subscribe

  30. Observable<AccessToken> • AccessTokenࣗମ΋Observableͱͯ͠औಘ͢Δ onNext onError Subscribe

  31. Observable<LineToken> • ը໘ભҠ΋ؚΊͯObservable onNext onError Subscribe

  32. લ൒ͷ·ͱΊ • refreshͷΤϯυϙΠϯτ͕ෆཁʹͳΓɺlogin͚ͩʹ·ͱ·ͬͨ • ෳࡶͳঢ়ଶ΋ObservableͰஈ֊తʹԾ૝Խͯ͠ɺετϨεͳ͘௨৴Ͱ͖ΔΑ͏ʹ ͳͬͨ

  33. ͓·͚ɿshare()๨ΕDDoSࣄނʹ஫ҙ • ಉ࣌ʹϦΫΤετ͕ࣦഊ͢ΔͱɺϦϑϨογϡͷແݶϧʔϓͷՄೳੑɾɾɾ ᶃෳ਺ͷ401 ᶄෳ਺ͷSubscribe ᶆτʔΫϯࣦޮʹΑΔࣦഊ ᶅෳ਺ͷrefreshʢલͷτʔΫϯͷinvalidateʣ ᶇ࠶ϦτϥΠʹΑΔsubscribe

  34. Agenda • LINE Creators StudioͷLINEϩάΠϯप ΓΛSDKΞοϓσʔτͷ͍ͭͰʹ࢓૊Έ ͔Β୯७Խͯ͠ɺObservableͰָʹѻ͑ ΔΑ͏ʹͨ͠࿩ • LINE

    SHOPύʔτͷLINEελϯϓͷϦ ιʔε؅ཧΛେن໛Β͘͠ΑΓ੾Γ෼͚ Δ͜ͱͰ੔ཧͨ͠࿩
  35. What is LINE SHOP Part? LINE(Messenger) • ػೳɾλεΫ͝ͱʹ෼୲ɺͦΕͧΕͰϨϏϡʔ LIFF Album

    SHOP LINE Pay Chat Registration LINE Beacon Timeline LINE News
  36. What is LINE SHOP Part? LINE(Messenger) • ػೳɾλεΫ͝ͱʹ෼୲ɺͦΕͧΕͰϨϏϡʔ LIFF Album

    SHOP LINE Pay Chat Registration LINE Beacon Timeline LINE NEWS
  37. What is LINE SHOP Part? LINE SHOP Part • ελϯϓɾֆจࣈɾணͤସ͑ؔ࿈ͷػೳɾը໘Λ୲౰

    • γϣοϓը໘ͱ͔ɺελϯϓ؅ཧը໘ͱ͔ɺΩʔϘʔυͱ͔ɺελ ϯϓͷϑΣονɾΩϟογϡͷػೳͱ͔ • ϑΣονͱ͔ͷ࿩Λ͠·͢
  38. Sticker Fetcher • Caller͸TalkRoomͱ͔Keyboardͱ͔ • ϩʔΧϧʹͳ͚Ε͹αʔόʔ͔Βऔಘ • Ұ౓μ΢ϯϩʔυͨ͠ΒϩʔΧϧʹΩϟογϡ

  39. ैདྷͷ࣮૷ • APIΫϥε࡞ͬͯ෼཭ • FileManagerΫϥε࡞ͬͯ෼཭

  40. LGTM!

  41. But…

  42. func imageName() -> String func stickerUrl() -> URL func imageUrl()

    -> URL func fetchSound() -> URL func localSticker() -> UIImage func fetchSticker() -> UIImage func fetchPopupStatic() -> UIImage func localUrl() -> URL func stickerName() -> String … Sticker Fetcher was…
  43. func imageName() -> String func stickerUrl() -> URL func imageUrl()

    -> URL func fetchSound() -> URL func localSticker() -> UIImage func fetchSticker() -> UIImage func fetchPopupStatic() -> UIImage func localUrl() -> URL func stickerName() -> String … Sticker Fetcher was… !
  44. ͳͥෳࡶ͔ • ελϯϓࣗମͷछྨͷଟ͞ • ը૾ͷछྨͷଟ͞ • ύοέʔδͱελϯϓ • ΞχϝʔγϣϯɺϙοϓΞοϓɺα΢ϯυɺΞχϝʔγϣϯα΢ϯυɺ… •

    छྨʹΑΓඞཁͳϦιʔε͕ҟͳΔ • τʔΫɺγϣοϓৄࡉɺγϣοϓҰཡɺΩʔϘʔυɺΩʔϘʔυλϒɺ2x… • ͦΕͧΕͷ໊લɾঢ়ଶΛ؅ཧ͢Δඞཁ • ύοέʔδ͸ணͤସ͑ɾֆจࣈڞ௨ɺελϯϓ͸ผ
  45. ৽͍࣮͠૷ • ໊લ؅ཧɾURL؅ཧΛ෼ׂ • ϗϯτ͸Package Image Fetcher΋࡞Ζ͏ͱ͕ͨ͠ɺ ໋໊Ͱे෼Θ͔Γ΍͘͢ͳͬͨ

  46. struct RemoteUrl { static var baseURL: URL static func stickerImageName()

    static func stickerImageUrl() static func stickerSoundFileUrl() static func packageImageName() static func packageImageUrl() } Remote URL • ҙࣝͯ͠PackageͱStickerΛ۠ผ
  47. Α͔ͬͨ͜ͱ • ΊͬͪΌΘ͔Γ΍͘͢ͳͬͨ • Ϣχοτςετ͕ॻ͖΍͘͢ͳͬͨ

  48. ޙ൒ͷ·ͱΊ • ن໛ʹ߹ΘͤͨΞʔΩςΫνϟ͕ඞཁ • ৑௕͗͢Δ͙Β͍͕ͪΐ͏Ͳ͍͍͜ͱ΋

  49. THANK YOU

  50. Observableͷৄࡉ https://qiita.com/_ha1f/items/e16ddc6017c4ad807c3c