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

Akihito Nakano

May 16, 2017
Tweet

More Decks by Akihito Nakano

Other Decks in Programming

Transcript

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

    w ผͷϦϙδτϦͷ਌ΫϥεΛܧঝͯ͠Δͷ΋͋Δ w Ϣχοτςετͷϝϯςφϯείετ w "1*ͷ࢓༷Λม͑ͨΒQIQWDSͷϑΟΫενϟ΋ਓؒͷखͰमਖ਼͠ͳ͍ͱ͍͚ͳ͍ w ಉ͡Α͏ͳςετ͕ͨ͘͞Μ͋Δ
  2. ΠϯετʔϧʙΫϥΠΞϯτੜ੒ $ 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 .
  3. ੜ੒͞ΕΔϑΝΠϧ WҰ෦ൈਮ ├── lib │ ├── Api │ │ ├──

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

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

    }, "name": "doggie", "photoUrls": [ "string" ], "tags": [ { "id": 1,
  6. ࣮ݱ·Ͱͷϋʔυϧ w 0QFO"1*Ͱఆٛ͢Δ w Ұؾʹ΍Δඞཁ͸ແ͍ͷͰ౎౓΍ͬͯ͘ w ϩʔΧϧ؀ڥͷվળ w 13͕͋ΔͷͰؤுΔʢՃ੎͓ئ͍͠·͢QSBZʣ w

    ࣮ઓ౤ೖΛ૝ఆͨ͠՝୊ w .PEFMʹ࣮૷ΛՃ͍͑ͨ৔߹͸Ͳ͏͢Δʁ w $JSDVJU#SFBLFSΛ࢖͍͍ͨ৔߹͸Ͳ͏͢Δʁ w ӡ༻Ͳ͏͢Δʁ ࣍ͷεϥΠυ΁
  7. .PEFMʹ࣮૷ΛՃ͍͑ͨΦϦδφϧςϯϓϨʔτ class PetApi { public function __construct( ClientInterface $client =

    null, Configuration $config = null, HeaderSelector $selector = null, $modelClass = null w ಠࣗͷίʔυੜ੒ςϯϓϨʔτΛ༻ҙͯ͠ੜ੒͞ΕΔίʔυΛΧελϚΠζ͢Δ w TXBHHFSDPEFHFOHFOFSBUFͷUΦϓγϣϯ w ࣮ߦ݁Ռͱͯ͠ฦ͢ϞσϧΫϥεΛࠩ͠ସ͑ΒΕΔΑ͏ʹ͢ΔͨΊʹ࠷খݶͷΧ ελϚΠζΛ͢Δ
  8. .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);
  9. $JSDVJU#SFBLFSΛ࢖͍͍ͨ class ApiFactory { public static function instantiate($apiClass) { ……

    return new $apiClass($httpClientWithCB, … , $modelClass); w༧Ί$JSDVJU#SFBLFSΛ૊ΈࠐΜͩΫϥΠΞϯτ͕ηοτ͞Εͨ"1*Πϯελ ϯεΛฦ͢ w IUUQTBDLJOUPTIHJUIVCJPCMPHTXBHHFSDPEFHFOXJUIHBOFTIB w "QJ$MJFOUΛܧঝͨ͠ΫϥεΛ༻ҙ͢Δํ๏
  10. ӡ༻ w ϓϥΠϕʔτ"1*ΫϥΠΞϯτઐ༻ͷϦϙδτϦΛ࡞Δ w ཁٻ͞ΕΔ1)1όʔδϣϯ͕ҧ͏ͷͰڞ༗ϦϙδτϦʹ͸ೖΕΒΕͳ͍ w Ϣχοτςετ w ਓ͕ؒॻ͘ίʔυ͚ͩςετΛॻ͚͹ྑ͍ͷͰ͸ w

    "1*ఆٛͱੜ੒͞ΕΔίʔυ͸ඞͣҰக͢Δ w "1*ͷৼΔ෣͍͸"1*ଆͷςετ͕อূ͢Δ w ։ൃͷྲྀΕ w "1*Λ࣮૷͢Δʗ0QFO"1*ͷఆٛΛॻ͘ w ΫϥΠΞϯτΛੜ੒͢ΔʗαϒΫϥεͷ࣮૷͕ඞཁͳΒ΍Δ ςετΛॻ͘