Slide 1

Slide 1 text

͓લ͸͜Ε·Ͱʹ࡞ͬͨ APIͷ਺Λ͍֮͑ͯΔͷ͔ʁ Geeks Who Drink -Public API Edition- 2018/4/16

Slide 2

Slide 2 text

ࣗݾ঺հ ߴࢁԹ @edvakf ϐΫγϒגࣜձࣾ ࣥߦ໾һCTO݉෱ԬΦϑΟεϚ ωʔδϟʔ JavaScript, PHP, Ruby, Go, Scala

Slide 3

Slide 3 text

ࠓ೔ͷςʔϚ pixivͰ͜Ε·ͰؔΘ͖ͬͯͨ৭ΜͳAPIͷ࿩Λ͠·͢ Web APIͷ͜ͱͰ͢ ΫϥεΠϯλʔϑΣΠεͳͲͷ͜ͱͰ͸͋Γ·ͤΜ

Slide 4

Slide 4 text

PHP→JS API 2010೥͝Ζ͔Β͋ͬͨɺ͍ΘΏΔී௨ͷJSON over HTTP API PHPͷarrayʢ഑ྻʹ΋࿈૝഑ྻʹ΋ͳΔߏ଄ʣΛjson_encodeͨ͠΋ͷ ܕఆٛͳͲແ͍ αʔόʔͱΫϥΠΞϯτΛಉ࣌ʹσϓϩΠͰ͖Δ PHP΋JS΋ʮintͬΆ͍stringΛ਺஋ͱͯ͠ѻ͑ΔʯͳͲܕʹॊೈ DB͔Βऔಘͨ͠஋Λͦͷ··JSONʹͨ͠Γ͍ͯͨ͠ͷͰɺͪΐͬͱةͳ͍͜ͱ ͕͋ͬͨ Τϥʔॲཧ΋ద౰

Slide 5

Slide 5 text

JSON APIͷΤϥʔॲཧ JSONͱͯ͠ύʔεͰ͖ͳ͚Ε͹Τϥʔ PHPͷΤϥʔॲཧΛHTMLΛฦ͢৔߹ͱJSONΛฦ͢৔߹Ͱ෼͚ͯͳ͔ͬͨͷͰɺ200Ҏ֎ͩ ͱHTMLͷΤϥʔϖʔδΛฦ͍ͯͨ͠ 200Ҏ֎ͳΒΤϥʔ εςʔλείʔυ͸ϓϩΩγ΍ϩάॲཧ΍ݕࡧΤϯδϯͳͲɺࣗ෼ͨͪͰίϯτϩʔϧ͠ ʹ͍͘৔ॴͰ࢖ΘΕΔ αʔόʔͱΫϥΠΞϯτ྆ํΛࣗࣾͰ࣋ͬͯΔͳΒ͋·Γεςʔλείʔυʹ߆Βͳ͍Ͱ ͍͍ errorͱ͍͏Ωʔ͕͋Ε͹Τϥʔʢ200Ͱ͋ͬͯ΋ʣ →Ͳͷύλʔϯ΋Α͘࢖ͬͯΔ

Slide 6

Slide 6 text

CSV API ࠷ॳظͷpixivΞϓϦ༻API͸CSVΛฦ͍ͯͨ͠ ྫ͑͹ `id, name, age` Λฦ͢ͳΒ `1000,ࢁాଠ࿠,23` Έ͍ͨͳϨεϙϯεͩͬͨ ෳ਺ϨεϙϯεΛฦ͢API͸ෳ਺ߦʹͳΔ APIΛ֦ு͢Δ࣌͸ӈଆʹΧϥϜΛ௥Ճ͢Δ ࢖Θͳ͘ͳͬͨΧϥϜ͸ۭจࣈʹͳΔʢͭ·ΓΧϯϚ͕࿈ଓ͢Δʣ ΧϥϜͷఆٛ͸ࣾ಺υΩϡϝϯτʹ͋Δ ͳͥJSONΛ࢖Θͳ͔ͬͨͷ͔ܦҢ͸ෆ໌ JSONʹൺ΂ͯΩʔΛ࣋ͨͳ͍ͷͰϨεϙϯεαΠζ͸খ͍͞

Slide 7

Slide 7 text

"Public API" 2012೥ࠒʹCSVͰ͸ͳ͍ʮͪΌΜͱͨ͠ʯεϚϗΞϓϦ޲͚API͕ཉ ͍͠ͱ͍͏੠͕ߴ·Δ ࣾ֎ͷఏܞاۀ͔Β΋APIΛٻΊΒΕΔ͜ͱ͕͋ͬͨ ʮ֎෦޲͚APIʯΛ࡞ͬͯɺΞϓϦ΋ͦΕΛ࢖͏Α͏ʹ͠Α͏ →"Public API" APIυΩϡϝϯτ΋υΩϡϝϯταΠτΛ࡞ͬͯMarkdownͰॻ͍ͨ OAuthೝূΛ͜ͷ࣮࣌૷

Slide 8

Slide 8 text

"Public API"ͷࣦഊ Twitter API΍GitHub APIͳͲɺެ։API͕ීٴͨ࣌͠୅ͩͬͨͷͰɺෆಛ ఆଟ਺ͷ։ൃऀ͕࢖͏APIΛ૝ఆͯ͠࡞ͬͨ pixivͱ͍͏αʔϏεͷબ୒ͱͯ͠ɺAPIΛҰൠެ։͢Δ͜ͱ͸ͳ͔ͬ ͨ ͍͔ͭ͘ͷఏܞاۀͱɺࣗࣾΞϓϦɾαʔϏε͔Β͔͠࢖Θͳ͍ ࣾ֎޲͚΋૝ఆͨ͠APIͳͷʹɺࣾ಺͔Β͔͠࢖Θͳ͍Ӆ͠ύϥϝʔλ ͕͋ͬͨΓ υΩϡϝϯταΠτͷߋ৽΋଺͍ͬͯͨΓ

Slide 9

Slide 9 text

LSUDsͱSSKDs Web API: The Good Parts (2014) LSUDs: Large Set of Unknown Developers େ੎ͷ஌Βͳ͍։ൃऀ޲͚ APIͷߋ৽͸ك SSKDs: Small Set of Known Developers গ਺ͷ஌ͬͯΔ։ൃऀ޲͚ʢࣗࣾΞϓϦ༻APIͳͲʣ APIͷߋ৽͸සൟ

Slide 10

Slide 10 text

ࣾ಺API ࣾ಺ͷαʔόʔؒ௨৴APIΛThriftͰߏங ThriftɿAPIهड़ݴޠΛॻ͍ͯαʔόʔͷΠϯλʔϑΣΠεͱΫϥΠΞϯτϥΠϒ ϥϦΛࣗಈੜ੒͢Δπʔϧ αʔόʔͷΠϯλʔϑΣΠε࣮૷͚ͩΛ࡞Ε͹ྑ͍ ࣮૷ͱυΩϡϝϯτͷဃ཭͕΄΅ੜ·Εͳ͍ pixiv͸PHPɺपลαʔϏε͸RubyͩͬͨΓScalaͩͬͨΓGoͩͬͨΓ εϚϗΞϓϦͷAPIͱͯ͠͸࢖Θͳ͍͜ͱʹͨ͠ طʹJSON͕σϑΝΫτελϯμʔυʹͳ͍ͬͯͨ όΠφϦͳͷͰɺcurlͰͪΐͬ͜ͱୟ͘Έ͍ͨͳσόοά͕໘౗

Slide 11

Slide 11 text

ݱࡏͷpixivΞϓϦ༻API "Public API"͸ເͱر๬Λ٧ΊࠐΜͩ΋ͷͩͬͨ ಺෦࣮૷తʹ΋ɺܧঝ΍ந৅Խͷմʹͳ͍ͬͯͨ ݱࡏͷΞϓϦ༻API͸ۃྗγϯϓϧʹɺDBݺͼग़͠ͷؔ਺Λ਺ݸݺΜ ͰJSONͷܗΛ࡞Δ͚ͩʹಛԽ CQRSʢDBҾͬுͬͯ͘Δ͚ͩͳͷʹؤுΓ͗͢Δͳʣ ຊ౰͸Command Query Responsibility Separation Swaggerఆٛ͸ඞͣॻ͘

Slide 12

Slide 12 text

Swagger (Open API Spec) APIఆٛΛॻ͘πʔϧ YAMLͰॻ͘ɺιʔείʔυதʹॻ͘ɺίϝϯτͰॻ͘ͳͲ swagger-uiͱ͍͏υΩϡϝϯτϖʔδͰ࣮ࡍʹϦΫΤετΛૹͬͯΈΔ͜ ͱ͕Ͱ͖Δ swagger-codegenͰΫϥΠΞϯτϥΠϒϥϦੜ੒ ͦ͏͍͏ΤίγεςϜ͕௒ॆ࣮͍ͯ͠Δ ࣾ಺Ͱ͸3೥͙Β͍લʹScalaͷAPIͰಋೖͨ͠ͷΛ͖͔͚ͬʹɺRails΍PHPͰ ΋ಋೖ͕ਐΈɺݱࡏ͸Swaggerͷແ͍API͸ك

Slide 13

Slide 13 text

Swagger-UI

Slide 14

Slide 14 text

APIఆٛͷࣗಈςετ APIͷ࣮૷͕ఆ͔ٛΒဃ཭͍ͯ͠ͳ͍͜ͱΛ୲อ͍ͨ͠ SwaggerΛRspecͰςετ SwaggerͷAPIఆٛ͸JSON SchemaͷαϒηοτͳͷͰJSON SchemaͷπʔϧͰݕূͰ͖Δ ͋Γ͕ͪͳʮΩʔ͕૿͑ͨͷʹυΩϡϝϯτॻ͖๨ΕͯΔʯ΋ ๷͛ΔΑ͏ʹͨ͠ APIͷϦϑΝΫλϦϯά͕ѹ౗తʹϥΫʹʂ

Slide 15

Slide 15 text

To REST or Not To REST ʮRESTʹ͢Δ΂͖͔൱͔ʯ͸Αٞ͘࿦ʹͳΔ ݸਓతʹ͸ɺಛʹRESTʹͩ͜ΘΔඞཁ͸ͳ͍ͱײ͡Δ RailsΛ࢖͍ͬͯΔͳΒRESTʹͯ͠΋ྑ͍ͷͰ͸ɺఔ౓ RailsͰ΋ϧʔςΟϯάͰଟগαϙʔτ͕͋Δ͚ͩ Swaggerॻ͍͓͚ͯ͹URLͷܗࣜ͸ΦϚέఔ౓ ಉ༷ʹGraphQL΋͋·Γ৯ࢦ͕ಈ͔ͳ͍ JSON+SwaggerͷΤίγεςϜ͕͜Ε͚ͩॆ࣮ͨ͠ࠓͱͳͬͯ͸…

Slide 16

Slide 16 text

ͦͷଞ APIΛ࢖ͬͯ৭ʑͰ͖Δ࿩

Slide 17

Slide 17 text

ΞϓϦͱ΢ΣϒͰಉ͡API

Slide 18

Slide 18 text

Viron

Slide 19

Slide 19 text

·ͱΊ ࣾ಺Ͱ͔͠࢖Θͳ͍ͷͳΒγϯϓϧʹ࡞Ζ͏ APIυΩϡϝϯτ͸ઈରॻ͜͏