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
NilAway による静的解析で「10 億ドル」を節約する #kyotogo / Kyoto Go 56th
ytaka23
3
380
alecthomas/kong はいいぞ / kamakura.go#7
fujiwara3
1
300
DUSt3R, MASt3R, MASt3R-SfM にみる3D基盤モデル
spatial_ai_network
2
110
社外コミュニティで学び社内に活かす共に学ぶプロジェクトの実践/backlogworld2024
nishiuma
0
260
統計データで2024年の クラウド・インフラ動向を眺める
ysknsid25
2
840
なぜCodeceptJSを選んだか
goataka
0
160
podman_update_2024-12
orimanabu
1
270
LINEヤフーのフロントエンド組織・体制の紹介【24年12月】
lycorp_recruit_jp
0
530
Snowflake女子会#3 Snowpipeの良さを5分で語るよ
lana2548
0
230
Snykで始めるセキュリティ担当者とSREと開発者が楽になる脆弱性対応 / Getting started with Snyk Vulnerability Response
yamaguchitk333
2
180
watsonx.ai Dojo #5 ファインチューニングとInstructLAB
oniak3ibm
PRO
0
160
レンジャーシステムズ | 会社紹介(採用ピッチ)
rssytems
0
150
Featured
See All Featured
How to Ace a Technical Interview
jacobian
276
23k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
Become a Pro
speakerdeck
PRO
26
5k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
Docker and Python
trallard
42
3.1k
It's Worth the Effort
3n
183
28k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Being A Developer After 40
akosma
87
590k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Testing 201, or: Great Expectations
jmmastey
40
7.1k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
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ͷҧ͍Λཧղ͠ɺదͳΧελϜߏମΛ࡞ ͢Δ͜ͱ͕ॏཁ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ