èªãã æ¬
æ¬æžãèªã¿ãªãããåèã«ãªã£ããšãããæ®æ®µèªåãéçºããŠããŠæã£ããšãããªãããæžããŠãããŸãã
æ¬æžã§ãããšããã®Web APIã¯ãã£ã±ãRESTãåæã§ãã(GraphQL,gRPCã«ã€ããŠãèšåã¯ãããŠããŸã)
ãã£ãã
æ®æ®µã¯èªç€Ÿãããã¯ãçšã®REST Endpointãå®çŸ©ããããšãå€ãã®ã§ããå²ãšã®ãã§ãã£ãŠããŸã£ãŠãããã®æ¬ã§èããŠããã¹ãèŠçŽ ãªãããåŠã¹ãã°ãããªãšæãèªãã§ã¿ãããšã«ããŸããã
ãŸãšã
APIã決ãããšãã«èããŠããã¹ãããšã®æŠèŠãã€ãããã®ã§ã¯ãªãã§ããããã ãã ãããªãããåé¡æèµ·ã«å¯ŸããŠåºæ¬çã«çãã¯ã(ç¶æ³æ¬¡ç¬¬ | ããªãã決ãã | ...)ãªã®ã§ãããããã¹ããã©ã¯ãã£ã¹ã ãæããŠçãªèªã¿æ¹ã¯ã§ããŸããã§ããã
- ã©ã®ããã«ããŠæ£ãããã£ãã·ã¥æéã決ããã°ããã®ã ãããããããç¶æ³æ¬¡ç¬¬ã§ããã
- ãã®åé¡(ããã¥ã¡ã³ããå®è£ ããçæãããç¬ç«ããŠç®¡çããã)ã«é¢ããŠã¯ãã©ã®æ¹æ³ããããæªããã§ã¯ãªããèšèšè ãšçµç¹ã«ãšã£ãŠããŸãããæ¹æ³ãéžæããå¿ èŠããããçã
第1éš APIãã¶ã€ã³ã®åºç€
第1ç« APIãã¶ã€ã³ãšã¯äœã
ãããããAPIãšã¯ãªã«ãã¿ãããªè©±ããå§ãŸããŸãã
Web(IT)ã®å匷ãã¯ããããšãAPIã£ãŠããããŠãã³ãšããªãã£ãã®ã§ãããã"APIãšã¯"ã«ãã£ããçããŠãããŠããæ¬ã¯æå€ãšè²Žéãªã®ã§ã¯ãšæããŸããã
ã¡ãªã¿ã«ãèªåã®äžã§ã®"API"ãšã¯ã®çãã¯ããã¡ããæèäŸåã§ã¯ããã®ã§ãããjsonè¿ãhttp serverã§ãã
æ¬æžã§ã¯ä»¥äžã®ããã«æžãããŠããŸãã
APIã¯ãã©ã®ãããªçš®é¡ã®ãã®ã§ãããäœããããŸãã€ã³ã¿ãŒãã§ã€ã¹ã§ãããïŒã€ã®ã·ã¹ãã ã察象è ãçµç¹ãªã©ãåºäŒãããåããããã€ã³ãã§ããã
ãã¯ãæŠå¿µèªäœãææ§ãªã®ã§ããã£ããããªããããªããããªèª¬æã«ãªã£ãŠããŸããŸãããã
11ç« ã§ã¯
Web APIãç°¡åã«èŠçŽãããšãåäœã®åæåã®ãªã¯ãšã¹ã/ã¬ã¹ãã³ã¹ + REST + HTTP/1.1 + JSON Web APIã«ãªãã ããã
ãšãããŸãã 第1ç« ã¯æ®æ®µããAPIãéçºããã䜿ã£ããããŠããéçºè ã®æ¹ãããAPIã«ã€ããŠããŸããã³ãšããŠããªãæ¹(äŸãã°ããžãã¹ã®æ¹)ã«å¯Ÿãã説æãšããŠãšãŠãããã®ã§ã¯ãšæããŸããã
第2ç« ãŠãŒã¶ãæèããAPIãèšèšãã
APIãå®éã«å©çšããã³ã³ã·ã¥ãŒãã®èŠç¹ãšAPIãæäŸããåŽã®çµç¹ããœãããŠã§ã¢ã®ãããã€ãã®èŠç¹ãšãã芳ç¹ãå°å
¥ãããŸãã
èŠã¯å®è£
ã®éœåãé èœããããšããããšã ãšç解ããŠããã®ã§ããããããã€ãã®èŠç¹ã®ã¿ã«ããšã¥ããŠã€ããããAPIãããã«äœ¿ãã«ããããé»åã¬ã³ãžã®å
·äœäŸã§ããã§ãããšèª¬æãããŸãã
APIã®ãŽãŒã«ãªã¹ããšããŒã¿ãããŒã¿ããŒã¹ãšããŸãã«ãäžèŽããŠãããšãããããã®APIããããã€ãã®èŠç¹ã«ãã£ãŠèšèšããŠããå¯èœæ§ãããã
ããã¯æå€ãšãã£ãŠããŸããã¡ã§ãã£ãããããããå®è£
ãã¿ããããã®ã§æ°ãã€ãããã§ãã
ãŸãšãããšãAPIãã¿ããšãããã€ãã®ããŒã¿ãã³ãŒããããžãã¹ããžãã¯ããœãããŠã§ã¢ã¢ãŒããã¯ãã£ã人éçµç¹ãããã£ãŠããŸãã®ãé¿ããããšããããšã ãšæããŸãã
ã³ã³ãŠã§ã€ã®æ³åãšããŠãã·ã¹ãã ãèšèšããçµç¹ã¯ãçµç¹ã®ã³ãã¥ãã±ãŒã·ã§ã³æ§é ã«ãã£ããã®èšèšãçã¿åºããšããæ Œèšã玹ä»ãããŠããŸããã
èªåã¯å€§ããªçµç¹ã§åããããšããªãã®ã§ããŸããŽããšããªãã£ãã®ã§ããããã¯ã©ã®çšåºŠæ®éçãªãã§ããããã
第3ç« ããã°ã©ãã³ã°ã€ã³ã¿ãŒãã§ã€ã¹ãèšèšãã
RESTã«ã€ããŠäžå¯§ã«èª¬æããŠãããŠããŸããããã§ã¯å²æããŸãããã¯ãããŠAPIèšèšãããæ¹ã«ãæ¬æžã¯ããããã§ãããªãšæããŸããã
CRUDã®HTTPã¡ãœãã(POST, GET, PUT, PATCH, DELETE)ãåãªãCRUDãè¶
ããã¢ã¯ã·ã§ã³ããããã¯ããŸãCRUDã§ã¯ãªãã¢ã¯ã·ã§ã³ãè¡šãããã«äœ¿ããªããã°ãªããªãããšãããã
ãšè¿°ã¹ãããŠãããããã¯å Žåã«ãã£ãŠã¯ããã©ã³èšèšè
ã«ãšã£ãŠãé£ããåé¡ãšãããŠããŸãã
gRPCã§APIãå®çŸ©ã§ãããšåŠçãã¡ãœãããšããŠçŽ çŽã«è¡šçŸã§ããã®ã§ãããããRESTã«ç¿»èš³ããããšãããšHTTPã¡ãœããã§æ©ãã ãããããŠäžéçãªãªãœãŒã¹(åŠçã®ãªã¯ãšã¹ããšã)ãè¡šçŸããŠãããäœæããŠããäœã«ããããããªãšæããŸããã
æ¬æžã§ã
èšèšè ãREST ãªãœãŒã¹ã§ã®ã¢ã¯ã·ã§ã³ãHTTPã¡ãœããã«ãããã³ã°ã§ããªãå Žåãæåã®éžæè¢ã¯ãããŠãã¢ã¯ã·ã§ã³ãªãœãŒã¹ãäœæããããšã§ããã
ãšãããã·ã§ããã³ã°ã«ãŒãã®ãã§ãã¯ã¢ãŠããPOST
/cart/checkout
, /check-out-cart
ã§è¡šçŸããããšãææ¡ããŠããŸãã
ãŸããPOST
ã¯ãŠãŒã¹ã±ãŒã¹ã«é©ããã¡ãœããããªãå Žåã®ããã©ã«ãã®ã¡ãœããã§ãããšããŠããŸãã
ãããRESTã¢ãã«ã«è¿ã¥ããã«ã¯PATCH
/cart
status = CHECKING_OUT
ãšããæ¹æ³ããããããã§ãã¯ã¢ãŠãããæ¹æ³ããããã¥ãããªã£ãŠããŸãç¹ãææããŠããŸãã
倧åãªããšã¯ããŠãŒã¶ãŒãã¬ã³ããªæ§ãšAPIã¹ã¿ã€ã«ãžã®æºæ ã«ã¯ãã¬ãŒããªããããããšãèªèããŠããããšã®ããã§ãã
RESTã«é¢ããŠã¯ä»¥äžã®ããã¥ã¡ã³ããèªãããšããªã¹ã¹ã¡ãããŠããŸãã
- Architectural Styles and the Design of Network-based Software Architectures
- Reflections on the REST Architectural Style and âPrincipled Design of the Modern Web Architectureâ
第4ç« APIèšè¿°ãã©ãŒãããã䜿ã£ãŠAPIãèšè¿°ãã
ãã®ç« ã§ã¯OpenAPI Specification(OAS)ã®æžãæ¹ã«ã€ããŠäžå¯§ã«è§£èª¬ããŠãããŠããŸãã
èªåãæè¿ãSwagger(2.0)ããOAS(3.0)ã䜿ãããã«ãªããŸããã
ã¯ãããŠOASãæžãããæ¹ã¯ãã®ç« ã§æŠèŠãæŽãã§ãããå
¬åŒã®ããã¥ã¡ã³ããã¿ãªããæžããŠã¿ãã®ãããã®ã§ã¯ãªãã§ããããã
description propertyã¯ããã©ãããã£ãããèŠãã°ãããã ãããšæã£ããããŸãããæžããã ããããŠãããã»ãããããšããã®ãèªåã®æ°å°ãªãç¥èŠã§ãã
OASãæã§æžããçæãããã«ã€ããŠã¯åŸã«ãµããããŸãã
第2éš ãŠãŒã¶ãã«ãªAPIã®èšèš
第5ç« åçŽæå¿«ãªAPIãèšèšãã
ååã®ä»ãæ¹
倧åãªã®ã¯ã³ã³ã·ã¥ãŒããŒãç解ã§ããããšã
- ç¥èªã¯äžè¬çãªãã®ä»¥å€ãããŠãããèãã§ã¯ãªãã
- booleanãšãã£ãåã¯ããã¥ã¡ã³ãã¿ãã°ãããã®ã§ååã«ãããªãã
- ã³ã³ããã¹ããå©çšããŠããã(
user.userName
->user.name
)
䜿ããããããŒã¿åãšãã©ãŒããã
- äºåã«èšç®ãããä»å 䟡å€ããã€ããŒã¿ãæäŸããã°ã³ã³ã·ã¥ãŒãåŽã§äœãããããªã£ããæšæž¬ãããããªããŠãããªãã
/accounts/<UUID>
ãã/accounts/<AccountNumber>
ã®ã»ãããŠãŒã¶ãã¬ã³ããªãŒãšãããã
TimeããŒã¿ã©ããããåé¡
UNIXã¿ã€ã ã¹ã¿ã³ãããISO 8601ã®æååã®ã»ãããã£ãšãŠãŒã¶ãã¬ã³ããªãŒã§ãããšãããŠããŸãã "2015-02-07"ã®ããã«ã¿ã€ã ãŸãŒã³åãéãã®ãªã¹ã¯ãæžããããã«å¿ èŠããªããšãã¯æå»å€ãæäŸããªãããšãå§ãããšããã ããããUTCãªã®ãJSTãªã®ãããããªãã®ã§ãRFC3369ã®ããã«å®å šãªæ å ±ã§ããã®ã§ã¯ãšæããŸããã
Enumã©ããããåé¡
æ°å€ã³ãŒãã䜿ãã®ã¯ãããŠããããªãèãã§ããã(éçºè
ã絶ããããã¥ã¡ã³ãã調ã¹ãã¯ãã«ãªããã)
0ãããªããŠã"type": "checking"
ãªãããã®çç±ã§æ°å€ã䜿ãå¿
èŠããããšãã¯ãtypeName
propertyãè¿œå ããŠããããšããã
ãšã©ãŒãã£ãŒãããã¯
ãšã©ãŒã¯ä»¥äžã®ããã«åé¡ã§ããã
malformed error å¿ é ãã©ã¡ãŒã¿ããªãã£ãããããŒã¿ã®åãéã£ããã
functional error ããããããžãã¹ããžãã¯ã«ãããšã©ãŒã
server error å®è£ ã®ãã°ã ã£ããDBãèœã¡ãŠãããã
ã³ã³ã·ã¥ãŒãã®èŠç¹ããã¯server errorã¯äžã€ç¹å®ããŠããã°ãããŠãååã
RFC7231ã«ããã°ã³ã³ã·ã¥ãŒãã«èµ·å ãããšã©ãŒã¯4XX, ãããã€ãã«èµ·å ãããšã©ãŒã¯5XXã䜿ãã
- malformed errorã¯400(Bad Request)
- functional error
- 403(Forbidden)
- 409(Conflict)
- server errorã¯500(Internal Server Error)
HTTPã¹ããŒã¿ã¹ã³ãŒãã ãã§ã¯äžåååé¡
è¡šçŸããããšããŠãããšã©ãŒã«å¯Ÿå¿ããã¹ããŒã¿ã¹ã³ãŒããããã°ããããŽã£ããããªããã®ãããã
Formã®field A
ãValidationéåã§ããããšãäŒãããå Žåã400ã ãã§ã¯è¶³ããªãã
çµå±HTTPã¹ããŒã¿ã¹ã³ãŒã以å€ã«ãšã©ãŒãèå¥ããæ
å ±ãå¿
èŠã
æ¬æžã§ã¯ä»¥äžã®ãããªãšã©ãŒã¬ã¹ãã³ã¹ãäŸç€ºãããŠããã
ãŠãŒã¶ã«è¡šç€ºãããšã©ãŒã¡ãã»ãŒãžãbackendããè¿ããããã³ãã§å®çŸ©ãããã¯å€æããããããšããã ãšæããã©ã¡ãã«ãã ãšã©ãŒãèå¥ããæ
å ±(ããã§ã¯type
)ãå¿
èŠã
ãããªã£ãŠãããšãšã©ãŒã«ãŽã£ãããã4XXã®ãšã©ãŒã³ãŒããè¿ãå¿
èŠæ§ãèããŠããããããªãããšæãã
ãã ããHTTPã¹ããŒã¿ã¹ã³ãŒãã¯frontãšbackendéã®æ§ã
ãªã³ã³ããŒãã³ãã«ãäŒããæ
å ±ãªã®ã§ãå
šéš500ã«ãããšããã¯ããã§åé¡ãèµ·ããã
ãšããããšã§èªåã®çµè«:
- ãªãœãŒã¹ãèŠã€ãããªãNotFoundãèªèšŒ/èªå¯ãšã©ãŒã¯ããããã¹ããŒã¿ã¹ã³ãŒã察å¿ãããã(ãã以å€ãå¯)
- ä»ã¯å šéš400
- ãšã©ãŒã«ã¯ãšã©ãŒãèå¥ããæ å ±ãä»äžãã
ããããŠããã°ã1æ¥ã®NotFoundæ°ãæ¥ã«å¢ããŠããŠããã¿ãããªæ
å ±ãã¡ããªã¯ã¹ããæŸãã€ã€ããHTTPã¹ããŒã¿ã¹ã«æ©ãŸãªããŠãæžãã
ãã ãã®æ¹æ³ããšããšãproductionã®formã«ãããªãœãŒã¹äœæã®å Žé¢ãªããã§ã¯çµå±ãã¹ãŠã®fieldã«ãªãããã®validationãããã®ã§fieldããšã«ãšã©ãŒèå¥æ
å ±ãå®çŸ©ããå¿
èŠãã§ãŠããŠããŸããæ倧æåæ°ãªããã¯ããã³ãã§ãããããšæããããããªããCSVçã®ãããäœæã ã£ããAPIé£æºã ã£ããã§ããã³ãä»ããªãã«ãŒããçãŸããŠããã®ã§çµå±backendã§èå¥ããå¿
èŠãã§ãŠããã
ãšã©ãŒãè€æ°è¿ããªããšãããªãåé¡
ãšã©ãŒãã²ãšã€ã ãã®å Žåã¯äžèšã®æ¹éã§ãããããšæã£ããã®ã®ãå®éã«ã¯Validationéåãªããã¯è€æ°ãããã
ããã«ãšã©ãŒãHTTPã¹ããŒã¿ã¹ã³ãŒãã§åé¡ããŠããå Žåãã©ã¡ããã®ã¬ã¹ãã³ã¹ã§è¿ãå¿
èŠãããã(ãããã¯ãã以å€)
èªåã®çµè«:
- HTTPã¹ããŒã¿ã¹ã³ãŒãããšã«ã¬ã¹ãã³ã¹ã®åããã
- Validationç³»ã®ãšã©ãŒ(functional error/400 Bad Request)ã¯è€æ°ã®ãšã©ãŒè¿ããããã«ããŠãã
å®è£ çã«ãèªèšŒ/èªå¯ãšåŠçã«å¿ èŠãªæ å ±ã®fetchããŠããvalidationããžãã¯èµ°ãããã®ã§ãããã圢ã«èœã¡çããã
æåã®ãã£ãŒãããã¯
åãªã確èªå¿çã§ã¯ãªãã³ã³ã·ã¥ãŒãã«æçãªæ
å ±ãæäŸãããã®ã§ãªããã°ãªããªãã
äœæããããªãœãŒã¹ã®æ
å ±ãäœããããµããŸããŠããã¹ãã§ããã次ã®ã¹ãããã§åœ¹ç«ã€ãããããªãæ
å ±ãæäŸãããšãããšãããŸãã
å人çã«ããäœæããããªãœãŒã¹ã®æ å ±ããã¹ãŠè¿ããŠããããšãã¹ãæžãããããŠãããªãšæããŸãã
第6ç« äºæž¬å¯èœãªAPIãèšèšãã
äžåºŠã蚪ããããšã®ãªã建ç©ã§ãã¢ãéããæ¹æ³ãç¥ã£ãŠããã®ã¯ãªãã ããããåããããªãã¢ãåã«ãããããšãããããã ã
äžè²«æ§
ã°ãã€ããççŸã®ãªãäžè²«æ§ã®ããèšèšãéèŠã§ãããšèšãããŠããŸãã
äžè²«æ§ã®ãªãå
·äœäŸãšããŠãåãæ
å ±ããšã³ããã€ã³ãããšã«éãååã«ãªã£ãŠããäŸãæããããŠããŸãã
ãã®èŠè«ã¯ãããšfieldåã«ã³ã³ããã¹ãã ããŠãããšããèãæ¹ãšã¶ã€ããã®ã§ãã®ããããæ©ãŸãããšããã§ããã
/accounts/{accountNumber}
ãš/transfers/delayed/{transferId}
ã®ããã«URLã®éå±€ã¬ãã«ãéãããšãäžè²«æ§ã«åããäŸãšããŠæããããŠããŸãã
(ãã®å Žå/delayed-transfers/{transferId}
ã«ãããšã)
äžè²«æ§ã®4ã€ã®ã¬ãã«
ä»ãŸã§ã®è©±ã¯åºæ¬çã«APIãå éšãã®äžè²«æ§ã§ãAPIããã€ã¹ãäžè²«æ§ã¯ããã«ãšã©ãŸãã以äžã®ã¬ãã«ããããšãããŠããŸãã
- ã¬ãã«1: APIå éšã§ã®äžè²«æ§
- ã¬ãã«2: çµç¹ãäŒæ¥ãããŒã ã®APIã«ãŸãããäžè²«æ§
- ã¬ãã«3: APIã®åé¡é åã§ã®äžè²«æ§
- ã¬ãã«4: å€ã®äžçãšã®äžè²«æ§
ã¬ãã«2ã«é¢ããŠã¯èšèšè
ã®å®å以å€ã«ãAPIãããã€ãçµç¹ã®ããŒã åã®ãããªãã®ãåãããŠãããã§ãã 13ç« ã§ãAPIãµãŒãã§ã¹ã®äžè²«æ§ãä¿ã€ã«ã¯APIã®èšèšè
å士ãååããå¿
èŠãæããããŠããŸãã ã¬ãã«3ã«ã€ããŠã¯ãã¡ã€ã³ç¥èãã€ãããããªãããã ãªãšæããŸããã
ã¬ãã«4ã§ã¯æšæºèŠæ Œãããã°ã§ããã ãæºæ ããããšãå«ãŸããã¿ããã§ãã(åçãã¿ã³ã®äžè§åœ¢ã¯ISO 7000ã§å®çŸ©ãããŠãã)
ããšãçµç¹å
ã®APIèšèšè
ãããªãäžäººã§ãAPIãã¶ã€ã³ã¬ã€ãã®ãããªããã¥ã¡ã³ããæŽåããå¿
èŠæ§ã説ãããŠããŸãã
èªåããã¶ã€ã³ã¬ã€ãã§åœåèŠåã決ãããšããããã¯ãããŠã¿ãããšæã£ãŠããŸãã
é©å¿æ§
Accept
/Content-Type
ãå©çšããã³ã³ãã³ãããŽã·ãšãŒã·ã§ã³ãAccept-Language
/Content-Language
ã«ããåœéåãšå°ååã«ã€ããŠãµããããŠããŸãã
çºèŠæ§
paginationã«é¢ããæ
å ±(ä»äœããŒãžç®ãåèšã®ãªãœãŒã¹ã«ãŠã³ã)ãã次ã«æå¹ãªã¢ã¯ã·ã§ã³ãè¿ããšãã£ããä»ã©ãã«ããŠãäœãã§ããããã«é¢ããæ
å ±(ã¡ã¿ããŒã¿)ãè¿ãããšãææ¡ãããŠããŸãã
ä»ãŸã§ã¯ãpaginationç³»ã®æ
å ±ããã¡ã¿æ
å ±ãšããŠè¿ãAPIããäœã£ãŠããªãã£ãã®ã§ãã®èãã¯ãšãŠãåèã«ãªããŸããã
APIã®ã¬ã¹ãã³ã¹ã«"href": "/resource/123/actions"
ã®ããã«é¢é£ããæ
å ±ãlinkã®åœ¢ã§æäŸããããšã§APIã®çºèŠå¯èœæ§ãé«ããèãã玹ä»ãããŠããŸãã
æ¥ããããªãããã®ãããã«ç¥èŠããªããæ¯éãã¹ããã©ã¯ãã£ã¹ãç¥ããããšæã£ãŠããŸããã
ãã æ¬æžã§ã¯ãHAL, Collection+JSON, JSON API, JSON-LD, Hydra, Sirenãšãã£ããã©ãŒããããç¥ãããŠãããæšæºã®ãããªãã®ã¯ãªããšæžãããŠãããç¹å®ã®æ¹åŒãæšããŠãããšããããšã¯ãããŸããã§ããã
(HATEOAS
ã®çºé³ã¯èè
ã§ãã£ãŠãããããªããããã§ã)
第7ç« ããŸãæŽçãããç°¡æœãªAPIãèšèšãã
ã¬ã¹ãã³ã¹ã®json fieldã«ã€ããŠä»¥äžã®ãããªèŠç¹ããã®æŽçãææ¡ãããŠããåèã«ãªããŸãã
- é¢é£ããæ å ±ã¯objectã«åãåºã
- é¢é£ããæ å ±ã¯è¿ãã«ãã
- éèŠåºŠãé«ãé ã«ãªãã¹ã
è€æ°ã®ãšã©ãŒãè¿ãå Žåã«ã¯ãéèŠãªãšã©ãŒé ã«ãœãŒãããããšãææ¡ãããŠããŸãã
å人çã«ã¯ããã¯ã¡ãã£ãšãããããªæ°ãããŸããã(ãªããªãåªå
床ãã€ããããªããšã©ãŒãå€ãããããªãããªãš)
第3éš ã³ã³ããã¹ãã«å¿ããAPIãã¶ã€ã³
ããã§ããã³ã³ããã¹ããšã¯ä»¥äžã®ãããªèŠç¹ãšç解ããŠããŸãã
- ãããã¯ãŒã¯ãæå¹å©çšã§ããŠããã
- ã»ãã¥ã¢ã
- ç Žå£çãªå€æŽããããããããã«èšèšãããŠããã
- ããã¥ã¡ã³ãåã§ããŠããã
第8ç« ã»ãã¥ã¢ãªAPIãèšèšãã
APIã®ã»ãã¥ãªãã£ã«ã€ããŠã¯äžåã®æ¬ã«ãªã£ãŠããããããªãã®ã§æ¬æžã¯ãªãŒããã¥ãŒãšããŠäœçœ®ã¥ããããŠããŸãã
OAuth 2 in ActionãAPI Security in Actionã玹ä»ãããŠããŸããã
APIã®ãŽãŒã«(endpoint)ãã©ãããåäœã§åãããèãããšãã«ã¹ã³ãŒããèæ
®ã«ãããŠããããšã倧äºããã§ãã
ã¹ã³ãŒãã®ããªã·ãŒãåèã«ãªããŸãããOASã ãšscopes
ãšããŠå®çŸ©ã§ããã®ãããã§ããã
ã¹ã³ãŒãã«å ããŠããã²ãšã€æ€èšããŠããã¹ãããšããããŸãããããAPIã®ãªã¯ãšã¹ã/ã¬ã¹ãã³ã¹ã«å«ãŸããã»ã³ã·ãã£ããªããŒã¿ã®åãæ±ãã§ãã
ãããéã«æ±ã£ãŠãããµãŒãã¹ãæã
話é¡ã«ãªã£ããããŸããã
ãªã«ãã»ã³ã·ãã£ããªããŒã¿ã«ããããã¯ãã¡ã€ã³(æ¥çãç£æ¥)ã«ããã®ã§å¿
ãCISO(Chief Information Security Officer)ãDPO(Deta Protection Officer)ãCDO(Chief Data Officer)ãŸãã¯æ³åéšã«çžè«ããããšã¢ããã€ã¹ãããŠããŸãã
çæ§ã®è·å Žã«ã¯ãããã£ã圹è·ã®æ¹ã
ããããŸãã§ããããã
ãªãœãŒã¹ãžã®ã¢ã¯ã»ã¹ã«å¯ŸããŠ403 Forbiddenãè¿ãããšã¯ãšãã«æé»çã«ãã®ãªãœãŒã¹ã®ååšãèªããŠããããšã«ãªãããæ
å ±æŒæŽ©ãšã¿ãªãããããšããããšæ³šæãããŠããŸãã
ã¯ã¬ãžããã«ãŒãçªå·ãšãã¯æããã§ããããã¡ã€ã³ãç解ããŠããªããšãã£ãŠããŸããããããªãã®ã§æ³šæãããã§ãã
GETã®ã¯ãšãªãã©ã¡ãŒã¿ã«ãã»ã³ã·ãã£ããªæ
å ±ãããªãããã«æ³šæãããŠããŸãã
GET /accounts?customerLastName=yuta
ã®ããã«ãã£ãŠããŸããšãã³ã³ã·ã¥ãŒããšãããã€ãéã®ãã¹ãŠã®HTTPãã°ã§è¿œè·¡ãããŠããŸããŸãã
ãããé²ãã«ã¯POST /accounts/search
ã§ãªã¯ãšã¹ãããã£ã«æ€çŽ¢ãã©ã¡ãŒã¿ãé
眮ããã®ããã£ãšãå®å
šã ãããšããŠããŸãã
ãããªã£ãŠãããšæ€çŽ¢ç³»ã®ãšã³ããã€ã³ãã¯ããããPOSTã«ããããªã£ãŠãããããªæ°ãããŸããRESTçã«ã©ããªãã§ããããã
第9ç« APIã®èšèšãé²åããã
ç Žå£çå€æŽ(ã³ã³ã·ã¥ãŒããã³ãŒããå€æŽããªããšåé¡ãèµ·ããå€æŽ)ãã©ããã£ãŠé¿ãããã«ã€ããŠè¿°ã¹ãããŠããŸãã
现ããå€æŽãåé¡ãããŠããŸãã(ããããã£åã®å€æŽãå¿
é ãããªãã·ã§ãã«ã«ãåã®å€æŽãåæã«å€ãè¿œå ...)ãçµè«ã ããããšãæ°ããèŠçŽ ãè¿œå ãã以å€å
šãŠç Žå£çå€æŽã«ãªããšãã話ã ãšæããŸãã
ãªãã€ã¬ã¯ããããããšã«ãåŠå®çã§ããçç±ã¯ãã¯ã©ã€ã¢ã³ãåŽããªãã€ã¬ã¯ã(301 Moved permanently)ã«åŸãèšå®ãããŠããä¿èšŒããªãã£ããããªã¯ãšã¹ããåæã«è»¢éãããããããªããšã©ãŒã«ãããããå Žåãããããã ããã§ãã
åŒçšãããŠãããã€ã©ã ã®æ³åã§ã¯ä»¥äžã®ããã«ã®ã¹ãããŠããŸãã
APIã«ååãªæ°ã®ãŠãŒã¶ãããã°ãã³ã³ã¹ãã©ã¯ã¿ã§äœãçŽæãããã¯åé¡ã§ã¯ãªããã·ã¹ãã ã®ç®ã«èŠããæ¯ãèãã¯ãã¹ãŠèª°ãã«äŸåããããšã«ãªãã
APIã®ããŒãžã§ãã³ã°
ã»ãã³ãã£ãã¯ããŒãžã§ãã³ã°ã¯APIã®å®è£
ã«ã¯é©ããŠããŠããã³ã³ã·ã¥ãŒãã®èŠç¹ããã¯ç Žå£çãªã¬ãã«ã®æ°å(ã¡ãžã£ãŒããŒãžã§ã³)ã ããéèŠã§ããããšã説æãããŠããŸãã
ãšãããšãããŒãžã§ã³åã¯æ°åã§ããå¿
èŠããªãèªç±ã«æ±ºããããšãã§ããŠã2017-10-19ã®ãããªæ¥ä»ã䜿ãããšãã§ãããšãããŠããŸãã
AWSã®CloudFormationãæ¥ä»ã§ããŒãžã§ã³æå®ããŠããã®ã¯ããããçç±ã ã£ããããªã®ãããããªãã§ãã
第10ç« ãããã¯ãŒã¯å¹çã®ããAPIãèšèšãã
ãããã¯ãŒã¯å¹çã«é¢ãããããã¯ãæ±ããŸãã
Cache-Control
ãETag
ã§ã®cacheã³ã³ããŒã«ã«ã€ããŠã®å
·äœäŸãªãããèŒã£ãŠããŸãã
ããã£ãŠå®éã«ããããšæããšAPIããšãŠãè€éã«ãªãããã ãšæãã®ã§ããã¿ãªããã©ããããŠããã§ãããããã®ãããã®ç¥èŠã¯ãšãŠãæ°ã«ãªãã®ã§ãåç¥ã®æ¹ãããããŸãããæããŠããã ãããã§ãã
ãã£ã«ã¿ãªã³ã°
/accounts/A1/transactions?page=2&size=25
ã®ãããªããŒãžããŒã¹ãªã¯ãšã¹ãã«ã€ããŠ
åååŒã調ã¹ãŠãã§ã«ååŸæžãã©ããããã§ãã¯ããéè€ããŠããããŒã¿ãç¡èŠãã責任ã¯ã³ã³ã·ã¥ãŒãã«ãããš(çãã)åèšããŠãããŠããŸãã
Reactã ãšListã®Itemç³»ã®ã³ã³ããŒãã³ãã®id propertyã«ãªãœãŒã¹ã®IDæž¡ãã°ãšãã«æèããªããŠãããã§ãããã
ãªã¯ãšã¹ãéã§ã®éè€ããŒã¿ã蚱容ã§ããªãå Žåã¯ãã«ãŒãœã«ããŒã¹ã®ããŒãžã³ã°ãææ¡ãããŠããŸãã
AWSãšãã ãšãã®æ¹åŒã§ãããããã ãã®æ¹åŒã§ããšUIäžã®ããŒãžããŒã·ã§ã³?ã§3ããŒãžç®ããããªãååŸãããããããšãã§ããªãããªã®ã§æ©ãŸããã§ãã
ãªã¹ããšè©³çŽ°
ããŒãã«ç³»ã®ã³ã³ããŒãã³ãã«ãªãœãŒã¹ã®äžèŠ§ã衚瀺ããŠãéžæããã詳现ç»é¢ã«é·ç§»ããåºæ¬çãªUIãå®çŸããããšãã«ãªã¹ãã®ã¬ã¹ãã³ã¹ã«ã©ããŸã§æ
å ±ã®ããããšããåé¡ã«ã€ããŠã
æ¬æžã§ã¯ãéåžžã¯åèŠçŽ ã®æŠèŠãæ¶ããããããããªããã°ãªããªããšæ±ºãŸã£ãŠããããã§ã¯ãªããå®å
šãªè¡šçŸãè¿ãã»ããå¹ççãªããšããããšè¿°ã¹ãŠãå
·äœäŸã玹ä»ããŠãããŠããŸãã
éçŽããŒã¿ã®TTLã¯ã¬ã¹ãã³ã¹ã®ããããã£ã®ãã¡æå°ã®TTLã«ãªãã®ã§ãã¬ã¹ãã³ã¹ã§è¿ãæ
å ±ãå¢ããã»ã©ãã£ãã·ã¥ã®å¯èœæ§ã劚ããããšã«ãªããšæ³šæãããŠããŸãã
ãªã¹ãã«ãã¹ãŠã®ãªãœãŒã¹ã®æ
å ±ããã以å€ã©ãããŠã察å¿ã§ããªããŠãŒã¹ã±ãŒã¹ãã§ãŠããŠããŸãã®ã¯é¿ããããªãã®ã§ãã¯ã©ã€ã¢ã³ããèªèº«ã§æ¬²ããæ
å ±ãéžæã§ããããã«ããããªããŸãã
äžã€ã®æ¡ãšããŠãAccept: application/vnd.bankingapi.extended+json
ã®ããã«Accept
ããããŒã§ãªãœãŒã¹ã®æ¬²ããæ
å ±ãã¯ã©ã€ã¢ã³ãããææã§ããä»çµã¿ããããããŠããŸãã(ããã§ã¯ãextended
, summarized
, complete
)
ããã¯æšæºã®ææ³ã§ã¯ãªãå®å
šãªã«ã¹ã¿ã ã¡ãã£ã¢ã¿ã€ãã§ããããã§ãã
ãããŸã§ããããªã£ãããGraphQLãããã®ã§ã¯ãšæã£ãŠãããGraphQLã®äŸã玹ä»ãããŠããŸããã
GraphQLã«ã€ããŠã¯èªåã®2021幎ã®èª²é¡ã§ããReact/FrontåŽã®client libraryã¯å
å®ããŠããã®ã§backendåŽã察å¿ã§ããããã«ããŠFront -> GraphQL -> gRPCãããããããªãããªãšèªåãšããŠã¯èããŠããŸãã
æ¬æžã§ããAPIã¬ã€ã€ãšããæèã§å
·äœçãªãŠãŒã¹ã±ãŒã¹ã«ãããããšã¯ã¹ããªãšã³ã¹APIãšç¹åããŠããªããªãªãžãã«APIã®ãããªã¬ã€ã€ãŒããªèšèšã«ã€ããŠãµããããŠããŸãã
第11ç« ã³ã³ããã¹ãã«åºã¥ããŠAPIãèšèšãã
ãªã¯ãšã¹ãã«å¯ŸããŠããã«ã¬ã¹ãã³ã¹ãè¿ãæå³ã§ã®åæçãªAPI以å€ã«ã€ããŠãµããããŠããŸãã
Web Hook
ã³ã³ã·ã¥ãŒãã«ããŒãªã³ã°ãããã®ã§ã¯ãªããããããç»é²ãããURLã«ãããã€ããéç¥ãããã€ãã³ããPOSTãªã¯ãšã¹ãããWeb Hookã«ã€ããŠèª¬æãããŠããŸãã
ã€ãã³ãããšã«Web Hook URLãçšæããã®ãæ±çšçãªURLãçšæããŠãããã¯ããŒãºã«ãããã軜éã§æ±çšçãªã€ãã³ãããããšãWeb Hookãã²ãšã€çšæããŠããã®ãããæŠç¥ã§ãããšãããŠããŸãã
çç±ã¯ãæ°ããã€ãã³ãã®è¿œå ã容æã§ããããšãšãã€ãã³ãã軜éã«ä¿ã£ãŠããã³ã³ã·ã¥ãŒãã詳现ãååŸããããã«ãããã€ããåŒã³åºãæ¹åŒã®ã»ããã»ãã¥ã¢ã ããã®ããã§ãã
ãŸãããªã¯ãšã¹ãã®æå·åãšçœ²åãmTLSãšãã£ãæ§ã
ãªä¿è·ã玹ä»ãããŠããŸãã(Slackã®Web HookãTLS(https)ãå¿
é ã§ããã)
Web Hookã«ã¯èŠæ Œã¯ãªãããã§ãããWebSubãšããW3Cãçºè¡šããŠããå§åãããããã§ãã
ã€ãã³ãã¹ããªãŒã
ãµãŒãããã¯ã©ã€ã¢ã³ãã«æ
å ±ãéç¥ãããå Žåãèªåã¯WebSocketããç¥ããªãã£ãã®ã§ããSSE(Server-Sent Event)ãšããæ¹æ³ãããããšãç¥ããŸããã
HTTPãããã³ã«ã§ã¬ã¹ãã³ã¹ããŒã¿ã«æ
å ±ãæµãç¶ããããšã§HTTPããŒã¹ã§ã€ãã³ãæ
å ±ãéç¥ã§ããä»çµã¿ã®ããã§ãã(ãã®ãããµãŒã -> ã¯ã©ã€ã¢ã³ãã®äžæ¹åã®ã¿)
è€æ°ã®èŠçŽ ã®åŠç
1APIãªã¯ãšã¹ãã§è€æ°ã®ãªãœãŒã¹ãåŠç(äœæ/æŽæ°)ããå Žåãç¹ã«ãã®ãªãœãŒã¹ã®åŠçã®äžéšã倱æããå Žåã©ã®ãããªã¬ã¹ãã³ã¹ãè¿ãã¹ããã«ã€ããŠã
æ¬æžã§ã¯ãæ£åžžã«åŠçã§ãããã®ã¯ã§ããã ãåŠçããŠãã¹ãŠãšã©ãŒãè¿ãããšãææ¡ããŠããŸãã
ãŸãããã®ããã®ã¹ããŒã¿ã¹ã³ãŒããšããŠ207(Multi-Status)ãå©çšããjsonã®ã¬ã¹ãã³ã¹äŸãèŒããŠãããŠããŸãã
å®éã«207䜿ããã¯æ©ãŸããã§ãããåŠçã®äžéšã倱æããŠãæåç³»ã®ã¹ããŒã¿ã¹ã³ãŒãè¿ããŠåŒã³åºãåŽã«æå/倱æããããããã®ãšã³ããªãŒãè¿ãã®ããããšããã®ã¯ãã®ãšããã ãšæããŸãã
AWSã®Batchç³»ã®APIãæŠããããªæãã§å®è£
ãããŠããŸãã
第12ç« APIãææžåãã
APIã®ããã¥ã¡ã³ãåã«ã€ããŠãOASã§äœæããŠããã°ããã¥ã¡ã³ãäœæã§ããšã³ã·ã¹ãã ã®æ©æµã«ãããããŸãã
èªåã¯æ¬æžã§ã玹ä»ãããŠããReDocãå©çšããŠããŸãã
TwiilioãStripeã®ããã¥ã¡ã³ãããããããããŠããŸããã
ããã¥ã¡ã³ããå®è£ ããçæãã¹ããåŠã
APIããã¥ã¡ã³ããå®è£
(ã³ã¡ã³ããAnnotationãå«ã)ã®ã¿ã«ããšã¥ããŠçæã§ããã°å®è£
ãšããã¥ã¡ã³ãã®åæãä¿ã€ããšãã§ããŸãã
ããããªããæ¬æžã§ã¯ããã€ããã®æ¹æ³ã®æ¬ ç¹ãææãããŠããŸããã
- æ¢åã®ã¢ãããŒã·ã§ã³ãã¬ãŒã ã¯ãŒã¯(å°ãªããšãèè
ãå©çšãããŠããã®)ã§ã¯APIèšè¿°ãã©ãŒããããçŽæ¥æäœãããšãã®ãããªæè»æ§ã¯åŸãããªãã
(æ±çšçãªããŒã¿æ§é ã®äŸãã³ã³ããã¹ãã«ããããã) - ããã¥ã¡ã³ããä¿®æ£ããããã«å®è³ªçã«ã³ãŒãã®å€æŽãå¿ èŠã«ãªãã
- ããã¥ã¡ã³ãçæã®ããã«æ©ã段éããã³ãŒããå®éã«æžããªããã°ãªããªãã
çµè«ãšããŠã¯ã©ã®æ¹æ³ããããæªããã§ã¯ãªããèšèšè ãšçµç¹ã«ãšã£ãŠããŸãããæ¹æ³ãéžæããå¿ èŠããããšã®ããšã§ããã
第13ç« APIãæé·ããã
6ç« ã®APIã§ã®äžè²«æ§ã§ããµããããŠããçµç¹ã®APIã¬ã€ãã©ã€ã³ã®éèŠæ§ã説æãããŠããŸãã
ããã§ããã¬ã€ãã©ã€ã³ãšã¯ãèšèšè
å
šå¡ãåŸãã«ãŒã«ãéãããã®ãšå®çŸ©ãããŠããŸãã
äœã£ãŠçµããã§ã¯ãªãå€æŽ(ãããŠå»æ¢ã«ã)ååãã§ããããšæ³šæãä¿ãããŠããŸãã
(ã¬ããã³ã¹ã®æé»é¢ãšããAPIèŠå¯ãšããè¡šçŸããããããã£ãã§ã)
Webé¢é£ã®RFCããŸãšãŸã£ãŠããWeb Conceptsãšãããµã€ãã玹ä»ãããŠããéåžžã«åèã«ãªããŸããã
äŸãã°[HTTP Headerããšã«é¢é£ããRFC](http://webconcepts.info/concepts/http-header/ãã®ã£ãŠãããããŸãã
ã¬ãã¥ãŒæã®ãã§ãã¯ãªã¹ãã®é ç®ãåèã«ãªããŸãããããŒã (çµç¹)ã§äœã£ãŠãããšããããã ãªãšæããŸããã
ãããã«
ãããããå®è£
ããã®è©±ãèªã¿ãã人ã«ã¯Real World HTTPããªã¹ã¹ã¡ã§ãã
èªåã¯ãŸã 第ïŒçèªããŠããŸãããã