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

EC新サービスにおけるスキーマファースト開発 / Schema First Development in the New EC Service

EC新サービスにおけるスキーマファースト開発 / Schema First Development in the New EC Service

EC Tech MTG #4(GMOペパボEC事業部の社内勉強会)のための資料です

Kōhei Yamamoto

October 24, 2017
Tweet

More Decks by Kōhei Yamamoto

Other Decks in Technology

Transcript

  1. !LZNNU(.01FQBCP *OD
    &$5FDI.5(
    EC৽αʔϏεʹ͓͚Δ
    εΩʔϚϑΝʔετ։ൃ

    View full-size slide

  2. ࿩͢͜ͱ
    wεΩʔϚϑΝʔετ։ൃͱ͍͏Web API࢓༷ఆٛΛ

    த৺ʹਾ͑ͨΞϓϦέʔγϣϯ։ൃͷ֓ཁ
    wεΩʔϚϑΝʔετ։ൃΛ࣮ݱ͢ΔͨΊʹඞཁͳಓ۩
    wEC৽αʔϏεͰͲͷΑ͏ʹεΩʔϚϑΝʔετ։ൃ
    Λ࣮ࢪ͍ͯ͠Δ͔

    View full-size slide

  3. εΩʔϚϑΝʔετ։ൃͱ͸

    View full-size slide

  4. εΩʔϚϑΝʔετ։ൃͱ͸
    wJSONΛฦ͢Web API͕ଘࡏ͢ΔΞϓϦέʔγϣϯͷ։ൃ͕ର৅
    wSPA (Single Page App)΍ϞόΠϧɺAPI୯ಠͰ΋
    wAPI࢓༷ʢεΩʔϚʣΛఆ͔ٛͯ͠ΒεΩʔϚΛ΋ͱʹ։ൃ͍ͯ͘͠
    εΩʔϚͷओͳߏ੒ཁૉ
    URL
    HTTPϝιου ϦΫΤετύϥϝʔλ
    εςʔλείʔυ ϨεϙϯεϘσΟ

    View full-size slide

  5. εΩʔϚϑΝʔετ։ൃͷϑϩʔ
    εΩʔϚఆٛ
    ϑϩϯτΤϯυ
    ։ൃ&ςετ
    API
    ։ൃ&ςετ
    ݁߹ςετ

    View full-size slide

  6. ·ͣҰॹʹ૬ஊ͠ͳ͕ΒεΩʔϚΛఆٛͯ͠ɺ
    εΩʔϚΛ΋ͱʹύϥϨϧʹ࣮૷ɻ݁߹ͯ͠
    ΋΄ͱΜͲෆ۩߹͸ݟ౰ͨΒͳ͍ͷͰͦͷ·
    ·ϦϦʔεɻ
    ͱͳΓɺೝࣝҧ͍΍݁߹࣌ͷτϥϒϧ͕গͳ
    ͘ͳΔͷͰՃ଎͕ظ଴Ͱ͖·͢ɻ
    εΩʔϚϑΝʔετ։ൃͷεεϝonk.ninja
    https://blog.onk.ninja/2017/09/21/schema_first_development

    View full-size slide

  7. εΩʔϚϑΝʔετ։ൃͷར఺
    w։ൃऀؒͰεΩʔϚʹؔ͢ΔೝࣝΛૣظʹҰக
    wΠϯλϑΣʔε͕ܾ·͔ͬͯΒ։ൃ͢ΔͷͰɺ

    ϑϩϯτΤϯυͱAPIͷ݁߹͕੒ޭ͠΍͍͢
    wఆٛͨ͠εΩʔϚࣗମΛ׆༻ͨ͠։ൃͷޮ཰Խ͕Մೳ

    View full-size slide

  8. εΩʔϚϑΝʔετ։ൃʹ
    ඞཁͳπʔϧ

    View full-size slide

  9. εΩʔϚϑΝʔετ։ൃͰ΍Δ͜ͱ
    wAPIͷεΩʔϚΛఆٛ
    wεΩʔϚΛ΋ͱʹΞϓϦέʔγϣϯΛ࣮૷
    wΫϥΠΞϯτ͕ଘࡏ͢Δͱ͖͸ฒߦ։ൃ
    wαʔό͸APIͷεΩʔϚʹ४ڌ࣮ͨ͠૷ʹ͢Δ

    View full-size slide

  10. wAPIͷεΩʔϚΛఆٛ
    wεΩʔϚΛ΋ͱʹΞϓϦέʔγϣϯΛ࣮૷
    wΫϥΠΞϯτ͕ଘࡏ͢Δͱ͖͸ฒߦ։ൃ
    wαʔό͸APIͷεΩʔϚʹ४ڌ࣮ͨ͠૷ʹ͢Δ
    εΩʔϚϑΝʔετ։ൃʹඞཁͳπʔϧ
    OpenAPI & Swagger tools
    Committee & RSpec
    EC৽αʔϏε΋͜ΕΒͷπʔϧͰ

    εΩʔϚϑΝʔετ։ൃͷ؀ڥΛ੔උ͍ͯͬͨ͠

    View full-size slide

  11. OpenAPI & Swagger tools

    View full-size slide

  12. OpenAPI
    wJSON/YAMLͰهड़Ͱ͖ΔRESTful API࢓༷هड़ͷͨΊͷ࢓༷
    w࢓༷ʹ͕ͨͬͯ͠ॻ͍ͨ΋ͷΛOpenAPIυΩϡϝϯτͱݺͿ

    w2017೥7݄ʹv3.0.0͕ϦϦʔε
    wରԠ͍ͯ͠Δπʔϧ͕গͳ͍ͷͰɺ΋͏͠͹Β͘͸v2.0Ͱ

    ਖ਼ࣜʹ͸v3.0.0͔Β

    View full-size slide

  13. OpenAPIυΩϡϝϯτͷྫʢൈਮʣ
    definitions:
    Shipment:
    type: object
    properties:
    id:
    type: integer
    example: 1
    shipped_at:
    type:
    - string
    - "null"
    format: date-time
    example: 2017-07-10T12:00:00Z
    paths:
    /admin/shipments/{shipmentId}:
    get:
    parameters:
    - shipmentId:
    in: path
    name: shipmentId
    type: integer
    required: true
    responses:
    '200':
    schema:
    type: object
    properties:
    shipment:
    $ref: '#/definitions/Shipment'
    ˡΤϯυϙΠϯτͷఆٛ
    ˣϨεϙϯεߏ଄ͷఆٛ

    View full-size slide

  14. Swagger tools
    • Swagger UI / Swagger Editor
    wOpenAPIυΩϡϝϯτ͔Βϒϥ΢βͰݟ΍͍͢υΩϡϝϯτΛੜ੒
    • Swagger Codegen
    wOpenAPIυΩϡϝϯτ͔Β֤छݴޠͰॻ͔ΕͨΫϥΠΞϯτͱ

    ελϒαʔόͷίʔυΛੜ੒
    wࠓճ͸ελϒαʔόΛੜ੒࣮ͯ͠API։ൃͱฒߦͨ͠ΫϥΠΞϯτ

    ։ൃΛ࣮ࢪ

    View full-size slide

  15. Committee & RSpec

    View full-size slide

  16. લఏʛAPIυΩϡϝϯτͷ৴པੑ୲อ
    wAPIͷυΩϡϝϯτͷ࣮૷ͱͷဃ཭Λ๷ࢭͯ͠৴པੑΛ୲อ͍ͨ͠
    wϑϩϯτʗ"1*݁߹࣌ͷෆ۩߹Λ͞ΒʹݮΒ͢
    wAPIΛ࢖͏ਓɺকདྷ։ൃʹՃΘΔਓɺະདྷͷࣗ෼͕ͨͪॿ͔Δ
    wOpenAPI͕͋Ε͹εΩʔϚΛར༻ͨ͠APIͷςετ͕Մೳ
    w࣮ࡍͷϨεϙϯεͱεΩʔϚ͕Ұக͢Δ͔νΣοΫ͢Ε͹Α͍
    wͰ͖Δ͚ͩػցతʹରࡦָ͕ͯ͠Ͱ͖ΔͱΑ͍

    View full-size slide

  17. Committeehttps://github.com/interagent/committee
    wJSON SchemaΛར༻ͨ͠APIαʔόͷͨΊͷRack Middleware܈
    wϦΫΤετʗϨεϙϯεJSONͷߏ଄όϦσʔγϣϯ
    wελϒαʔό
    wςετͰར༻Ͱ͖ΔϨεϙϯεߏ଄όϦσʔγϣϯ༻ͷϝιου

    View full-size slide

  18. JSON Schemahttp://json-schema.org
    w ͋ΔJSONͷߏ଄Λنఆ͢Δʢͭ·ΓεΩʔϚʣJSON
    type: object
    properties:
    id:
    type: string
    email:
    type: string
    role:
    type: array
    items:
    type: string
    enum:
    - admin
    - normal
    // όϦσʔγϣϯOK
    {
    "id": "1",
    "email": "[email protected]",
    "role": "normal"
    }
    // όϦσʔγϣϯNGʢid͕integerʣ
    {
    "id": 1
    "email": "[email protected]"
    }
    id, email, roleΛଐੑʹ࣋ͭJSON Schema JSONʹର͢ΔόϦσʔγϣϯ͕Մೳ

    View full-size slide

  19. JSON Schemaͷ׆༻
    wOpenAPIυΩϡϝϯτͷϨεϙϯεهड़͸JSON SchemaΛར༻Մೳ
    wCommittee

    wJSON SchemaΛར༻ͯ͠όϦσʔγϣϯΛ࣮ߦ
    wOpenAPI v2.0ΛಡΈࠐΜͰɺͦΕͧΕͷΤϯυϙΠϯτʹରԠ͢Δ
    ϨεϙϯεΛೝࣝՄೳ
    w0QFO"1*υΩϡϝϯτʹॻ͍ͨεΩʔϚͰAPIͷςετ͕Ͱ͖Δʂ

    View full-size slide

  20. Committeeͷબఆཧ༝
    wର߅അ͸Apivoreͱ͍͏RSpecͱ౷߹ͨ͠JSONͷόϦσʔγϣϯ༻
    gem https://github.com/westfieldlabs/apivore

    wRSpecͱີ݁߹ͳײ͡ςετίʔυͷϝϯς͕΍΍ͭΒͦ͏
    • Committee

    w֤Rack middleware͕ૄͳ࡞Γʹͳ͍ͬͯͯಋೖ͠΍͍͢
    wίʔυ͕ಡΈ΍͘͢HerokuϓϩμΫτͱ͍͏҆৺ײ

    View full-size slide

  21. ςετ΁ͷ૊ΈࠐΈ
    wAPIͷςετͰ͸RSpecͷrequest specΛॻ͘
    wrequest specΛॻ͍ͨΒউखʹJSON SchemaͰόϦσʔγϣϯͯ͠
    ΄͍͠
    describe 'GET /shipments/:id' do
    it 'returns 200 OK' do
    get "/shipments/#{shipment.id}"
    expect(response.status).to eq 200
    end
    end

    View full-size slide

  22. request spec࣮ߦ࣌ͷόϦσʔγϣϯ
    describe 'GET /shipments/:id' do
    it 'returns 200 OK' do
    get "/shipments/#{shipment.id}"
    end
    end
    HTTPϦΫΤετΛൃߦͨ͋͠ͱʹϨεϙϯεͷJSONΛCommitteeͰ

    νΣοΫ assert_schema_conform
    ͢ΔΑ͏ʹॲཧࠩ͠ସ͑
    ։ൃ࣌ʹεΩʔϚʹΑΔνΣοΫΛҙࣝ͠ͳ͍ͰΑ͘ͳͬͨʂ
    # ActionDispatch::Integration::Sessionʹprepend
    def process(*args)
    super *args
    assert_schema_conform
    end
    ˞งғؾͳͷͰৄࡉ͸ϒϩάݟͯͶ
    http://blog.kymmt.com/entry/auto-json-validation-with-committee

    View full-size slide

  23. όϦσʔγϣϯࣦഊ࣌ͷྫ
    1) Users GET /users/:id conforms to the schema
    Failure/Error: assert_schema_conform
    Committee::InvalidResponse:
    Invalid response.
    #: failed schema #/properties//users/{userId}/properties/GET: "email" wasn't supplied.
    request specΛॻ͍͓͚ͯ͹ɺOpenAPIυΩϡϝϯτͷϨεϙϯεεΩʔϚͱ

    ࣮ࡍͷϨεϙϯεJSON͕৯͍ҧ͏ͱ͖ʹࢦఠͯ͘͠ΕΔΑ͏ʹͳͬͨ

    View full-size slide

  24. εΩʔϚϑΝʔετ։ൃͷ࣮ྫ

    View full-size slide

  25. ʮεΩʔϚϑΝʔετ։ൃͷ࣮ྫʯͷਅ࣮
    w࣮͸ʮεΩʔϚϑΝʔετ։ൃ΍Δͧʂʂʯͱ͔ݴͬͨ͜ͱͳ͍
    wOpenAPIΛಋೖ͠ͳ͕ΒΈΜͳͰ΍͍ͬͯͨΒͦΕͬΆ͘

    ͳ͍ͬͯͨ
    wRubyKaigi 2017Ͱ֓೦ʹ໊લΛ͚ͭͯ΋Βͬͨײ͡
    wref: API Development in 2017 - RubyKaigi 2017

    http://rubykaigi.org/2017/presentations/onk.html

    View full-size slide

  26. ৽αʔϏε։ൃ࣌ͷྲྀΕ
    APIఆٛ
    εʔύʔ
    ϨϏϡʔ
    ։ൃͱ
    ςετ
    ݁߹

    View full-size slide

  27. APIఆٛ
    wඞཁͳػೳͷཁ݅ʹج͍ͮͯAPIΛఆٛ
    wYAMLͰॻ͘ͱOpenAPIυΩϡϝϯτΛੜ੒Ͱ͖Δ

    ߏ੒
    wϑϩϯτʗAPIͲͪΒͷ୲౰Ͱ΋ඞཁͳΒ୭͔͕ॻ͘

    View full-size slide

  28. εʔύʔϨϏϡʔ
    wAPIఆ͕ٛॻ͚ͨΒSlackͰ੠͔͚ͯσεΫ·ΘΓͰಥൃϨϏϡʔ
    wϨϏϡʔϙΠϯτ
    wURLɺϦιʔεͷ੾Γ͔ͨɺ໋໊ɺύϥϝʔλʗϨεϙϯεଐੑͷ
    աෆ଍ɺεςʔλείʔυͳͲ
    w͸͡Ίʹٞ࿦͢Δ͜ͱͰ࢓༷͕಄ͷதͰ੔ཧ͞ΕΔޮՌ΋͋Γ

    View full-size slide

  29. ։ൃͱςετ
    wAPI͸APIఆٛΛ΋ͱʹ࣮૷
    wϑϩϯτΤϯυ͸Swagger CodegenͰੜ੒ͨ͠ελϒαʔόΛ

    ࢖ͬͯAPIͱฒߦ࡞ۀͰ࣮૷
    wCommitteeʹΑΔςετ͸࠷ۙೖΕͨͷͰ·ͩͦΕ΄Ͳ׆༂ͯ͠

    ͳ͍ʜʜ
    wࠓޙʹظ଴

    View full-size slide

  30. ஫ҙ఺ʛϨεϙϯεͷJSON
    wಉ͡ϦιʔεΛද͢JSONΦϒδΣΫτʹ͍ͭͯ͸APIશମͰߏ଄Λἧ͑ͯ
    ͍͘͜ͱΛઃܭ࣌ʗϨϏϡʔ࣌ʹҙࣝͨ͠΄͏͕Α͍
    waddressͱ͍͏JSONΦϒδΣΫτ͕৔ॴʹΑͬͯnameʢडऔਓࢦ໊ʣ
    Λ͍࣋ͬͯͨΓ͍࣋ͬͯͳ͔ͬͨΓʜʜ
    w΋͘͠͸࢓૊Έͱ੍ͯ͠໿ΛՃ͑Δ
    w͋ΔϦιʔεதͷωετͨ͠ΦϒδΣΫτΛඞͣผͷϦιʔεʹ͢Δ
    wref: RESTful Web APIΛݫີͳϦιʔεࢦ޲ʹ͢Δ- Qiita

    https://qiita.com/qsona/items/f7ef78ddbbc2873d1d13

    View full-size slide

  31. ·ͱΊ
    wεΩʔϚϑΝʔετ։ൃΛ༻͍ͨΞϓϦέʔγϣϯ։ൃΛ঺հ
    wOpenAPI͕޿·ͬͯAPIεΩʔϚ·ΘΓͷࣄྫ΍ఆ൪πʔϧ͕ἧͬͯ
    ͖ͨͷ͕ॏཁ
    w͍·ͦ͜εΩʔϚϑΝʔετ։ൃΛҙࣝ͢ΔͱΑ͍ͷͰ͸ͳ͍͔

    View full-size slide