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
Overview: Swift OpenAPI Generator
Search
ojun
January 25, 2024
Technology
3
1.4k
Overview: Swift OpenAPI Generator
ojun
January 25, 2024
Tweet
Share
More Decks by ojun
See All by ojun
Xcode 16のPreviewModifierと@Previewableを活用した効率的なプレビュー方法の考察
ojun9
2
290
Catch Up with Swift 5.10
ojun9
2
700
if 式と switch 式による SwiftUI のプレビューエラー対策
ojun9
1
1.2k
楽しい夏休み!String Catalogs で新しい発見
ojun9
0
1.8k
ユニットテストを学んだ次に知りたかったApple標準APIに対するテストのやり方
ojun9
1
380
ハッカソンにおけるiOSアプリ開発での学びと楽しさ
ojun9
1
310
Other Decks in Technology
See All in Technology
フルカイテン株式会社 採用資料
fullkaiten
0
32k
Google CloudのLLM活用の選択肢を広げるVertex AIのパートナーモデル
nayuts
0
130
PDF Viewer作成の今までとこれから
hunachi
0
480
Road to Single Activity
yurihondo
2
240
Next.js のページ遷移を全力で止める
ypresto
9
3.6k
watsonx.ai Dojo 環境準備について
oniak3ibm
PRO
0
360
Mocking in Rust Applications
taiki45
2
420
Creative UIs with Compose: DroidKaigi 2024
chrishorner
1
610
事前準備が肝!AI活用のための業務改革
layerx
PRO
1
400
急拡大する開発組織を支えるナレッジワークの E2E テスト基盤
kworkdev
PRO
0
120
再考 アクターモデル/ reconsider actor model
ytake
0
380
内製化を目指す事業会社が、システム開発会社と共に進める「開発生産性改善」の取り組み事例 #devsumi
yuwji
1
150
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
123
18k
Why You Should Never Use an ORM
jnunemaker
PRO
53
8.9k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
32k
Unsuck your backbone
ammeep
667
57k
The World Runs on Bad Software
bkeepers
PRO
64
11k
Code Review Best Practice
trishagee
62
16k
Navigating Team Friction
lara
183
13k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
158
15k
How GitHub (no longer) Works
holman
310
140k
Ruby is Unlike a Banana
tanoku
96
11k
Git: the NoSQL Database
bkeepers
PRO
425
64k
Faster Mobile Websites
deanohume
304
30k
Transcript
@ojun Overview: Swift OpenAPI Generator
ࣗݾհ
ࣗݾհ • name: ojun • 2023৽ଔೖࣾ • iOSΤϯδχΞ
ࠓൃද͢Δ͜ͱ
ࠓൃද͢Δ͜ͱ • Swift OpenAPI Generator ͷ overview • αʔόଆ &
ΫϥΠΞϯτଆͷ࣮ • ΞϓϦ։ൃͷࡍʹඞཁʹͳΔ͜ͱ • APIClientͷӅṭ • Headerͷೖ • Middlewareͷਂ۷Γ
• Swift OpenAPI Generator ͷ overview • αʔόଆ & ΫϥΠΞϯτଆͷ࣮
• ΞϓϦ։ൃͷࡍʹඞཁʹͳΔ͜ͱ • APIClientͷӅṭ • Headerͷೖ • Middlewareͷਂ۷Γ ࠓൃද͢Δ͜ͱ “https://developer.apple.com/videos/play/wwdc2023/10171/"ΑΓҾ༻
Swift OpenAPI Generator ͱʁ
Swift OpenAPI Generator ͱʁ • AppleʹΑͬͯ։ൃ͞Εͨ Swift Package Plugin •
OpenAPIυΩϡϝϯτ͔ΒSwiftͷΫϥΠΞϯτ͓ΑͼαʔόʔίʔυΛ ੜ͢Δ͜ͱ͕Մೳ • OpenAPI ͷ 3.0 ͱ 3.1 Ͱಈ࡞͢Δ
OpenAPIͷॏཁੑ • OpenAPIɺHTTPϕʔεͷAPIΛهड़͢ΔͨΊͷۀքඪ४ • JSON·ͨYAMLͰॻ͔ΕͨOpenAPIυΩϡϝϯτʹΑΓɺAPIͷߏ Λ໌֬ʹ͠ɺҟͳΔπʔϧͰͷࣗಈԽ͕ՄೳͱͳΔ
Swift OpenAPI Generatorͷత • APIݺͼग़͠ͰඞཁͱͳΔ͍ΘΏΔ”ّࣜతͳίʔυ”Λࣗಈੜ͢Δ͜ͱ • ίʔυϏϧυ࣌ʹੜ͞Εɺ͔ͭɺAPIυΩϡϝϯτͱৗʹಉظ͢Δ ͷͰϦϙδτϦʹίʔυΛؚΊΔඞཁ͕ͳ͘ͳΔ ࣗಈੜ
Swift OpenAPI Generatorͷత • APIݺͼग़͠ͰඞཁͱͳΔ͍ΘΏΔ”ّࣜతͳίʔυ”Λࣗಈੜ͢Δ͜ͱ • ίʔυϏϧυ࣌ʹੜ͞Εɺ͔ͭɺAPIυΩϡϝϯτͱৗʹಉظ͢Δ ͷͰϦϙδτϦʹίʔυΛؚΊΔඞཁ͕ͳ͘ͳΔ • ख࡞ۀʹΑΔAPIؔ࿈ίʔυͷهड़͔Βղ์͞ΕɺΑΓޮత͔ͭਖ਼֬
ʹϓϩδΣΫτΛਐΊΔΑ͏ʹͳΔ🎉
Swift OpenAPI Generatorͷಛ
Swift OpenAPI Generatorͷಛ • Swift Package Plugin • Ϗϧυ࣌ʹҎԼ͕ίʔυੜ͞ΕΔ •
Type safe ͳϓϩύςΟ • ೖग़ྗͷΤϯίʔυɾσίʔυॲཧ • ϦΫΤετͷੜ
αʔόଆͷ࣮
αʔόଆͷ࣮ • ΫϥΠΞϯτଆΛಈ࡞ͤ͞ΔͨΊʹVaporΛར༻ͨ͠ϩʔΧϧͰಈ࡞͢ ΔαʔόΛ࣮͢Δ
OpenAPIͷ༰ • શ༰ӈͷ௨Γ
OpenAPIͷ༰ • OpenAPIͷόʔδϣϯΛ هड़ • info͓Αͼserversͷ༰Λ هड़
OpenAPIͷ༰ • v1/welcome_message Λ࣮ • parameter ͱͯ͠ {name: String} Λड͚औΔ
• response ͱͯ͠ PixivAppNightMessageΛ ฦ͢
OpenAPIͷ༰ • PixivAppNightMessage {message: String}ΛؚΉ
None
BuildΛߦ͏ͱҎԼͷίʔυ͕ࣗಈੜ͞ΕΔ
APIProtocolʹ४ڌͨ͠ߏମΛ࣮͢Δ
APIProtocolʹ४ڌͨ͠ߏମΛ࣮͢Δ
APIProtocolʹ४ڌͨ͠ߏମΛ࣮͢Δ return ͕ෳࡶʹͳΔ
APIProtocolʹ४ڌͨ͠ߏମΛ࣮͢Δ
• ࣮ߦ ͢Δ͜ͱͰ ϩʔΧϧͷαʔό͕ ্ཱ͕ͪΔ 🎉 APIProtocolʹ४ڌͨ͠ߏମΛ࣮͢Δ
αʔόଆͷ࣮ 🎉
Clientଆͷ࣮
OpenAPIͷ࣮ • ϩʔΧϧͰͷ։ൃͳͷͰ ઐ༻ͷurlΛՃ͢Δ
None
Clientଆͷ࣮ 1. ClientͷॳظԽ 2. APIΛୟ͘ 3. responseͷղੳ
Clientଆͷ࣮ 1. ClientͷॳظԽ 2. APIΛୟ͘ 3. responseͷղੳ
Clientଆͷ࣮ 1. ClientͷॳظԽ 2. APIΛୟ͘ 3. responseͷղੳ
Clientଆͷ࣮ 1. ClientͷॳظԽ 2. APIΛୟ͘ 3. responseͷղੳ ࣮ߦ͢Δͱʮojun͞Μɺpixiv App NightΑ͏ͦ͜!ʯ͕ग़ྗ͞ΕΔ
Client ଆͷ࣮ 🎉
Swift OpenAPI GeneratorͷӅṭ
Swift OpenAPI GeneratorͷӅṭ • ݱࡏͷ࣮Ͱɺར༻ଆ͕ Swift OpenAPI Generator ʹґଘͨ͠ϝιο υΛݺͼग़͍ͯ͠Δ
→ ར༻ऀ͕Generatorͷ࣮ͷৄࡉΛΔඞཁ͕͋Δ • ͜Εղܾ͢ΔͨΊʹɺ৽͍͠ߏମΛಋೖ͠ɺSwift OpenAPI Generator ͷػೳΛϥοϓ͢Δ • ͜ΕʹΑΓSwift OpenAPI Generatorͷతͳґଘ͕ؔӅṭ͞Εɺ APIͷ༻͕ΑΓγϯϓϧʹͳΔ
Swift OpenAPI GeneratorͷӅṭ • APIClientΛఆٛ͢Δ
Swift OpenAPI GeneratorͷӅṭ • clientΛఆٛ͢Δ
Swift OpenAPI GeneratorͷӅṭ • clientΛఆٛ͢Δ • any APIProtocolͳclientΛ ఆٛ͠initͰॳظԽ͢Δ
Swift OpenAPI GeneratorͷӅṭ • clientΛఆٛ͢Δ • any APIProtocolͳclientΛ ఆٛ͠initͰॳظԽ͢Δ •
APIͷ͚ͩϝιουΛ࡞͠ ͦͷதͰAPIΛୟ͘
Swift OpenAPI GeneratorͷӅṭ • ෳͷ initializer Λఏڙ͠ έʔεʹԠͯ͡ݺͼग़͢ initializer ΛΓସ͑Δ
͜ͱͰ testable ͳ࣮ ʹͳΔ
Ӆṭ 🎉
HeaderͷՃ
HeaderͷՃ • APIΛୟ͘ࡍʹHeaderʹରͯ͠ ༷ʑͳΛՃ͢Δ͜ͱ͕͋Δ • Swift OpenAPI Generatorͷ ClientΛར༻͢ΔࡍHeaderΛ Ճ͢Δඞཁ͕͋Δ
ClientMiddleware
ClientMiddleware
ClientMiddleware
ClientMiddleware
ClientMiddleware
ClientMiddleware
ClientMiddleware
ClientMiddleware
࣮ࡍʹ࡞ͬͯΈΑ͏ʂ
None
None
None
None
None
None
None
None
HTTPField.NameΛࢦఆ͢Δ
HTTPField.Nameͱʁ
None
None
HTTPField.NameΛࢦఆ͢Δ
None
ΧελϜͳHTTPField.NameΛࢦఆ͍ͨ͠ • σϑΥϧτͰ“Appp-OS” “App-Version”ଘࡏ͍ͯ͠ͳ͍
• σϑΥϧτͰ“Appp-OS” “App-Version”ଘࡏ͍ͯ͠ͳ͍ • → ࣗ࡞͢Δඞཁ͕͋Δ ΧελϜͳHTTPField.NameΛࢦఆ͍ͨ͠
None
None
ຖճมʹೖΕΔͷ͕໘
ConsumingΩʔϫʔυΛ༩͢Δ ͜ͱͰճආͰ͖Δ
༩Ͱ͖Δͷ͕ίϯύΠϥͷόά Ͱ͋ΔՄೳੑ͋Δ ConsumingΩʔϫʔυΛ༩͢Δ ͜ͱͰճආͰ͖Δ
HeaderInjectionMiddleware ͷ 🎉
࡞ͨ͠ Middleware ͷࢦఆ
None
None
Middleware ͷਂ۷Γ
None
Transport ͱ Middlewareͷҧ͍ʁ
Transport • ࣮ࡍʹωοτϫʔΫΛ༻͠ɺHTTPૢ࡞Λ࣮ߦ͢Δ(جຊͱͳΔ)HTTP ϥΠϒϥϦΛநԽ͢Δ • ҎԼͷ2ͭͷ߹ custom transport ͷ࣮Λݕ౼͢Δͱྑ͍ •
ΈͷHTTPϥΠϒϥϦʹରԠ͢ΔΫϥΠΞϯττϥϯεϙʔτ͕·ͩ ଘࡏ͠ͳ͍߹ • ςετͰ͍͠ωοτϫʔΫ݅ΛγϛϡϨʔτ͢Δඞཁ͕͋Δ߹
ClientMiddleware • transport ʹҾ͖͞ΕΔલͷϦΫΤετͱɺtransport ʹΑͬͯ͞Ε ͨޙͷϨεϙϯεΛಡΈऔΓɺมߋ͢Δ͜ͱ͕Ͱ͖Δ • HTTPϦΫΤετͱϨεϙϯεΛड͚औΔ͕ɺࣗͰHTTPૢ࡞Λ࣮ߦ͢ Δଘࡏ͠ͳ͍ •
Ώ͑ʹɺϛυϧΣΞՃͷ next ύϥϝʔλΛऔΓɺHTTPίʔϧ ΛϛυϧΣΞελοΫͷઌ಄ʹ͋Δ transport ʹҕৡ͢Δ
ClientMiddleware • ೝূɺϩάهɺϝτϦΫεɺτϨʔγϯάɺΧελϜϔομͷೖΛ ߦ͏͜ͱʹద͍ͯ͠Δ • ·ͨɺҎԼͷΑ͏ͳςετʹద͍ͯ͠Δ • ࣮ࡍͷαʔόʔͷϦΫΤετ࣌ʹϥϯμϜͳࣦഊΛೖ • ͦͷঢ়گΛར༻ͯ͠ɺϦτϥΠΤϥʔϋϯυϦϯάͷϩδοΫΛς
ετ͢Δ͜ͱ
ॲཧͷॱং ʢAPIϦΫΤετૹ৴࣌ʣ
ॲཧͷॱংʢAPIϦΫΤετૹ৴࣌ʣ • [Middleware] ͷ্͔Β࣮ߦ • ྃޙɺtarnsport͕࣮ߦ͞ΕΔ
None
1
1 2
1 2 3
1 2 3 4
1 2 3 4 OpenAPIRuntime/Interface/UniversalClient.swift Ͱ࣮͞ΕͯΔ
Ҿʹnext͕ଘࡏͨ͠ཧ༝ͱ return ͢Δࡍʹ next Λࢦఆ͍ͯͨ͠ཧ༝͕ݟ͑ͯ͘Δ
ॲཧͷॱং ʢϨεϙϯεड৴࣌ʣ
ॲཧͷॱংʢϨεϙϯεड৴࣌ʣ • ࠷ॳʹtarnsport͕࣮ߦ͞ΕΔ • ྃޙɺ[Middleware] ͷԼ͔Βॱ࣮࣍ߦ͞ΕΔ • * Կ͔ࢼ͕ͨ͠ɺϨεϙϯεड৴࣌ʹͦͦMiddleware͕࣮ߦ ͞Εͳ͔ͬͨ
·ͱΊ
·ͱΊ • Swift OpenAPI Generator Λར༻͢Δ͜ͱͰɺAPIݺͼग़͠αʔόʔ࣮ ʹඞཁͳίʔυΛࣗಈੜ͠ɺ։ൃऀ͕ख࡞ۀΛলུͯ͠ޮత͔ͭ ਖ਼֬ʹϓϩδΣΫτΛਐΊΒΕΔΑ͏ʹͳΔ • Generatorͷ࣮ͷৄࡉΛӅͨ͢Ίʹclient෦Λϥοϓ͢Δ
·ͱΊ • ClientMiddlewareΛར༻ͯ͠HeaderΛࢦఆ͢Δ • ΧελϜͳHeaderHTTPField.NameΛ֦ு͢Δ • MiddlewareΛ͏͜ͱͰೝূɺϩάهͳͲ༷ʑͳ͜ͱΛߦ͑Δ • transportͱmiddlewareͷҧ͍Λཧղ͠ɺదͳΧελϜߏମΛ࡞ ͢Δ͜ͱ͕ॏཁ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ