Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
プライベートAPIのクライアントをいい感じにしたい / pepabo_ec_tech_mtg02
Search
Akihito Nakano
May 16, 2017
Programming
0
1.7k
プライベートAPIのクライアントをいい感じにしたい / pepabo_ec_tech_mtg02
2017/05/16 第2回 EC事業部 Tech MTG
Akihito Nakano
May 16, 2017
Tweet
Share
More Decks by Akihito Nakano
See All by Akihito Nakano
OpenAPI Generator Meetup #1
akihito_nakano
1
1.2k
Libraの現状と技術面をざっくりと / astudy20191114
akihito_nakano
4
1.1k
カート刷新プロジェクトにおけるOpenAPIを活用したコード自動生成の今と明日 / astudy20190419
akihito_nakano
2
570
平静を保ち、コードを生成せよ 〜 OpenAPI Generator誕生の背景と軌跡 〜 / gunmaweb34
akihito_nakano
24
54k
サーキットブレーカー 〜 有料契約店舗数 国内 No.1 ECサービスに神を宿す 〜 / pepabo_ec_tech_mtg01
akihito_nakano
1
3.9k
並行・並列処理のテストは難しい
akihito_nakano
2
4.3k
並列処理で消耗しているかたへ - Snidel のご紹介 -
akihito_nakano
4
3.4k
Other Decks in Programming
See All in Programming
EventSourcingの理想と現実
wenas
6
2.3k
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
870
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
受け取る人から提供する人になるということ
little_rubyist
0
230
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
580
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1k
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
290
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
330
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
BBQ
matthewcrist
85
9.3k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Designing for humans not robots
tammielis
250
25k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
A better future with KSS
kneath
238
17k
Teambox: Starting and Learning
jrom
133
8.8k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
410
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Why Our Code Smells
bkeepers
PRO
334
57k
Happy Clients
brianwarren
98
6.7k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Transcript
தڿਓ(.01&1"#0JOD ୈճ&$ࣄۀ෦5FDI.5( ϓϥΠϕʔτ"1*ͷΫϥΠΞϯτΛ ͍͍ײ͡ʹ͍ͨ͠
8&#ΞϓϦέʔγϣϯΤϯδχΞ &$ࣄۀ෦Χϥʔϛʔγϣοϓάϧʔϓ தڿਓ ͖͋ͬʔ !/","/0@"LJIJUP ಛ߈ୂνʔϜ IUUQTBDLJOUPTIHJUIVCJP
࣍ wղܾ͍ͨ͜͠ͱ wཧ w࣮ݱ·Ͱͷϋʔυϧ wϨϏϡʔґཔ
ղܾ͍ͨ͜͠ͱ
ΧΦεԽ͖͍ͯͯ͠Δ
ϓϥΠϕʔτ"1*ΫϥΠΞϯτͷݱঢ় w ࣮ʹ౷Ұײ͕ͳ͍ w +40/Λσίʔυ͚ͨͩ͠ͷσʔλΛฦ͢ͷ͋ΕɺϞσϧ૬ͷΠϯελϯεΛฦ͢ͷ ͋Δ w ෳͷϦϙδτϦʹࢄ࣮ͯ͠͞Ε͍ͯΔ w ͦΕͧΕʹ"1*Λୟͨ͘ΊͷɾࢠΫϥε͕ଘࡏ͢Δ
w ผͷϦϙδτϦͷΫϥεΛܧঝͯ͠Δͷ͋Δ w Ϣχοτςετͷϝϯςφϯείετ w "1*ͷ༷Λม͑ͨΒQIQWDSͷϑΟΫενϟਓؒͷखͰमਖ਼͠ͳ͍ͱ͍͚ͳ͍ w ಉ͡Α͏ͳςετ͕ͨ͘͞Μ͋Δ
ΧΦεԽ·ͩ࢝·͔ͬͨΓ ͍·ͷ͏ͪʹखΛଧ͍ͪͨ
ཧ
4XBHHFS$PEFHFO
4XBHHFS$PEF(FOFSBUPS wIUUQTXBHHFSJPTXBHHFSDPEFHFO w0QFO"1*4XBHHFSʹԊͬͨఆ͔ٛΒ৭ʑͳݴޠɾϑϨʔϜϫʔΫͷαʔ όʔɺΫϥΠΞϯτɺςετελϒΛੜͰ͖Δ w࠷৽W wW ͦΖͦΖϦϦʔε ͔Β(V[[MF 143 Λ)551ΫϥΠΞϯτͱͯ͠
ར༻͍ͯ͠Δ w1)1Ҏ্
ΠϯετʔϧʙΫϥΠΞϯτੜ $ 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 .
ੜ͞ΕΔϑΝΠϧ WҰ෦ൈਮ ├── lib │ ├── Api │ │ ├──
PetApi.php │ │ ├── StoreApi.php │ │ └── UserApi.php │ ├── ApiClient.php │ ├── Model │ │ ├── ApiResponse.php │ │ ├── Category.php │ │ ├── Order.php │ │ ├── Pet.php │ │ ├── Tag.php │ │ └── User.php
ϖοτετΞͷTXBHHFSKTPO { "paths": { "/pet/{petId}": { "get": { "operationId": “getPetById",
…… "definitions": { "Pet": { "type": "object", "properties": { "category": { "$ref": "#/definitions/Category" },
QFU\QFU*E^ͷϨεϙϯε { "id": 1, "category": { "id": 1, "name": "string"
}, "name": "doggie", "photoUrls": [ "string" ], "tags": [ { "id": 1,
ΫϥΠΞϯτͷ͍৺ (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) {
ΫϥΠΞϯτͷ͍৺ (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) {
࣮ઓೖΛఆͨ͠՝ wϞσϧʹ࣮ΛՃ͍͑ͨ w$JSDVJU#SFBLFSΛ͍͍ͨ w4XBHHFS$PEFHFO $JSDVJU#SFBLFS (BOFTIB w IUUQTBDLJOUPTIHJUIVCJPCMPHTXBHHFSDPEFHFOXJUIHBOFTIB
ϋʔυϧ
࣮ݱ·Ͱͷϋʔυϧ w 0QFO"1*Ͱఆٛ͢Δ w ҰؾʹΔඞཁແ͍ͷͰͬͯ͘ w ϩʔΧϧڥͷվળ w 13͕͋ΔͷͰؤுΔʢՃ͓ئ͍͠·͢QSBZʣ w
࣮ઓೖΛఆͨ͠՝ w .PEFMʹ࣮ΛՃ͍͑ͨ߹Ͳ͏͢Δʁ w $JSDVJU#SFBLFSΛ͍͍ͨ߹Ͳ͏͢Δʁ w ӡ༻Ͳ͏͢Δʁ ࣍ͷεϥΠυ
ίʔυࣗಈੜͷϓϥΫςΟε w(FOFSBUJPO(BQύλʔϯ wIUUQTNBSUJOGPXMFSDPNETM$BUBMPHHFOFSBUJPO(BQIUNM wIUUQXXXIZVLJDPNEQEQJOGPIUNM(FOFSBUJPO(BQ wlॻ͖Ճ͑ͯͨ෦͕ফ͑ͯΔʜzΛ͙ wࣗಈੜ͞Εͨίʔυͱਓ͕ؒॻ͍ͨίʔυΛ͢Δ wੜ͞ΕͨίʔυͷαϒΫϥεʹਓ͕࣮ؒΛՃ͑Δ
.PEFMʹ࣮ΛՃ͍͑ͨ߹ͬͯʁ ࣮ྫͱͯ͠ɺطଘͷ.PEFMͷ࣮ΛݟͯΈΔ
(FOFSBUJPO(BQύλʔϯΛద༻͍͕ͨ͠ʜ (new \Swagger\Client\Api\PetApi)->getPetById(1); // class Swagger\Client\Model\Pet w1FUϞσϧʹखΛೖΕ͍ͨ w1FU"QJͷฦΓ͕1FUϞσϧ w1FUͷαϒΫϥεΛར༻͢Δ༨͕ແ͍ʜ
࣮ઓೖΛఆͨ͠՝ .PEFMʹ࣮ΛՃ͍͑ͨ wΦϦδφϧςϯϓϨʔτͱϑΝΫτϦΛ͏ wΦϦδφϧςϯϓϨʔτ wਓ͕ؒॻ͘αϒΫϥεΛར༻͢ΔͨΊͷԼΛ࡞Δ wϑΝΫτϦ w"1*ΫϥεͷੜΛ୲͢Δ w"1*Ϋϥε͕ฦ͖͢ϞσϧΛཧͤ͞Δ
.PEFMʹ࣮ΛՃ͍͑ͨΦϦδφϧςϯϓϨʔτ class PetApi { public function __construct( ClientInterface $client =
null, Configuration $config = null, HeaderSelector $selector = null, $modelClass = null w ಠࣗͷίʔυੜςϯϓϨʔτΛ༻ҙͯ͠ੜ͞ΕΔίʔυΛΧελϚΠζ͢Δ w TXBHHFSDPEFHFOHFOFSBUFͷUΦϓγϣϯ w ࣮ߦ݁Ռͱͯ͠ฦ͢ϞσϧΫϥεΛࠩ͠ସ͑ΒΕΔΑ͏ʹ͢ΔͨΊʹ࠷খݶͷΧ ελϚΠζΛ͢Δ
.PEFMʹ࣮ΛՃ͍͑ͨΦϦδφϧςϯϓϨʔτ Πϝʔδ
.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);
.PEFMʹ࣮ΛՃ͍͑ͨϑΝΫτϦ Πϝʔδ
.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ύλʔϯΛద༻༷ͨ͠ࢠ
࣮ઓೖΛఆͨ͠՝ $JSDVJU#SFBLFSΛ͍͍ͨ wϑΝΫτϦΛ͏ w"1*ΫϥεΛ࡞Δͱ͖ʹɺ$JSDVJU#SFBLFSΛΈࠐΜͩ)551ΫϥΠΞ ϯτΛηοτ͢Δ
$JSDVJU#SFBLFSΛ͍͍ͨ class ApiFactory { public static function instantiate($apiClass) { ……
return new $apiClass($httpClientWithCB, … , $modelClass); w༧Ί$JSDVJU#SFBLFSΛΈࠐΜͩΫϥΠΞϯτ͕ηοτ͞Εͨ"1*Πϯελ ϯεΛฦ͢ w IUUQTBDLJOUPTIHJUIVCJPCMPHTXBHHFSDPEFHFOXJUIHBOFTIB w "QJ$MJFOUΛܧঝͨ͠ΫϥεΛ༻ҙ͢Δํ๏
$JSDVJU#SFBLFSΛ͍͍ͨ Πϝʔδ
ӡ༻ w ϓϥΠϕʔτ"1*ΫϥΠΞϯτઐ༻ͷϦϙδτϦΛ࡞Δ w ཁٻ͞ΕΔ1)1όʔδϣϯ͕ҧ͏ͷͰڞ༗ϦϙδτϦʹೖΕΒΕͳ͍ w Ϣχοτςετ w ਓ͕ؒॻ͘ίʔυ͚ͩςετΛॻ͚ྑ͍ͷͰ w
"1*ఆٛͱੜ͞ΕΔίʔυඞͣҰக͢Δ w "1*ͷৼΔ͍"1*ଆͷςετ͕อূ͢Δ w ։ൃͷྲྀΕ w "1*Λ࣮͢Δʗ0QFO"1*ͷఆٛΛॻ͘ w ΫϥΠΞϯτΛੜ͢ΔʗαϒΫϥεͷ࣮͕ඞཁͳΒΔ ςετΛॻ͘
ݱঢ়ˠ4XBHHFS$PEFHFOΛ͏ͱ w࣮ʹ౷Ұײ͕ͳ͍ wੜ͞ΕΔίʔυͰ͕උ͞Εɺ౷Ұײ͕ͰΔ wෳͷϦϙδτϦʹࢄ࣮ͯ͠͞Ε͍ͯΔ wઐ༻ͷϦϙδτϦʹ౷Ұ wϢχοτςετͷϝϯςφϯείετ wਓ͕ؒॻ͍ͨίʔυ͚ͩςετ͢Εྑ͍
ϨϏϡʔ͓ئ͍͠·͢