Slide 1

Slide 1 text

Web API ߨ࠲ @gomachan46 Treasure2017

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Իήʔ

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Web

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

WebΞϓϦέʔγϣϯ

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Web API

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Webܦ༝Ͱ ར༻Ͱ͖ΔAPI Web API is … ?

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

Web APIઃܭ

Slide 46

Slide 46 text

URL HTTP JSON Web APIઃܭ

Slide 47

Slide 47 text

URL Uniform Resource Locator

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

ΫΤϦετϦϯά Query String

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

HTTP HyperText Transfer Protocol

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

εςʔτϨε

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

ΫοΩʔ

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

ηογϣϯ

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

HTTPϝιου

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

ϝιου ҙຯ Կճ΍ͬͯ΋ ݁Ռ͕ಉ͔͡ ϦιʔεΛ มߋ͠ͳ͍͔ (&5 औಘ ˓ ˓ 1045 ࡞੒ ☓ ☓ 165 ߋ৽ ˓ ☓ %&-&5& ࡟আ ˓ ☓

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

هࣄ৘ใҰཡΛऔಘ͢Δ: 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ϝιου

Slide 79

Slide 79 text

εςʔλείʔυ

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

APIͷ݁ՌΛ ୺తʹද͢

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

هࣄ৘ใҰཡΛऔಘ͢Δ: 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ϝιου

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

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) ԋश: εςʔλείʔυ

Slide 88

Slide 88 text

HTTPϔομ

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

JSON

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

ԋश

Slide 100

Slide 100 text

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

Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

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

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

࣮ફฤ

Slide 110

Slide 110 text

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