Web APIについての雑談

Web APIについての雑談

よく悩むところを列挙しました

Fb69c72d5af289d6674d2659246a5291?s=128

Tomohiro Nishimura

December 23, 2015
Tweet

Transcript

  1. Web API ʹ͍ͭͯͷࡶஊ ؔϞό #9

  2. ג ͸ͯͳ 8FCΞϓϦέʔγϣϯΤϯδχΞ εϚʔτϑΥϯΞϓϦΤϯδχΞ ͲΕ΋த్൒୺ͰযΔ ޚࡏॴַΑ͔ͬͨ id:Sixeight (@tomohi_ro)

  3. Web API ΢ΣϒΤʔϐʔΞΠ

  4. ؔϞό#9 Web API ͱ͸ͳΜͧ΍ )551ϓϩτίϧΛར༻ͯ͠ωοτϫʔ Ϋӽ͠ʹݺͼग़͢"1* 8FC"1*5IF(PPE1BSUT

  5. εϚʔτϑΥϯΞϓϦ ͱ Web API

  6. ౴͑͸ͳ͍

  7. ؔϞό#9 LSUDs ͱSSKDs ▸ LSUDs (large set of unknown developers)

    ▸ ޿͘Ұൠతʹެ։͞Ε͍ͯΔ ▸ Twitter/Facebook/Instagram/ Google/etc ▸ SSKDs (small set of known developers) ▸ ݶΒΕͨൣғʹެ։͞Ε͍ͯΔ ▸ ࣗࣾΞϓϦͳͲ
  8. 44,%T গ਺ͷ஌͍ͬͯΔ։ൃऀ޲͚

  9. ؔϞό#9 SSKDs (small set of known developers) ▸ ૝ఆ͞Εͨ࢖͍ํ͔͠͞Εͳ͍ ▸

    ΞϓϦʹ࠷దԽͨ͠ܗͰఏڙͰ͖Δ ▸ ։ൃऀಉ࢜Ͱձ࿩Ͱ͖Δ ▸ ີ݁߹͕ͪ͠ ▸ ଥڠ͕ͪ͠
  10. ίʔϧ 1εΫϦʔϯɺ

  11. ؔϞό#9 1εΫϦʔϯɺ1ίʔϧ ▸ 1ը໘Λදࣔ͢ΔͷʹɺWeb APIͷݺͼग़͠͸Ұճʹ͓͍͑ͨ͞ ▸ جຊతʹ͸ෳ਺ͷϦιʔε͕ඞཁ ▸ αʔόʔଆͷ࣮૷͸ෳࡶʹͳΔ ▸

    ͲΜͲΜRESTͷߟ͑ํ͔ΒͦΕΔ ▸ ஫ҙਂ͘ઃܭ͢Ε͹Մೳ ▸ ը໘Λத৺ʹߟ͑͗͢Δͱมߋʹऑ͘ͳΔ
  12. ΤϯυϙΠϯτ APIͷإ

  13. ؔϞό#9 ΤϯυϙΠϯτ ▸ SSKDsͩͱΤϯυϙΠϯτͳΜͰ΋͍͍ͱ͍͏࿩΋ ▸ ϝϯςφϯεੑ ▸ ݟͨ໨ѱ͍ͱ΍ΔؾʹӨڹ͢Δ ▸ ෳ਺ܥͳͷ͔୯਺ܗͳͷ͔

    ▸ HATEOASΈ͍ͨͳͷ΋ਅ໘໨ʹߟ͑ͯ΋ྑͦ͞͏
  14. 3&45 Ԟ͞Μʹઆ໌͠ΖͱݴΘΕͯ΋೉͍͠ see also: http://www.geocities.jp/yamamotoyohei/rest/rest-to-my-wife.htm

  15. ؔϞό#9 REST ▸ ͜͜Ͱ͸ϑΟʔϧσΟϯάͬΆ͍࿩ ▸ 1εΫϦʔϯɺ1ίʔϧͱ૬ੑѱ͍ؾ͕͢Δ ▸ ϩάΞ΢τ͸ DELETE ͳͷ͔Ͳ͏͔

    ▸ λΠτϧͷߋ৽͸ PATCHʁʁʁʁʁʁ ▸ ૢ࡞͕ႈ౳ͳΒ PUT/DELETEɺͦ͏͡Όͳ͚Ε͹POST
  16. Ϩεϙϯε ҙ֎ͱ໎͏

  17. ؔϞό#9 Ϩεϙϯε ▸ ϦιʔεΛߋ৽ͨ͠ͱ͖ͷϨεϙϯε͸ɺߋ৽લʁͦΕͱ΋ߋ৽ޙʁ ▸ ϦιʔεΛফͨ͠ͱ͖͸204? ▸ 200͚ͩΕͲϨεϙϯεཁΒͳ͍ͱ͖͸ʁ ▸ {“success”:

    true} vs {} vs ۭจࣈྻ (←͜Ε͸ͳ͍) ▸ ഑ྻΛฦ͢ͱ͖ʹ΋ΦϒδΣΫτʹ͢Δ͔Ͳ͏͔ ▸ ͜Ε͸ηΩϡϦςΟͷ໰୊͕͋ΔͷͰΦϒδΣΫτͰ ▸ ඞཁͳཁૉ͚ͩʁ֦ுੑΛߟ͑ͯଟΊʹ΋Β͓ͬͯ͘ʁ ▸ {“user”: { id: 1, name: “hoge”}} vs {“user_id”: 1, “user_name”: “hoge”} ▸ ೔෇ͷϑΥʔϚοτͲ͏͢Δͷ͔ ▸ ϖʔδϯάͲ͏͢Δͷ͔ ▸ JSONͳͷ͔ଞͷϑΥʔϚοτͳͷ͔
  18. ΤϥʔϨεϙϯε ࣮͸ॏཁ

  19. ؔϞό#9 ΤϥʔϨεϙϯε ▸ ϑΥʔϚοτ͸ݻఆ͍ͨ͠ ▸ ϝοηʔδΛ;͘ΊΔ vs ΞϓϦͰੜ੒ ▸ ଟݴޠԽͲ͏͢Δ͔ɺߋ৽͠΍͢͞͸Ͳ͏͔

    ▸ ΞϓϦଆͰΤϥʔʹର͢ΔέΞ͕ͻͭΑ͏͕Ͳ͏͔ ▸ Τϥʔίʔυඞཁͳͷ͔ ▸ Ͳ͜·Ͱ৘ใ͕͍Δͷ͔
  20. ೝূ ࣗ෼Ͱ͋Δ͜ͱͷূ໌

  21. ؔϞό#9 ೝূ ▸ APIτʔΫϯΈ͍ͨͳͷ༻ҙ͢Δ ▸ Expireͱ͔ϦϑϨογϡͷ࢓૊Έ͕͍Δ ▸ ΋ΕͨΒࠔΔ ▸ ܦ࿏͕҉߸Խ͞Ε͍ͯͨΒ·͍͍͋

    ▸ OAuth 2.0 ▸ ͏·͘΍Δํ๏͸͋Γͦ͏͚ͩΕͲ஌ݟ͸ͳ͠
  22. ҉߸Խ ೉ಡԽ Ӆ͍ͨ͠ͷͰ

  23. ؔϞό#9 ҉߸Խ,೉ಡԽ ▸ Ӆ͍ͨ͠৘ใΛӅ͢ ▸ ୈࡾऀʹରͯ͠(ݸਓ৘ใ)ɺ࢖༻ऀʹରͯ͠ (ήʔϜͱ͔) ▸ HTTPSͩͱ͍͍ͩͨ҆৺ ▸

    ಺༰Λ೉ಡԽͯ͠όΠφϦͰૹΔͱ͔ ▸ ΞϓϦଆͰσίʔυ͢Δͷେม໰୊ ▸ σίϯύΠϧ͞Εͨͱ͖ͷରࡦ ▸ ૬ख͸CIA͡Όͳͯ͘ૉਓ
  24. όʔδϣϯ ޙํޓ׵ͷཁ

  25. ؔϞό#9 όʔδϣϯ ▸ ޙํޓ׵ੑΛอ͍ͪͨ ▸ όʔδϣϯΛࢦఆ͢Δ͜ͱͰϨεϙϯεΛฦ͠Θ͚Δ ▸ /v1/hoge/piyo, ?version=1, X-API-Version:

    1, application/ vnd.example.v1+json ▸ Ͳ͏΍ͬͯ؅ཧ͢Δ͔ ▸ ผͷϑΝΠϧͰ؅ཧ͢Δ ▸ ifจͰ෼ذ͢Δ ▸ ݹ͍όʔδϣϯΛ͍ͭഇࢭ͢Δ͔
  26. ඇޓ׵ͳมߋ ආ͚ΒΕͳ͍

  27. ؔϞό#9 ඇޓ׵ͳมߋ ▸ ਃ੥Ͳ͏͢Δͷ͔ ▸ ਃ੥༻ͷαʔόʔΛ༻ҙͯͦͪ͠Βʹ޲͚Δ ▸ ΤϯυϙΠϯτΛฦ͢API (HATEOASʹͭͳ͕Δ) ▸

    ਃ੥࣌ͷΈຊ൪ͱผͷ΋ͷݟͤΔͷͲ͏ͳͷ͔ ▸ ͪͳΈʹ͸ͯͳͰ͸΍ͬͯͳ͍ ▸ ڧ੍όʔδϣϯΞοϓ ▸ ΞϓϦͷόʔδϣϯΛ্͛ͳ͍ͱ࢖͑ͳ͘͢Δ
  28. ࿩୊ ͦͷଞͷ

  29. ؔϞό#9 ࿩୊ ▸ υΩϡϝϯτ ▸ ςετ ▸ ΦʔέεϨʔγϣϯ૚ ▸ ཉ͍͠஋͚ͩ΋Β͏ͱ͔

    ▸ ࣗಈੜ੒ ▸ Swaggerͱ͔ ▸ ແݶʹ͋Δ
  30. ·ͱΊ ͦͯ͠

  31. ؔϞό#9 ·ͱΊ ▸ ౴͑͸ͳ͍ ▸ ϓϩδΣΫτຖʹߟ͑Δඞཁ͕͋Δ ▸ ͱʹ͔͘໎͏ ▸ ձ࿩͠·͠ΐ͏

  32. ϝϦʔΫϦε Ϛε