Upgrade to Pro — share decks privately, control downloads, hide ads and more …

api-design-practice.pdf

3925ee6eaa41031bac799de0f4f528ec?s=47 to4iki
September 07, 2014

 api-design-practice.pdf

3925ee6eaa41031bac799de0f4f528ec?s=128

to4iki

September 07, 2014
Tweet

Transcript

  1. API ઃܭͰ஫ҙ͍ͯ͠Δࣄ toshiki takezawa @to4iki

  2. None
  3. ࠓ͔Βͷ಺༰͸ ͋͘·Ͱ։ൃऀࢹ఺Ͱ͢ APIΛ࡞Δࢹ఺Ͱ

  4. લఏ REST ~.pl ~.do ~servletͱ͔ ಛఆݴޠʹґଘ͍ͯ͠Δ͔ΒγεςϜมߋ͠ ʹ͍͘ɻ FW(struts → rails)ͱ͔ม͑ͨΒURLมΘΔ

    ΑͶ ! ɾwebΛࢧ͑Δٕज़ಡ΋͏
  5. 1. ಈࢺͰ͸ͳ໊͘ࢺ ɾϦιʔεΛ໊ࢺͰද͢ collectionͰ υϝΠϯʹΑͬͯ͸ಈࢺ΋͋Γ /convert?from=EUR&to=CNY&amount=100` ɾHTTP ϝιουΛϦιʔεͷૢ࡞ͱ͢Δ get, post,

    put, delete ɾ֊૚͸ͳΔ͚ͩઙ͘อͭɺγϯϓϧʹ /users/5678/dogs /resource/identifier/resource ͕໨҆ ɾURLʹϝιουΛؚΊͳ͍ /users/show/123ͱ͔ → /users/123 ɾෳࡶ͞͸ΫΤϦύϥϝʔλʹԡ͠ࠐΉ /users?q=hoge&offset=50&limit=30
  6. 2. ΤϥʔϋϯυϦϯά ɾεςʔλείʔυΛద੾ʹ࢖༻͢Δ 200: success 400൪୅: client error 404: not

    found 409 conflict 500: server error
  7. 3. όʔδϣχϯά ɾAPI ͷϨεϙϯεΛϋϯυϧͯ͠ϩδοΫ͕มΘΔ ৔߹, URL ʹ͍ΕΔ ͔ͯɺೖΕͳ͍ͱޓ׵ੑ੾ͬͨมߋ͘Θ͑ͨͱ͖ࢮ ͵ /api/1.0/dogs

    /api/1.1/dogs ! AcceptsϔομʔͰόʔδϣχϯάͯ͠΋
  8. 4. ϦιʔεͷUUIDΛఏڙ͢Δ “uuid":"39D3FA7E-­‐29F5-­‐4406-­‐9B54-­‐AE70FA3CBF87"   ! sequenceͰ΋͍͍ͱࢥ͏͚Ͳ ! mysqlͳΒ SELECT UUID()

    AS u ! postgre͸σϑΥϧτରԠͯ͠ͳ͍͔Β uuid-ossp͍ΕͯͶ
  9. 5. ϦιʔεͷtimestampΛఏڙ͢Δ {      "createTime":1406391453253,      "modifyTime":1406421657000  

    }
  10. 6. ֎෦Ωʔͷࢀষ͸ωετ͢Δ    "name":"fute",      "ownerId":  “13524-­‐...",   !

    Ͱ͸ͳ͘   !    "name":"fute",      "owner":  {          "id":  "13524-­‐...",      }   Ϩεϙϯεͷߏ଄Λมߋͨ͠ΓɺτοϓϨϕϧͷϑΟʔ ϧυΛ௥Ճ͢Δࣄͳ͘ɺؔ࿈৘ใΛؚΊΔࣄ͕Ͱ͖Δ
  11. 7. Ϧιʔεࢀরͷalias༻ҙ͠ͱ͘ ɾid /users/:id ɾname /users/:name ɾmail /users/:mail

  12. ଞʹ΋ ɾAPIΞΫηε͸SSLඞਢʹ͍͔ͨ͠΋ ɾdocumentॆ࣮ͤ͞Α͏ ɾEtagͰΩϟογϡαϙʔτͱ͔ (ΫϥΠΞϯτͷif-none-matchϔομͰ cache࠷৽͔νΣοΫͱ͔)

  13. ͓ΘΓ