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

Open APIやSPA構成のAPI設計 / openapi-api-design

Open APIやSPA構成のAPI設計 / openapi-api-design

Gotanda.rb#44 ”API/JSONについて”@オンライン

A92d05c5da067dc28a32527430f762ca?s=128

WalkerSumida

July 22, 2020
Tweet

Transcript

  1. 0QFO"1*΍41"ߏ੒ ͷ"1*ઃܭ ىۀ४උத "ZVNV4VNJEB

  2. ࣗݾ঺հ

  3. w େֶߦ͖ͳ͕Β*5ϕϯνϟʔاۀͰ໿೥ؒ༗ঈΠϯλʔ ϯ w 1)1 .Z42- $BLF1)1 Y 8PSEQSFTT $

    "41 /&5 w ౦ࣳ৘ใγεςϜגࣜձࣾͰ೥ؒϔϧεέΞࣄۀʹैࣄ w 7#/&5 $ 0SBDMF%BUBCBTF w ̍ਓͰىۀ͠Α͏ͱࢼΈΔ͕ࣦഊ w 3VCZPO3BJMT 'VFM1)1 .Z42- "84 "OTJCMF w גࣜձࣾϚπϦΧʹΤϯδχΞೋਓ໨ͷ૑ۀϝϯόʔͱ͠ ͯ+PJO w /PUJBࣄۀ੹೚ऀ݉4FOTFTϓϩμΫτϚωʔδϟʔ w 3VCZPO3BJMT "OHVMBS+4 .Z42- "84 w ىۀ४උத ܦྺ
  4. 0QFO"1*΍41"ߏ੒ ͷ"1*ઃܭ

  5. <0QOF"1* "1*> ιʔτपΓ

  6. w ߋ৽೔Λ߱ॱͰऔಘ w PSEFSVQEBUFE@BU PSEFS@BTD@EFTDEFTD ͋ͱ͋ͱࠔΔύλʔϯ

  7. ߲໨ຖʹιʔτํ޲Λࢦఆ͠ ͍ͨ

  8. w ߋ৽೔Λ߱ॱͰಉ͡೔࣌ͷ৔߹͸ JEͷঢॱͰऔಘ w PSEFSVQEBUFE@BU PSEFS@BTD@EFTDEFTD w Ͳ͏͢Δʜ PSEFSͰ΋࡞Δ ͋ͱ͋ͱࠔΔύλʔϯ

  9. PSEFSͱ͍͏ιʔτ߲໨Λࢦఆ͢Δͱ ͜ΖͰঢॱ߱ॱ΋ಉ࣌ʹࢦఆ͢Δ

  10. PSEFSVQEBUFE@BU JE ߱ॱ ঢॱ

  11.   Λলུͨ͠৔߹͸ঢॱ ͱ͢Δ PSEFSVQEBUFE@BU JE

  12. ݸਓతʹ͸TPSUͷ΄͏͕޷͖ TPSUVQEBUFE@BU JE

  13. 0QFO"1*ʹ͸ඞਢͰ͔ͭɺ41"ߏ੒ ͷ"1*ʹ΋ରԠ͓͍ͯͨ͠΄͏͕͍͍

  14. <"1*> ϖʔδઐ༻ͷ"1*͸ͭ͘Βͳ ͍΄͏͕͍͍

  15. w ಉ͡ϦιʔεͰ΋ϖʔδຖʹ +40/ͷߏ੒Λม͍͑ͨ w /PUJPOͷΑ͏ʹදࣔΛ5BCMFܗࣜ ΍#PBSEܗࣜɺ-JTUܗࣜʹ੾Γସ ͑Δ৔߹ͳͲ ϖʔδઐ༻ͷ"1*͸ͭ͘Βͳ͍΄͏͕͍͍

  16. ಉ͡ϦιʔεͰ΋ϖʔδຖʹ +40/ͷߏ੒Λม͍͑ͨ

  17. Θ͔Δ͜ͱ͸෼͔Δɻͦ͏͢ΔͱϑϩϯτͰ ͷՃ޻͕ݮΔͷͰָͳͱ͖͋Γ·͢ΑͶɻ

  18. Ͱ΋ɺ΍Γ࢝ΊΔͱίϯτϩʔ ϥ͕ಡΈʹͯͨ͘͘·Βͳ͍ʜ

  19. class ItemController < ActionController::API def index return json_for_table if params[:page_type]

    == 'table' return json_for_board if params[:page_type] == 'board' return json_for_list if params[:page_type] == 'list' @items = Item.where(...).order(...) end end
  20. ͔ͩΒͱ͍ͬͯ

  21. class ItemController < ActionController::API def index @items = Item.where(...).order(...) end

    def index_for_table ... end def index_for_board ... end def index_for_list ... end end
  22. ͱ͢Δͱ ϧʔςΟϯά͕ݟʹ͘͘ͳΔ

  23. GET /items GET /items/index_for_table GET /items/index_for_board GET /items/index_for_list ͋·Γ៉ྷͰͳ͍ͷͱɺItemController͕݁ہංେԽ͢Δ

  24. ύϑΥʔϚϯε্΍ͦͷଞͷཧ༝ͰͲ͏ ͯ͠΋ͱ͍͏ͳΒɺͤΊͯ͜Μͳײ͡ʹ

  25. class ItemController < ActionController::API def index @items = Item.where(...).order(...) end

    end class ItemForTableController < ActionController::API def index ... end end class ItemForBoardController < ActionController::API def index ... end end class ItemForListController < ActionController::API def index ... end end
  26. GET /items GET /items_for_table GET /items_for_board GET /items_for_list ϧʔςΟϯάతʹ͸ऩ·Γ͕͍͍ͱ͍͑͹͍͍ͷͱɺ ControllerͷංେԽΛճආͰ͖Δ

  27. w ಺෦ͷ"1*΋0QOF"1*΋ڞ௨Ͱ͖ Δ෦෼͸͋ΔͷͰɺڞ௨Խ͢Δͨ ΊͷHFN w IUUQTHJUIVCDPN XBMLFSTVNJEBTFOSP "1*ͷ࢓༷Λڞ௨Խ͢ΔͨΊͷHFN

  28. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ