Slide 1

Slide 1 text

@ojun Overview: Swift OpenAPI Generator

Slide 2

Slide 2 text

ࣗݾ঺հ

Slide 3

Slide 3 text

ࣗݾ঺հ • name: ojun • 2023೥৽ଔೖࣾ • iOSΤϯδχΞ

Slide 4

Slide 4 text

ࠓ೔ൃද͢Δ͜ͱ

Slide 5

Slide 5 text

ࠓ೔ൃද͢Δ͜ͱ • Swift OpenAPI Generator ͷ overview • αʔόଆ & ΫϥΠΞϯτଆͷ࣮૷ • ΞϓϦ։ൃͷࡍʹඞཁʹͳΔ͜ͱ • APIClientͷӅṭ • Headerͷ஫ೖ • Middlewareͷਂ۷Γ

Slide 6

Slide 6 text

• Swift OpenAPI Generator ͷ overview • αʔόଆ & ΫϥΠΞϯτଆͷ࣮૷ • ΞϓϦ։ൃͷࡍʹඞཁʹͳΔ͜ͱ • APIClientͷӅṭ • Headerͷ஫ೖ • Middlewareͷਂ۷Γ ࠓ೔ൃද͢Δ͜ͱ “https://developer.apple.com/videos/play/wwdc2023/10171/"ΑΓҾ༻

Slide 7

Slide 7 text

Swift OpenAPI Generator ͱ͸ʁ

Slide 8

Slide 8 text

Swift OpenAPI Generator ͱ͸ʁ • AppleʹΑͬͯ։ൃ͞Εͨ Swift Package Plugin • OpenAPIυΩϡϝϯτ͔ΒSwiftͷΫϥΠΞϯτ͓ΑͼαʔόʔίʔυΛ ੜ੒͢Δ͜ͱ͕Մೳ • OpenAPI ͷ 3.0 ͱ 3.1 Ͱಈ࡞͢Δ

Slide 9

Slide 9 text

OpenAPIͷॏཁੑ • OpenAPI͸ɺHTTPϕʔεͷAPIΛهड़͢ΔͨΊͷۀքඪ४ • JSON·ͨ͸YAMLͰॻ͔ΕͨOpenAPIυΩϡϝϯτʹΑΓɺAPIͷߏ଄ Λ໌֬ʹ͠ɺҟͳΔπʔϧͰͷࣗಈԽ͕ՄೳͱͳΔ

Slide 10

Slide 10 text

Swift OpenAPI Generatorͷ໨త • APIݺͼग़͠ͰඞཁͱͳΔ͍ΘΏΔ”ّࣜతͳίʔυ”Λࣗಈੜ੒͢Δ͜ͱ • ίʔυ͸Ϗϧυ࣌ʹੜ੒͞Εɺ͔ͭɺAPIυΩϡϝϯτͱৗʹಉظ͢Δ ͷͰϦϙδτϦʹίʔυΛؚΊΔඞཁ͕ͳ͘ͳΔ ࣗಈੜ੒

Slide 11

Slide 11 text

Swift OpenAPI Generatorͷ໨త • APIݺͼग़͠ͰඞཁͱͳΔ͍ΘΏΔ”ّࣜతͳίʔυ”Λࣗಈੜ੒͢Δ͜ͱ • ίʔυ͸Ϗϧυ࣌ʹੜ੒͞Εɺ͔ͭɺAPIυΩϡϝϯτͱৗʹಉظ͢Δ ͷͰϦϙδτϦʹίʔυΛؚΊΔඞཁ͕ͳ͘ͳΔ • ख࡞ۀʹΑΔAPIؔ࿈ίʔυͷهड़͔Βղ์͞ΕɺΑΓޮ཰త͔ͭਖ਼֬ ʹϓϩδΣΫτΛਐΊΔΑ͏ʹͳΔ🎉

Slide 12

Slide 12 text

Swift OpenAPI Generatorͷಛ௃

Slide 13

Slide 13 text

Swift OpenAPI Generatorͷಛ௃ • Swift Package Plugin • Ϗϧυ࣌ʹҎԼ͕ίʔυੜ੒͞ΕΔ • Type safe ͳϓϩύςΟ • ೖग़ྗͷΤϯίʔυɾσίʔυॲཧ • ϦΫΤετͷੜ੒

Slide 14

Slide 14 text

αʔόଆͷ࣮૷

Slide 15

Slide 15 text

αʔόଆͷ࣮૷ • ΫϥΠΞϯτଆΛಈ࡞ͤ͞ΔͨΊʹVaporΛར༻ͨ͠ϩʔΧϧͰಈ࡞͢ ΔαʔόΛ࣮૷͢Δ

Slide 16

Slide 16 text

OpenAPIͷ಺༰ • શ༰͸ӈͷ௨Γ

Slide 17

Slide 17 text

OpenAPIͷ಺༰ • OpenAPIͷόʔδϣϯΛ هड़ • info͓Αͼserversͷ಺༰Λ هड़

Slide 18

Slide 18 text

OpenAPIͷ಺༰ • v1/welcome_message Λ࣮૷ • parameter ͱͯ͠ {name: String} Λड͚औΔ • response ͱͯ͠ PixivAppNightMessageΛ ฦ͢

Slide 19

Slide 19 text

OpenAPIͷ಺༰ • PixivAppNightMessage ͸ {message: String}ΛؚΉ

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

BuildΛߦ͏ͱҎԼͷίʔυ͕ࣗಈੜ੒͞ΕΔ

Slide 22

Slide 22 text

APIProtocolʹ४ڌͨ͠ߏ଄ମΛ࣮૷͢Δ

Slide 23

Slide 23 text

APIProtocolʹ४ڌͨ͠ߏ଄ମΛ࣮૷͢Δ

Slide 24

Slide 24 text

APIProtocolʹ४ڌͨ͠ߏ଄ମΛ࣮૷͢Δ return ͕ෳࡶʹͳΔ

Slide 25

Slide 25 text

APIProtocolʹ४ڌͨ͠ߏ଄ମΛ࣮૷͢Δ

Slide 26

Slide 26 text

• ࣮ߦ ͢Δ͜ͱͰ ϩʔΧϧͷαʔό͕ ্ཱ͕ͪΔ 🎉 APIProtocolʹ४ڌͨ͠ߏ଄ମΛ࣮૷͢Δ

Slide 27

Slide 27 text

αʔόଆͷ࣮૷׬੒ 🎉

Slide 28

Slide 28 text

Clientଆͷ࣮૷

Slide 29

Slide 29 text

OpenAPIͷ࣮૷ • ϩʔΧϧͰͷ։ൃͳͷͰ ઐ༻ͷurlΛ௥Ճ͢Δ

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Clientଆͷ࣮૷ 1. ClientͷॳظԽ 2. APIΛୟ͘ 3. responseͷղੳ

Slide 32

Slide 32 text

Clientଆͷ࣮૷ 1. ClientͷॳظԽ 2. APIΛୟ͘ 3. responseͷղੳ

Slide 33

Slide 33 text

Clientଆͷ࣮૷ 1. ClientͷॳظԽ 2. APIΛୟ͘ 3. responseͷղੳ

Slide 34

Slide 34 text

Clientଆͷ࣮૷ 1. ClientͷॳظԽ 2. APIΛୟ͘ 3. responseͷղੳ ࣮ߦ͢Δͱʮojun͞Μɺpixiv App Night΁Α͏ͦ͜!ʯ͕ग़ྗ͞ΕΔ

Slide 35

Slide 35 text

Client ଆͷ࣮૷׬੒ 🎉

Slide 36

Slide 36 text

Swift OpenAPI GeneratorͷӅṭ

Slide 37

Slide 37 text

Swift OpenAPI GeneratorͷӅṭ • ݱࡏͷ࣮૷Ͱ͸ɺར༻ଆ͕ Swift OpenAPI Generator ʹґଘͨ͠ϝιο υΛ௚઀ݺͼग़͍ͯ͠Δ → ར༻ऀ͕Generatorͷ࣮૷ͷৄࡉΛ஌Δඞཁ͕͋Δ • ͜Εղܾ͢ΔͨΊʹɺ৽͍͠ߏ଄ମΛಋೖ͠ɺSwift OpenAPI Generator ͷػೳΛϥοϓ͢Δ • ͜ΕʹΑΓSwift OpenAPI Generator΁ͷ௚઀తͳґଘؔ܎͕Ӆṭ͞Εɺ APIͷ࢖༻͕ΑΓγϯϓϧʹͳΔ

Slide 38

Slide 38 text

Swift OpenAPI GeneratorͷӅṭ • APIClientΛఆٛ͢Δ

Slide 39

Slide 39 text

Swift OpenAPI GeneratorͷӅṭ • clientΛఆٛ͢Δ

Slide 40

Slide 40 text

Swift OpenAPI GeneratorͷӅṭ • clientΛఆٛ͢Δ • any APIProtocolͳclientΛ ఆٛ͠init಺ͰॳظԽ͢Δ

Slide 41

Slide 41 text

Swift OpenAPI GeneratorͷӅṭ • clientΛఆٛ͢Δ • any APIProtocolͳclientΛ ఆٛ͠init಺ͰॳظԽ͢Δ • APIͷ਺͚ͩϝιουΛ࡞੒͠ ͦͷதͰAPIΛୟ͘

Slide 42

Slide 42 text

Swift OpenAPI GeneratorͷӅṭ • ෳ਺ͷ initializer Λఏڙ͠ έʔεʹԠͯ͡ݺͼग़͢ initializer Λ੾Γସ͑Δ ͜ͱͰ testable ͳ࣮૷ ʹͳΔ

Slide 43

Slide 43 text

Ӆṭ׬੒ 🎉

Slide 44

Slide 44 text

Headerͷ௥Ճ

Slide 45

Slide 45 text

Headerͷ௥Ճ • APIΛୟ͘ࡍʹHeaderʹରͯ͠ ༷ʑͳ஋Λ௥Ճ͢Δ͜ͱ͕͋Δ • Swift OpenAPI Generatorͷ ClientΛར༻͢Δࡍ΋HeaderΛ ௥Ճ͢Δඞཁ͕͋Δ

Slide 46

Slide 46 text

ClientMiddleware

Slide 47

Slide 47 text

ClientMiddleware

Slide 48

Slide 48 text

ClientMiddleware

Slide 49

Slide 49 text

ClientMiddleware

Slide 50

Slide 50 text

ClientMiddleware

Slide 51

Slide 51 text

ClientMiddleware

Slide 52

Slide 52 text

ClientMiddleware

Slide 53

Slide 53 text

ClientMiddleware

Slide 54

Slide 54 text

࣮ࡍʹ࡞ͬͯΈΑ͏ʂ

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

HTTPField.NameΛࢦఆ͢Δ

Slide 64

Slide 64 text

HTTPField.Nameͱ͸ʁ

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

HTTPField.NameΛࢦఆ͢Δ

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

ΧελϜͳHTTPField.NameΛࢦఆ͍ͨ͠ • σϑΥϧτͰ͸“Appp-OS”΍ “App-Version”͸ଘࡏ͍ͯ͠ͳ͍

Slide 70

Slide 70 text

• σϑΥϧτͰ͸“Appp-OS”΍ “App-Version”͸ଘࡏ͍ͯ͠ͳ͍ • → ࣗ࡞͢Δඞཁ͕͋Δ ΧελϜͳHTTPField.NameΛࢦఆ͍ͨ͠

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

ຖճม਺ʹೖΕΔͷ͕໘౗

Slide 74

Slide 74 text

ConsumingΩʔϫʔυΛ෇༩͢Δ ͜ͱͰճආͰ͖Δ

Slide 75

Slide 75 text

෇༩Ͱ͖Δͷ͕ίϯύΠϥͷόά Ͱ͋ΔՄೳੑ͸͋Δ ConsumingΩʔϫʔυΛ෇༩͢Δ ͜ͱͰճආͰ͖Δ

Slide 76

Slide 76 text

HeaderInjectionMiddleware ͷ׬੒ 🎉

Slide 77

Slide 77 text

࡞੒ͨ͠ Middleware ͷࢦఆ

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

Middleware ͷਂ۷Γ

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

Transport ͱ Middlewareͷҧ͍͸ʁ

Slide 83

Slide 83 text

Transport • ࣮ࡍʹωοτϫʔΫΛ࢖༻͠ɺHTTPૢ࡞Λ࣮ߦ͢Δ(جຊͱͳΔ)HTTP ϥΠϒϥϦΛந৅Խ͢Δ • ҎԼͷ2ͭͷ৔߹͸ custom transport ͷ࣮૷Λݕ౼͢Δͱྑ͍ • ޷ΈͷHTTPϥΠϒϥϦʹରԠ͢ΔΫϥΠΞϯττϥϯεϙʔτ͕·ͩ ଘࡏ͠ͳ͍৔߹ • ςετͰ௝͍͠ωοτϫʔΫ৚݅ΛγϛϡϨʔτ͢Δඞཁ͕͋Δ৔߹

Slide 84

Slide 84 text

ClientMiddleware • transport ʹҾ͖౉͞ΕΔલͷϦΫΤετͱɺtransport ʹΑͬͯ౉͞Ε ͨޙͷϨεϙϯεΛಡΈऔΓɺมߋ͢Δ͜ͱ͕Ͱ͖Δ • HTTPϦΫΤετͱϨεϙϯεΛड͚औΔ͕ɺࣗ਎ͰHTTPૢ࡞Λ࣮ߦ͢ Δ੹೚͸ଘࡏ͠ͳ͍ • Ώ͑ʹɺϛυϧ΢ΣΞ͸௥Ճͷ next ύϥϝʔλΛऔΓɺHTTPίʔϧ Λϛυϧ΢ΣΞελοΫͷઌ಄ʹ͋Δ transport ʹҕৡ͢Δ

Slide 85

Slide 85 text

ClientMiddleware • ೝূɺϩάه࿥ɺϝτϦΫεɺτϨʔγϯάɺΧελϜϔομͷ஫ೖΛ ߦ͏͜ͱʹద͍ͯ͠Δ • ·ͨɺҎԼͷΑ͏ͳςετʹ΋ద͍ͯ͠Δ • ࣮ࡍͷαʔόʔ΁ͷϦΫΤετ࣌ʹϥϯμϜͳࣦഊΛ஫ೖ • ͦͷঢ়گΛར༻ͯ͠ɺϦτϥΠ΍ΤϥʔϋϯυϦϯάͷϩδοΫΛς ετ͢Δ͜ͱ

Slide 86

Slide 86 text

ॲཧͷॱং ʢAPIϦΫΤετૹ৴࣌ʣ

Slide 87

Slide 87 text

ॲཧͷॱংʢAPIϦΫΤετૹ৴࣌ʣ • [Middleware] ͷ্͔Β࣮ߦ • ׬ྃޙɺtarnsport͕࣮ߦ͞ΕΔ

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

1

Slide 90

Slide 90 text

1 2

Slide 91

Slide 91 text

1 2 3

Slide 92

Slide 92 text

1 2 3 4

Slide 93

Slide 93 text

1 2 3 4 OpenAPIRuntime/Interface/UniversalClient.swift ಺Ͱ࣮૷͞ΕͯΔ

Slide 94

Slide 94 text

Ҿ਺ʹnext͕ଘࡏͨ͠ཧ༝ͱ return ͢Δࡍʹ next Λࢦఆ͍ͯͨ͠ཧ༝͕ݟ͑ͯ͘Δ

Slide 95

Slide 95 text

ॲཧͷॱং ʢϨεϙϯεड৴࣌ʣ

Slide 96

Slide 96 text

ॲཧͷॱংʢϨεϙϯεड৴࣌ʣ • ࠷ॳʹtarnsport͕࣮ߦ͞ΕΔ • ׬ྃޙɺ[Middleware] ͷԼ͔Βॱ࣮࣍ߦ͞ΕΔ • * Կ౓͔ࢼ͕ͨ͠ɺϨεϙϯεड৴࣌ʹͦ΋ͦ΋Middleware͕࣮ߦ ͞Εͳ͔ͬͨ

Slide 97

Slide 97 text

·ͱΊ

Slide 98

Slide 98 text

·ͱΊ • Swift OpenAPI Generator Λར༻͢Δ͜ͱͰɺAPIݺͼग़͠΍αʔόʔ࣮ ૷ʹඞཁͳίʔυΛࣗಈੜ੒͠ɺ։ൃऀ͕ख࡞ۀΛলུͯ͠ޮ཰త͔ͭ ਖ਼֬ʹϓϩδΣΫτΛਐΊΒΕΔΑ͏ʹͳΔ • Generatorͷ࣮૷ͷৄࡉΛӅͨ͢Ίʹclient෦෼Λϥοϓ͢Δ

Slide 99

Slide 99 text

·ͱΊ • ClientMiddlewareΛར༻ͯ͠HeaderΛࢦఆ͢Δ • ΧελϜͳHeader͸HTTPField.NameΛ֦ு͢Δ • MiddlewareΛ࢖͏͜ͱͰೝূɺϩάه࿥ͳͲ༷ʑͳ͜ͱΛߦ͑Δ • transportͱmiddlewareͷҧ͍Λཧղ͠ɺద੾ͳΧελϜߏ଄ମΛ࡞੒ ͢Δ͜ͱ͕ॏཁ

Slide 100

Slide 100 text

͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ