Web API 講座 in Treasure2017

Web API 講座 in Treasure2017

2017/08/17

VOYAGE GROUPにて開催されているエンジニア志望学生向けインターンシップTreasure https://voyagegroup.com/internship/treasure/ にてWeb API設計入門の話をした際のスライドです。

F547a629b5f3a3d0dad8cc96810eeb5c?s=128

Go Nakanishi

August 17, 2017
Tweet

Transcript

  1. Web API ߨ࠲ @gomachan46 Treasure2017

  2. ࣗݾ঺հ ͳ͔ʹ͠͝͏ (@gomachan46) VOYAGE MARKETING Ruby, PHP, Golang, … Treasure2012ࢀՃ

  3. ͜Θ͘ͳ͍Symfony2 - ۀ຿Ͱ࢖ͬͯ ؾ͍ͮͨ7ͭͷྑ͍ͱ͜Ζ ࣾ಺޲͚χϡʔεఏڙAPIΛS3+API GatewayͰ αʔόʔϨεʹαΫͬͱߏஙͯ͠Έͨ

  4. Իήʔ

  5. ͋ͳͨʹ͓͢͢Ί͢ΔͨͬͨҰͭͷ࠷ߴͷΩʔϘʔυ λʔϛφϧͰ༡΂ΔԻήʔ࡞ͬͯΈͨ ͋ͳͨʹ͓͢͢Ί͢ΔWebSocketΛ༻͍ͨશ͘৽͍͠ OAuthͷ͝঺հ

  6. ΢ΥʔϛϯάΞοϓ ͋͞ͷ͡ΎΜͼ͏ΜͲ͏

  7. 7ਓνʔϜΛ૊ΜͰ͍ͩ͘͞ ͓୊Λઌ಄ͷਓʹ͚͓ͩ఻͑͠·͢ ࣋ͪ࣌ؒ ඳ࣌ؒ͘: 30ඵ લͷਓͷֆΛݟΔ࣌ؒ: 10ඵ ͓ֆ͔͖఻ݴήʔϜ

  8. ࢖͏͚ͩͰͳ͘ ࢖͍΍͍͢APIΛ ఏڙͰ͖ΔΤϯδχΞʹͳΔ ࠓ೔ͷΰʔϧ

  9. Web WebΞϓϦέʔγϣϯ Web API Web APIઃܭ ͓͠ͳ͕͖

  10. Web

  11. Web is … ? WebͷਤΛඳ͍ͯΈ·͠ΐ͏

  12. –Wikipediaઌੜ World Wide Webͱ͸ɺ Πϯλʔωοτ্Ͱఏڙ͞ΕΔ ϋΠύʔςΩετγεςϜͰ͋Δɻ

  13. None
  14. –Wikipediaઌੜ ϋΠύʔςΩετͱ͸ɺ Πϯλʔωοτ্ʹࢄࡏ͢Δ υΩϡϝϯτಉ࢜Λ૬ޓʹࢀরՄೳʹ͢Δ γεςϜͰ͋Δɻ

  15. None
  16. None
  17. None
  18. None
  19. υΩϡϝϯτಉ࢜Λ ΫϞͷ૥ͷΑ͏ʹ ϦϯΫͰ݁ͼ͚ͭΔγεςϜ Web is … ?

  20. WebΞϓϦέʔγϣϯ

  21. None
  22. αʔόαΠυॏࢹ suzuken/wikiελΠϧ ΫϥΠΞϯταΠυॏࢹ suzuken/go-todoελΠϧ ΫϥΠΞϯταΠυͱαʔόαΠυ

  23. αʔόαΠυॏࢹ ਤΛඳ͍ͯΈ·͠ΐ͏

  24. None
  25. αʔόαΠυ͸HTMLΛ
 ಈతʹ࡞੒ ΫϥΠΞϯταΠυ͸HTMLΛ
 ϨϯμϦϯά͢Δ͚ͩ αʔόαΠυॏࢹ

  26. None
  27. ΫϥΠΞϯταΠυॏࢹ ਤΛඳ͍ͯΈ·͠ΐ͏

  28. None
  29. ΫϥΠΞϯταΠυ͸HTMLΛ
 ಈతʹ࡞੒ αʔόαΠυ͸HTML࡞੒ʹඞ ཁͳJSONΛฦ͚ͩ͢ ΫϥΠΞϯταΠυॏࢹ

  30. None
  31. Webͱ͸Կ͔͕Θ͔Δ WebΞϓϦέʔγϣϯͷྲྀΕ͕Θ͔Δ ΫϥΠΞϯταΠυͱαʔόαΠυͷ ΍ΓऔΓ͕Θ͔Δ Web APIΛֶͿԼ஍͕Ͱ͖Δ ͜͜·Ͱͷ·ͱΊ

  32. Web API

  33. Web API is … ? Web APIͷਤΛඳ͍ͯΈ·͠ΐ͏

  34. API is … ? Wikipediaઌੜʹฉ͍ͯΈ·͠ΐ͏

  35. –Wikipediaઌੜ APIͱ͸ɺ ιϑτ΢ΣΞίϯϙʔωϯτ͕ ޓ͍ʹ΍ΓͱΓ͢Δͷʹ࢖༻͢Δ ΠϯλϑΣʔεͷ࢓༷Ͱ͋Δɻ

  36. None
  37. –Wikipediaઌੜ ͭ·Γɺ؆୯ʹ͍͑͹ɺ ΞϓϦέʔγϣϯΛϓϩάϥϜ͢Δʹ͋ͨͬͯɺ ϓϩάϥϜͷखؒΛলͨ͘Ίɺ ΋ͬͱ؆ܿʹϓϩάϥϜͰ͖ΔΑ͏ʹ ઃఆ͞ΕͨΠϯλʔϑΣʔεͷࣄͰ͋Δɻ

  38. None
  39. None
  40. Webܦ༝Ͱ ར༻Ͱ͖ΔAPI Web API is … ?

  41. None
  42. None
  43. None
  44. APIͱ͸Կ͔͕Θ͔Δ Web APIͱ͸Կ͔͕Θ͔Δ Web APIͷ࢖ΘΕΔྲྀΕ͕Θ͔Δ Web APIઃܭΛֶͿԼ஍͕Ͱ͖Δ ͜͜·Ͱͷ·ͱΊ

  45. Web APIઃܭ

  46. URL HTTP JSON Web APIઃܭ

  47. URL Uniform Resource Locator

  48. None
  49. Web্ͷ৘ใ(Ϧιʔε)Λ Ұҙʹࢦࣔ͢͠

  50. None
  51. ಈࢺ͸࢖Θ໊ͣࢺΛ༻͍Δ URL͸ϦιʔεΛࢦࣔ͢͠ࣄʹઐ೦ ϦʔμϒϧURLΛҙࣝ͢Δ ౷ҰײΛ࣋ͨͤΔ URL

  52. /api/articles هࣄ৘ใҰཡ /api/articles/1 ID:1ͷهࣄ৘ใ e.g. هࣄ৘ใΛѻ͏API

  53. ࣍ͷػೳΛఏڙ͢ΔAPIͷURLΛߟ͍͑ͯͩ͘͞ هࣄ৘ใҰཡΛऔಘ͢Δ: /api/articles ID:1ͷهࣄ৘ใΛऔಘ͢Δ: /api/articles/1 هࣄ৘ใΛ࡞੒͢Δ: ??? ID:1ͷهࣄ৘ใΛߋ৽͢Δ: ??? ID:1ͷهࣄ৘ใΛ࡟আ͢Δ:

    ??? ԋश: URL
  54. هࣄ৘ใҰཡΛऔಘ͢Δ: /api/articles ID:1ͷهࣄ৘ใΛऔಘ͢Δ: /api/articles/1 هࣄ৘ใΛ࡞੒͢Δ: /api/articles ID:1ͷهࣄ৘ใΛߋ৽͢Δ: /api/articles/1 ID:1ͷهࣄ৘ใΛ࡟আ͢Δ: /api/articles/1

    ԋश: URL
  55. ΫΤϦετϦϯά Query String

  56. None
  57. ?Ҏલ͸Ϧιʔεͷҙຯ ?Ҏ߱͸ΫϥΠΞϯτͷҙࢥ ?Ҏ߱͸ແͯ͘΋ಈ͘ ΫΤϦετϦϯά

  58. /api/articles?page=2 Ϧιʔεͷҙຯ هࣄ৘ใҰཡ ΫϥΠΞϯτͷҙࢥ 2ϖʔδ໨Λ͍ͩ͘͞ e.g. هࣄ৘ใΛѻ͏API

  59. ࠷దͳURLΛͦΕͧΕબ୒͍ͯͩ͘͠͞ ݕࡧͰ͖ΔAPIΛ࡞Γ͍ͨ৔߹ /api/articles/search/suzuken /api/articles/search?q=suzuken ͋ΔهࣄͷίϝϯτΛऔಘ͢ΔAPIΛ࡞Γ͍ͨ৔߹ /api/articles/1/comments /api/comments?article=1 ԋश: ΫΤϦετϦϯά

  60. HTTP HyperText Transfer Protocol

  61. None
  62. εςʔτϨε HTTPϝιου εςʔλείʔυ HTTPϔομ HTTP

  63. εςʔτϨε

  64. εςʔτϨε is … ? ঢ়ଶ͕ͳ͍ #ͱ͸

  65. * νΩνʔηοτ * νΩνʔ * αΠυϝχϡʔ * υϦϯΫ νΩνʔηοτΛ஫จ͍ͯͩ͘͠͞ ʁ

    ʁ ʁ
  66. None
  67. ΫοΩʔ

  68. None
  69. ηογϣϯ

  70. None
  71. HTTPϝιου

  72. Ϧιʔεʹର͢Δ ॲཧΛද͢

  73. GET/POST/PUT/DELETE ͜Ε͚ͩʂ

  74. Ϧιʔε(URL)ʹରͯ͠ԿΛ͢Δ͔ 4छྨͷத͔ΒબͿ͚ͩ બͿͨΊʹͪΌΜͱϝιουͷ ࣋ͭҙຯɾಛ௃Λཧղ͢Δ HTTPϝιου

  75. ϝιου ҙຯ Կճ΍ͬͯ΋ ݁Ռ͕ಉ͔͡ ϦιʔεΛ มߋ͠ͳ͍͔ (&5 औಘ ˓ ˓

    1045 ࡞੒ ☓ ☓ 165 ߋ৽ ˓ ☓ %&-&5& ࡟আ ˓ ☓
  76. هࣄ৘ใҰཡΛऔಘ͢Δ: /api/articles ID:1ͷهࣄ৘ใΛऔಘ͢Δ: /api/articles/1 هࣄ৘ใΛ࡞੒͢Δ: /api/articles ID:1ͷهࣄ৘ใΛߋ৽͢Δ: /api/articles/1 ID:1ͷهࣄ৘ใΛ࡟আ͢Δ: /api/articles/1

    ԋश: URL
  77. ࣍ͷػೳΛఏڙ͢ΔAPIͷHTTPϝιουΛߟ͍͑ͯͩ͘͞ هࣄ৘ใҰཡΛऔಘ͢Δ: ??? /api/articles ID:1ͷهࣄ৘ใΛऔಘ͢Δ: ??? /api/articles/1 هࣄ৘ใΛ࡞੒͢Δ: ??? /api/articles

    ID:1ͷهࣄ৘ใΛߋ৽͢Δ: ??? /api/articles/1 ID:1ͷهࣄ৘ใΛ࡟আ͢Δ: ??? /api/articles/1 ԋश: HTTPϝιου
  78. هࣄ৘ใҰཡΛऔಘ͢Δ: GET /api/articles ID:1ͷهࣄ৘ใΛऔಘ͢Δ: GET /api/articles/1 هࣄ৘ใΛ࡞੒͢Δ: POST /api/articles ID:1ͷهࣄ৘ใΛߋ৽͢Δ:

    PUT /api/articles/1 ID:1ͷهࣄ৘ใΛ࡟আ͢Δ: DELETE /api/articles/1 ԋश: HTTPϝιου
  79. εςʔλείʔυ

  80. εςʔτϨε HTTPϝιου εςʔλείʔυ HTTPϔομ HTTP

  81. APIͷ݁ՌΛ ୺తʹද͢

  82. 2XX͸੒ޭܥ 3XX͸ϦμΠϨΫτܥ 4XX͸ΫϥΠΞϯτΤϥʔܥ 5XX͸αʔόΤϥʔܥ εςʔλείʔυ

  83. GET /api/articles(200 OK) GET /api/articles/1(200 OK) e.g. هࣄ৘ใΛѻ͏API

  84. ͲΜͳεςʔλείʔυ͕͋ Δ͔ௐ΂ͯΈ·͠ΐ͏ άϧʔϓ಺Ͱ෼୲ͯ͠ௐ΂ͨ ݁ՌΛڞ༗͠·͠ΐ͏ ԋश: εςʔλείʔυ

  85. هࣄ৘ใҰཡΛऔಘ͢Δ: GET /api/articles ID:1ͷهࣄ৘ใΛऔಘ͢Δ: GET /api/articles/1 هࣄ৘ใΛ࡞੒͢Δ: POST /api/articles ID:1ͷهࣄ৘ใΛߋ৽:

    PUT /api/articles/1 ID:1ͷهࣄ৘ใΛ࡟আ͢Δ: DELETE /api/articles/1 ԋश: HTTPϝιου
  86. GET /api/articles(200 OK) GET /api/articles/1(200 OK) POST /api/articles(???) PUT /api/articles/1(???)

    DELETE /api/articles/1(???) ԋश: εςʔλείʔυ
  87. GET /api/articles(200 OK) GET /api/articles/1(200 OK) POST /api/articles(201 Created) PUT

    /api/articles/1(200 OK) DELETE /api/articles/1(204 No Content) ԋश: εςʔλείʔυ
  88. HTTPϔομ

  89. ΫϥΠΞϯτ΍αʔόͷ ڍಈΛܾΊΔ ෇Ճ৘ใΛද͢

  90. Ϩεϙϯε͕Ͳ͏͍͏ܗࣜͳͷ ͔Λڭ͑ͯ͘ΕΔ application/json image/png e.g. Content-Type

  91. POST /api/articles Location: https://example.jp/ api/articles/2 ग़དྷ্͕ͬͨϦιʔεͷURLΛ ڭ͑ͯ͋͛Δ e.g. هࣄ৘ใΛѻ͏API

  92. JSON

  93. None
  94. ϢʔβϑϨϯυϦʔΛڧ͘ҙࣝ͢Δ ԿΛඞཁͱ͍ͯ͠Δ͔ Ͳ͏࢖͍͍ͨͱࢥ͍ͬͯΔ͔ ޙ͔Βཁૉ଍͍ͨ͠ͱ͖ʹࠔΒͳ ͍Α͏ʹ͢Δ JSON

  95. GET /api/articles(200 OK) [ “id1”, “id2”, “id3” ] e.g. هࣄ৘ใΛѻ͏API

  96. GET /api/articles(200 OK) [ { “id”: “id1”, “title”: “title”, “body”:

    “body” }, … } e.g. هࣄ৘ใΛѻ͏API
  97. GET /api/articles(200 OK) { “articles” : [ … ], …

    } e.g. هࣄ৘ใΛѻ͏API
  98. URL͸ϦιʔεΛҰҙʹࢦ໊ࣔ͢͠ࢺ HTTPϝιου͸GET/POST/PUT/DELETEͰද͢ಈ ࢺ εςʔλείʔυ͸2XX/3XX/4XX/5XXͰද֓͢ཁ HTTPϔομ͸ڍಈΛܾΊΔ෇Ճ৘ใ JSON͸ϢʔβϑϨϯυϦʔͳৄࡉ৘ใ ·ͱΊ

  99. ԋश

  100. ΠΧ͕ ΠϯΫΛ ܸͪ߹͏ήʔϜ https://twitter.com/omahuko/status/707978301457829889

  101. ໊લ ॏෳ͸ͳ͠ ϒΩܥ౷ γϡʔλʔ, ϩʔϥʔ, νϟʔδϟʔͷ3छ ϒΩ͸3छͷͲΕ͔ʹଐ͢Δ ϒΩͷ࢓༷

  102. ॳ৺ऀγϡʔλʔ ϒΩܥ౷: γϡʔλʔ

  103. ໊લ ϒΩܥ౷ ॳ৺ऀγϡʔλʔ γϡʔλʔ ॳ৺ऀϩʔϥʔ ϩʔϥʔ ॳ৺ऀνϟʔδϟʔ νϟʔδϟʔ 5SFBTVSFγϡʔλʔ γϡʔλʔ

  104. ϒΩ৘ใCRUD API(؅ཧऀ༻) Ұཡ ৄࡉ ࡞੒ ߋ৽ ࡟আ ࣍ͷAPIΛઃܭ͍ͯͩ͘͠͞

  105. ϒΩ৘ใҰཡΛϒΩܥ౷ͰߜΓ ࠐΜͰऔಘͰ͖ΔΑ͏ʹͯ͘͠ ͍ͩ͞ ࢓༷มߋ

  106. ໊લ ϒΩܥ౷ ॳ৺ऀγϡʔλʔ γϡʔλʔ 5SFBTVSFγϡʔλʔ γϡʔλʔ

  107. ϒΩ৘ใҰཡΛιʔτͰ͖ΔΑ ͏ʹ͍ͯͩ͘͠͞ Ͳͷ߲໨Ͱιʔτ͢Δ͔ ঢॱ/߱ॱ ࢓༷มߋ

  108. ಉ໊͡લͷϒΩΛ࡞੒͠Α͏ͱ ͨ͠৔߹ΤϥʔͱͳΔΑ͏ʹ ߟྀ͍ͯͩ͘͠͞ ࣍ͷAPIΛઃܭ͍ͯͩ͘͠͞

  109. ࣮ફฤ

  110. ࣮ӡ༻͕࢝·Δͱෛՙߴಅͳ Ͳ৭ʑͱ໰୊͕ى͖Δ λΠϜΞ΢τͨ࣌͠ αʔόΤϥʔ͕ى͖ͨ࣌ ͲͷΑ͏ʹΞϓϦέʔγϣϯͱͯ͠ৼΔ෣͍·͔͢ʁ