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

プライベートAPIのクライアントをいい感じにしたい / pepabo_ec_tech_mtg02

プライベートAPIのクライアントをいい感じにしたい / pepabo_ec_tech_mtg02

2017/05/16 第2回 EC事業部 Tech MTG

B54cc1be9e7ea6e3ff17eaf995fbe35f?s=128

Akihito Nakano

May 16, 2017
Tweet

Transcript

  1. த໺ڿਓ(.01&1"#0JOD ୈճ&$ࣄۀ෦5FDI.5( ϓϥΠϕʔτ"1*ͷΫϥΠΞϯτΛ ͍͍ײ͡ʹ͍ͨ͠

  2. 8&#ΞϓϦέʔγϣϯΤϯδχΞ &$ࣄۀ෦Χϥʔϛʔγϣοϓάϧʔϓ த໺ڿਓ ͖͋ͬʔ !/","/0@"LJIJUP ಛ߈ୂνʔϜ IUUQTBDLJOUPTIHJUIVCJP

  3. ໨࣍ wղܾ͍ͨ͜͠ͱ wཧ૝ w࣮ݱ·Ͱͷϋʔυϧ wϨϏϡʔґཔ

  4. ղܾ͍ͨ͜͠ͱ

  5. ΧΦεԽ͖͍ͯͯ͠Δ

  6. ϓϥΠϕʔτ"1*ΫϥΠΞϯτͷݱঢ় w ࣮૷ʹ౷Ұײ͕ͳ͍ w +40/Λσίʔυ͚ͨͩ͠ͷσʔλΛฦ͢ͷ΋͋Ε͹ɺϞσϧ૬౰ͷΠϯελϯεΛฦ͢ͷ ΋͋Δ w ෳ਺ͷϦϙδτϦʹ෼ࢄ࣮ͯ͠૷͞Ε͍ͯΔ w ͦΕͧΕʹ"1*Λୟͨ͘Ίͷ਌ɾࢠΫϥε͕ଘࡏ͢Δ

    w ผͷϦϙδτϦͷ਌ΫϥεΛܧঝͯ͠Δͷ΋͋Δ w Ϣχοτςετͷϝϯςφϯείετ w "1*ͷ࢓༷Λม͑ͨΒQIQWDSͷϑΟΫενϟ΋ਓؒͷखͰमਖ਼͠ͳ͍ͱ͍͚ͳ͍ w ಉ͡Α͏ͳςετ͕ͨ͘͞Μ͋Δ
  7. ΧΦεԽ͸·ͩ࢝·ͬͨ͹͔Γ ͍·ͷ͏ͪʹखΛଧ͍ͪͨ

  8. ཧ૝

  9. 4XBHHFS$PEFHFO

  10. 4XBHHFS$PEF(FOFSBUPS wIUUQTXBHHFSJPTXBHHFSDPEFHFO w0QFO"1*4XBHHFSʹԊͬͨఆ͔ٛΒ৭ʑͳݴޠɾϑϨʔϜϫʔΫͷαʔ όʔɺΫϥΠΞϯτɺςετ΍ελϒΛੜ੒Ͱ͖Δ w࠷৽͸W wW ͦΖͦΖϦϦʔε ͔Β͸(V[[MF 143 Λ)551ΫϥΠΞϯτͱͯ͠

    ར༻͍ͯ͠Δ w1)1Ҏ্
  11. ΠϯετʔϧʙΫϥΠΞϯτੜ੒ $ brew install swagger-codegen $ swagger-codegen generate \ -i

    http://petstore.swagger.io/v2/swagger.json \ -l php \ -o . ಛఆͷόʔδϣϯΛ࢖͍͍ͨ৔߹͸ $ git clone https://github.com/swagger-api/swagger-codegen $ ./run-in-docker.sh mvn package $ ./run-in-docker.sh generate \ -i http://petstore.swagger.io/v2/swagger.json \ -l php \ -o .
  12. ੜ੒͞ΕΔϑΝΠϧ WҰ෦ൈਮ ├── lib │ ├── Api │ │ ├──

    PetApi.php │ │ ├── StoreApi.php │ │ └── UserApi.php │ ├── ApiClient.php │ ├── Model │ │ ├── ApiResponse.php │ │ ├── Category.php │ │ ├── Order.php │ │ ├── Pet.php │ │ ├── Tag.php │ │ └── User.php
  13. ϖοτετΞͷTXBHHFSKTPO { "paths": { "/pet/{petId}": { "get": { "operationId": “getPetById",

    …… "definitions": { "Pet": { "type": "object", "properties": { "category": { "$ref": "#/definitions/Category" },
  14. QFU\QFU*E^ͷϨεϙϯε { "id": 1, "category": { "id": 1, "name": "string"

    }, "name": "doggie", "photoUrls": [ "string" ], "tags": [ { "id": 1,
  15. ΫϥΠΞϯτͷ࢖͍৺஍ (new \Swagger\Client\Api\PetApi)->getPetById(1); class Swagger\Client\Model\Pet#9 (1) { protected $container =>

    array(6) { 'id' => int(1) 'category' => class Swagger\Client\Model\Category#10 (1) {
  16. ΫϥΠΞϯτͷ࢖͍৺஍ (new \Swagger\Client\Api\PetApi)->findPetsByStatus('sold') array(1424) { [0] => class Swagger\Client\Model\Pet#4239 (1)

    { } [1] => class Swagger\Client\Model\Pet#4243 (1) {
  17. ࣮ઓ౤ೖΛ૝ఆͨ͠՝୊ wϞσϧʹ࣮૷ΛՃ͍͑ͨ w$JSDVJU#SFBLFSΛ࢖͍͍ͨ w4XBHHFS$PEFHFO $JSDVJU#SFBLFS (BOFTIB  w IUUQTBDLJOUPTIHJUIVCJPCMPHTXBHHFSDPEFHFOXJUIHBOFTIB

  18. ϋʔυϧ

  19. ࣮ݱ·Ͱͷϋʔυϧ w 0QFO"1*Ͱఆٛ͢Δ w Ұؾʹ΍Δඞཁ͸ແ͍ͷͰ౎౓΍ͬͯ͘ w ϩʔΧϧ؀ڥͷվળ w 13͕͋ΔͷͰؤுΔʢՃ੎͓ئ͍͠·͢QSBZʣ w

    ࣮ઓ౤ೖΛ૝ఆͨ͠՝୊ w .PEFMʹ࣮૷ΛՃ͍͑ͨ৔߹͸Ͳ͏͢Δʁ w $JSDVJU#SFBLFSΛ࢖͍͍ͨ৔߹͸Ͳ͏͢Δʁ w ӡ༻Ͳ͏͢Δʁ ࣍ͷεϥΠυ΁
  20. ίʔυࣗಈੜ੒ͷϓϥΫςΟε w(FOFSBUJPO(BQύλʔϯ wIUUQTNBSUJOGPXMFSDPNETM$BUBMPHHFOFSBUJPO(BQIUNM wIUUQXXXIZVLJDPNEQEQJOGPIUNM(FOFSBUJPO(BQ wlॻ͖Ճ͑ͯͨ෦෼͕ফ͑ͯΔʜzΛ๷͙ wࣗಈੜ੒͞Εͨίʔυͱਓ͕ؒॻ͍ͨίʔυΛ෼཭͢Δ wੜ੒͞ΕͨίʔυͷαϒΫϥεʹਓ͕࣮ؒ૷ΛՃ͑Δ

  21. .PEFMʹ࣮૷ΛՃ͍͑ͨ৔߹ͬͯʁ ࣮ྫͱͯ͠ɺطଘͷ.PEFMͷ࣮૷ΛݟͯΈΔ

  22. (FOFSBUJPO(BQύλʔϯΛద༻͍͕ͨ͠ʜ (new \Swagger\Client\Api\PetApi)->getPetById(1); // class Swagger\Client\Model\Pet w1FUϞσϧʹखΛೖΕ͍ͨ w1FU"QJͷฦΓ஋͕1FUϞσϧ w1FUͷαϒΫϥεΛར༻͢Δ༨஍͕ແ͍ʜ

  23. ࣮ઓ౤ೖΛ૝ఆͨ͠՝୊  .PEFMʹ࣮૷ΛՃ͍͑ͨ wΦϦδφϧςϯϓϨʔτͱϑΝΫτϦΛ࢖͏ wΦϦδφϧςϯϓϨʔτ wਓ͕ؒॻ͘αϒΫϥεΛར༻͢ΔͨΊͷԼ஍Λ࡞Δ wϑΝΫτϦ w"1*Ϋϥεͷੜ੒Λ୲౰͢Δ w"1*Ϋϥε͕ฦ͢΂͖ϞσϧΛ؅ཧͤ͞Δ

  24. .PEFMʹ࣮૷ΛՃ͍͑ͨΦϦδφϧςϯϓϨʔτ class PetApi { public function __construct( ClientInterface $client =

    null, Configuration $config = null, HeaderSelector $selector = null, $modelClass = null w ಠࣗͷίʔυੜ੒ςϯϓϨʔτΛ༻ҙͯ͠ੜ੒͞ΕΔίʔυΛΧελϚΠζ͢Δ w TXBHHFSDPEFHFOHFOFSBUFͷUΦϓγϣϯ w ࣮ߦ݁Ռͱͯ͠ฦ͢ϞσϧΫϥεΛࠩ͠ସ͑ΒΕΔΑ͏ʹ͢ΔͨΊʹ࠷খݶͷΧ ελϚΠζΛ͢Δ
  25. .PEFMʹ࣮૷ΛՃ͍͑ͨΦϦδφϧςϯϓϨʔτ Πϝʔδ

  26. .PEFMʹ࣮૷ΛՃ͍͑ͨϑΝΫτϦ w ࣮ߦ݁Ռͱͯ͠ฦ͢΂͖ϞσϧΫϥεΛ؅ཧͤ͞Δ ApiFactory::instantiate(‘PetApi’)->getPetById(1); // class ……\Model\HandWrittenPet class ApiFactory {

    private static $map = [‘PetApi’ => ‘HandWrittenPet’]; public static function instantiate($apiClass) { …… return new $apiClass(… , $modelClass);
  27. .PEFMʹ࣮૷ΛՃ͍͑ͨϑΝΫτϦ Πϝʔδ

  28. .PEFMʹ࣮૷ΛՃ͍͑ͨ ApiFactory::instantiate(‘PetApi’)->getPetById(1); // class Swagger\Client\Model\HandWrittenPet (new \Swagger\Client\Api\PetApi)->getPetById(1); // class Swagger\Client\Model\Pet

    ϏϑΥʔ Ξϑλʔ ΦϦδφϧςϯϓϨʔτ ϑΝΫτϦͰ (FOFSBUJPO(BQύλʔϯΛద༻༷ͨ͠ࢠ
  29. ࣮ઓ౤ೖΛ૝ఆͨ͠՝୊  $JSDVJU#SFBLFSΛ࢖͍͍ͨ wϑΝΫτϦΛ࢖͏ w"1*ΫϥεΛ࡞Δͱ͖ʹɺ$JSDVJU#SFBLFSΛ૊ΈࠐΜͩ)551ΫϥΠΞ ϯτΛηοτ͢Δ

  30. $JSDVJU#SFBLFSΛ࢖͍͍ͨ class ApiFactory { public static function instantiate($apiClass) { ……

    return new $apiClass($httpClientWithCB, … , $modelClass); w༧Ί$JSDVJU#SFBLFSΛ૊ΈࠐΜͩΫϥΠΞϯτ͕ηοτ͞Εͨ"1*Πϯελ ϯεΛฦ͢ w IUUQTBDLJOUPTIHJUIVCJPCMPHTXBHHFSDPEFHFOXJUIHBOFTIB w "QJ$MJFOUΛܧঝͨ͠ΫϥεΛ༻ҙ͢Δํ๏
  31. $JSDVJU#SFBLFSΛ࢖͍͍ͨ Πϝʔδ

  32. ӡ༻ w ϓϥΠϕʔτ"1*ΫϥΠΞϯτઐ༻ͷϦϙδτϦΛ࡞Δ w ཁٻ͞ΕΔ1)1όʔδϣϯ͕ҧ͏ͷͰڞ༗ϦϙδτϦʹ͸ೖΕΒΕͳ͍ w Ϣχοτςετ w ਓ͕ؒॻ͘ίʔυ͚ͩςετΛॻ͚͹ྑ͍ͷͰ͸ w

    "1*ఆٛͱੜ੒͞ΕΔίʔυ͸ඞͣҰக͢Δ w "1*ͷৼΔ෣͍͸"1*ଆͷςετ͕อূ͢Δ w ։ൃͷྲྀΕ w "1*Λ࣮૷͢Δʗ0QFO"1*ͷఆٛΛॻ͘ w ΫϥΠΞϯτΛੜ੒͢ΔʗαϒΫϥεͷ࣮૷͕ඞཁͳΒ΍Δ ςετΛॻ͘
  33. ݱঢ়ˠ4XBHHFS$PEFHFOΛ࢖͏ͱ w࣮૷ʹ౷Ұײ͕ͳ͍ wੜ੒͞ΕΔίʔυͰ଍৔͕੔උ͞Εɺ౷Ұײ͕ͰΔ wෳ਺ͷϦϙδτϦʹ෼ࢄ࣮ͯ͠૷͞Ε͍ͯΔ wઐ༻ͷϦϙδτϦʹ౷Ұ wϢχοτςετͷϝϯςφϯείετ wਓ͕ؒॻ͍ͨίʔυ͚ͩςετ͢Ε͹ྑ͍

  34. ϨϏϡʔ͓ئ͍͠·͢