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.8k
Overview: Swift OpenAPI Generator
ojun
January 25, 2024
Tweet
Share
More Decks by ojun
See All by ojun
Swift Evolution かるた
ojun9
2
93
Xcode 16のPreviewModifierと@Previewableを活用した効率的なプレビュー方法の考察
ojun9
4
610
Catch Up with Swift 5.10
ojun9
2
750
if 式と switch 式による SwiftUI のプレビューエラー対策
ojun9
1
1.4k
楽しい夏休み!String Catalogs で新しい発見
ojun9
0
2k
ユニットテストを学んだ次に知りたかったApple標準APIに対するテストのやり方
ojun9
1
400
ハッカソンにおけるiOSアプリ開発での学びと楽しさ
ojun9
1
320
Other Decks in Technology
See All in Technology
10分で学ぶKubernetesコンテナセキュリティ/10min-k8s-container-sec
mochizuki875
3
330
非機能品質を作り込むための実践アーキテクチャ
knih
3
1.1k
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
muziyoshiz
2
2.2k
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
170
20241214_WACATE2024冬_テスト設計技法をチョット俯瞰してみよう
kzsuzuki
3
450
株式会社ログラス − エンジニア向け会社説明資料 / Loglass Comapany Deck for Engineer
loglass2019
3
32k
LINE Developersプロダクト(LIFF/LINE Login)におけるフロントエンド開発
lycorptech_jp
PRO
0
120
C++26 エラー性動作
faithandbrave
2
730
生成AIをより賢く エンジニアのための RAG入門 - Oracle AI Jam Session #20
kutsushitaneko
4
220
サイボウズフロントエンドエキスパートチームについて / FrontendExpert Team
cybozuinsideout
PRO
5
38k
AI時代のデータセンターネットワーク
lycorptech_jp
PRO
1
280
KubeCon NA 2024 Recap: How to Move from Ingress to Gateway API with Minimal Hassle
ysakotch
0
200
Featured
See All Featured
It's Worth the Effort
3n
183
28k
Scaling GitHub
holman
458
140k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Adopting Sorbet at Scale
ufuk
73
9.1k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Thoughts on Productivity
jonyablonski
67
4.4k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
510
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
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ͷҧ͍Λཧղ͠ɺదͳΧελϜߏମΛ࡞ ͢Δ͜ͱ͕ॏཁ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ