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.7k
Overview: Swift OpenAPI Generator
ojun
January 25, 2024
Tweet
Share
More Decks by ojun
See All by ojun
Swift Evolution かるた
ojun9
2
90
Xcode 16のPreviewModifierと@Previewableを活用した効率的なプレビュー方法の考察
ojun9
4
530
Catch Up with Swift 5.10
ojun9
2
740
if 式と switch 式による SwiftUI のプレビューエラー対策
ojun9
1
1.3k
楽しい夏休み!String Catalogs で新しい発見
ojun9
0
1.9k
ユニットテストを学んだ次に知りたかったApple標準APIに対するテストのやり方
ojun9
1
390
ハッカソンにおけるiOSアプリ開発での学びと楽しさ
ojun9
1
320
Other Decks in Technology
See All in Technology
Can We Measure Developer Productivity?
ewolff
1
150
AWS Media Services 最新サービスアップデート 2024
eijikominami
0
200
ノーコードデータ分析ツールで体験する時系列データ分析超入門
negi111111
0
410
適材適所の技術選定 〜GraphQL・REST API・tRPC〜 / Optimal Technology Selection
kakehashi
1
170
複雑なState管理からの脱却
sansantech
PRO
1
140
AWS Lambdaと歩んだ“サーバーレス”と今後 #lambda_10years
yoshidashingo
1
170
Incident Response Practices: Waroom's Features and Future Challenges
rrreeeyyy
0
160
New Relicを活用したSREの最初のステップ / NRUG OKINAWA VOL.3
isaoshimizu
2
590
エンジニア人生の拡張性を高める 「探索型キャリア設計」の提案
tenshoku_draft
1
120
100 名超が参加した日経グループ横断の競技型 AWS 学習イベント「Nikkei Group AWS GameDay」の紹介/mediajaws202411
nikkei_engineer_recruiting
1
170
Adopting Jetpack Compose in Your Existing Project - GDG DevFest Bangkok 2024
akexorcist
0
110
OCI Vault 概要
oracle4engineer
PRO
0
9.7k
Featured
See All Featured
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
What's in a price? How to price your products and services
michaelherold
243
12k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Being A Developer After 40
akosma
86
590k
How STYLIGHT went responsive
nonsquared
95
5.2k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Designing for Performance
lara
604
68k
Speed Design
sergeychernyshev
24
610
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
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ͷҧ͍Λཧղ͠ɺదͳΧελϜߏମΛ࡞ ͢Δ͜ͱ͕ॏཁ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ