View in English

  • Apple Developer
    • Get Started

    Explore Get Started

    • Overview
    • Learn
    • Apple Developer Program

    Stay Updated

    • Latest News
    • Hello Developer
    • Platforms

    Explore Platforms

    • Apple Platforms
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store

    Featured

    • Design
    • Distribution
    • Games
    • Accessories
    • Web
    • Home
    • CarPlay
    • Technologies

    Explore Technologies

    • Overview
    • Xcode
    • Swift
    • SwiftUI

    Featured

    • Accessibility
    • App Intents
    • Apple Intelligence
    • Games
    • Machine Learning & AI
    • Security
    • Xcode Cloud
    • Community

    Explore Community

    • Overview
    • Meet with Apple events
    • Community-driven events
    • Developer Forums
    • Open Source

    Featured

    • WWDC
    • Swift Student Challenge
    • Developer Stories
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Centers
    • Documentation

    Explore Documentation

    • Documentation Library
    • Technology Overviews
    • Sample Code
    • Human Interface Guidelines
    • Videos

    Release Notes

    • Featured Updates
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • tvOS
    • Xcode
    • Downloads

    Explore Downloads

    • All Downloads
    • Operating Systems
    • Applications
    • Design Resources

    Featured

    • Xcode
    • TestFlight
    • Fonts
    • SF Symbols
    • Icon Composer
    • Support

    Explore Support

    • Overview
    • Help Guides
    • Developer Forums
    • Feedback Assistant
    • Contact Us

    Featured

    • Account Help
    • App Review Guidelines
    • App Store Connect Help
    • Upcoming Requirements
    • Agreements and Guidelines
    • System Status
  • Quick Links

    • Events
    • News
    • Forums
    • Sample Code
    • Videos
 

Vídeos

Abrir menu Fechar menu
  • Coleções
  • Todos os vídeos
  • Sobre

Mais vídeos

  • Sobre
  • Código
  • Meet Swift OpenAPI Generator

    Discover how Swift OpenAPI Generator can help you work with HTTP server APIs whether you're extending an iOS app or writing a server in Swift. We'll show you how this package plugin can streamline your workflow and simplify your codebase by generating code from an OpenAPI document.

    Capítulos

    • 0:44 - Considerations when making API calls
    • 1:52 - Meet OpenAPI
    • 6:15 - Making API calls from your app
    • 12:33 - Adapting as the API evolves
    • 14:23 - Testing your app with mocks
    • 16:12 - Server development in Swift
    • 19:24 - Adding a new operation

    Recursos

    • URLSession Transport for Swift OpenAPI Generator
    • Swift OpenAPI Generator Runtime
    • Swift OpenAPI Generator package plugin
      • Vídeo HD
      • Vídeo SD

    Vídeos relacionados

    WWDC22

    • Meet Swift Package plugins
    • Use Xcode for server-side development
  • Buscar neste vídeo...
    • 4:17 - Example OpenAPI document

      openapi: "3.0.3"
      info:
        title: "GreetingService"
        version: "1.0.0"
      servers:
      - url: "http://localhost:8080/api"
        description: "Production"
      paths:
        /greet:
          get:
            operationId: "getGreeting"
            parameters:
            - name: "name"
              required: false
              in: "query"
              description: "Personalizes the greeting."
              schema:
                type: "string"
            responses:
              "200":
                description: "Returns a greeting"
                content:
                  application/json:
                    schema:
                      $ref: "#/components/schemas/Greeting"
    • 7:05 - CatService openapi.yaml

      openapi: "3.0.3"
      info:
        title: CatService
        version: 1.0.0
      servers:
        - url: http://localhost:8080/api
          description: "Localhost cats 🙀"
      paths:
        /emoji:
          get:
            operationId: getEmoji
            parameters:
            - name: count
              required: false
              in: query
              description: "The number of cats to return. 😽😽😽"
              schema:
                type: integer
            responses:
              '200':
                description: "Returns a random emoji, of a cat, ofc! 😻"
                content:
                  text/plain:
                    schema:
                      type: string
    • 8:03 - Making API calls from your app

      import SwiftUI
      import OpenAPIRuntime
      import OpenAPIURLSession
      
      #Preview {
          ContentView()
      }
      
      struct ContentView: View {
          @State private var emoji = "🫥"
      
          var body: some View {
              VStack {
                  Text(emoji).font(.system(size: 100))
                  Button("Get cat!") {
                      Task { try? await updateEmoji() }
                  }
              }
              .padding()
              .buttonStyle(.borderedProminent)
          }
      
          let client: Client
      
          init() {
              self.client = Client(
                  serverURL: try! Servers.server1(),
                  transport: URLSessionTransport()
              )
          }
      
          func updateEmoji() async throws {
              let response = try await client.getEmoji(Operations.getEmoji.Input())
      
              switch response {
              case let .ok(okResponse):
                  switch okResponse.body {
                  case .text(let text):
                      emoji = text
                  }
              case .undocumented(statusCode: let statusCode, _):
                  print("cat-astrophe: \(statusCode)")
                  emoji = "🙉"
              }
          }
      }
    • 9:48 - CatServiceClient openapi-generator-config.yaml

      generate:
        - types
        - client
    • 13:24 - Adapting as the API evolves

      import SwiftUI
      import OpenAPIRuntime
      import OpenAPIURLSession
      
      #Preview {
          ContentView()
      }
      
      struct ContentView: View {
          @State private var emoji = "🫥"
      
          var body: some View {
              VStack {
                  Text(emoji).font(.system(size: 100))
                  Button("Get cat!") {
                      Task { try? await updateEmoji() }
                  }
                  Button("More cats!") {
                      Task { try? await updateEmoji(count: 3) }
                  }
              }
              .padding()
              .buttonStyle(.borderedProminent)
          }
      
          let client: Client
      
          init() {
              self.client = Client(
                  serverURL: try! Servers.server1(),
                  transport: URLSessionTransport()
              )
          }
      
          func updateEmoji(count: Int = 1) async throws {
              let response = try await client.getEmoji(Operations.getEmoji.Input(
                  query: Operations.getEmoji.Input.Query(count: count)
              ))
      
              switch response {
              case let .ok(okResponse):
                  switch okResponse.body {
                  case .text(let text):
                      emoji = text
                  }
              case .undocumented(statusCode: let statusCode, _):
                  print("cat-astrophe: \(statusCode)")
                  emoji = "🙉"
              }
          }
      }
    • 15:06 - Testing your app with mocks

      import SwiftUI
      import OpenAPIRuntime
      import OpenAPIURLSession
      
      #Preview {
          ContentView(client: MockClient())
      }
      
      struct ContentView<C: APIProtocol>: View {
          @State private var emoji = "🫥"
      
          var body: some View {
              VStack {
                  Text(emoji).font(.system(size: 100))
                  Button("Get cat!") {
                      Task { try? await updateEmoji() }
                  }
                  Button("More cats!") {
                      Task { try? await updateEmoji(count: 3) }
                  }
              }
              .padding()
              .buttonStyle(.borderedProminent)
          }
      
          let client: C
      
          init(client: C) {
              self.client = client
          }
      
          init() where C == Client {
              self.client = Client(
                  serverURL: try! Servers.server1(),
                  transport: URLSessionTransport()
              )
          }
      
          func updateEmoji(count: Int = 1) async throws {
              let response = try await client.getEmoji(Operations.getEmoji.Input(
                  query: Operations.getEmoji.Input.Query(count: count)
              ))
      
              switch response {
              case let .ok(okResponse):
                  switch okResponse.body {
                  case .text(let text):
                      emoji = text
                  }
              case .undocumented(statusCode: let statusCode, _):
                  print("cat-astrophe: \(statusCode)")
                  emoji = "🙉"
              }
          }
      }
      
      struct MockClient: APIProtocol {
          func getEmoji(_ input: Operations.getEmoji.Input) async throws -> Operations.getEmoji.Output {
              let count = input.query.count ?? 1
              let emojis = String(repeating: "🤖", count: count)
              return .ok(Operations.getEmoji.Output.Ok(
                  body: .text(emojis)
              ))
          }
      }
    • 16:58 - Implementing a backend server

      import Foundation
      import OpenAPIRuntime
      import OpenAPIVapor
      import Vapor
      
      struct Handler: APIProtocol {
          func getEmoji(_ input: Operations.getEmoji.Input) async throws -> Operations.getEmoji.Output {
              let candidates = "🐱😹😻🙀😿😽😸😺😾😼"
              let chosen = String(candidates.randomElement()!)
              let count = input.query.count ?? 1
              let emojis = String(repeating: chosen, count: count)
              return .ok(Operations.getEmoji.Output.Ok(body: .text(emojis)))
          }
      }
      
      @main
      struct CatService {
          public static func main() throws {
              let app = Vapor.Application()
              let transport = VaporTransport(routesBuilder: app)
              let handler = Handler()
              try handler.registerHandlers(on: transport, serverURL: Servers.server1())
              try app.run()
          }
      }
    • 18:43 - CatService Package.swift

      // swift-tools-version: 5.8
      import PackageDescription
      
      let package = Package(
          name: "CatService",
          platforms: [
              .macOS(.v13),
          ],
          dependencies: [
              .package(
                   url: "https://github.com/apple/swift-openapi-generator",
                  .upToNextMinor(from: "0.1.0")
              ),
              .package(
                  url: "https://github.com/apple/swift-openapi-runtime",
                  .upToNextMinor(from: "0.1.0")
              ),
              .package(
                  url: "https://github.com/swift-server/swift-openapi-vapor",
                  .upToNextMinor(from: "0.1.0")
              ),
              .package(
                  url: "https://github.com/vapor/vapor",
                  .upToNextMajor(from: "4.69.2")
              ),
          ],
          targets: [
              .executableTarget(
                  name: "CatService",
                  dependencies: [
                      .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
                      .product(name: "OpenAPIVapor", package: "swift-openapi-vapor"),
                      .product(name: "Vapor", package: "vapor"),
                  ],
                  resources: [
                      .process("Resources/cat.mp4"),
                  ],
                  plugins: [
                      .plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator"),
                  ]
              ),
          ]
      )
    • 19:08 - CatService openapi.yaml

      openapi: "3.0.3"
      info:
        title: CatService
        version: 1.0.0
      servers:
        - url: http://localhost:8080/api
          description: "Localhost cats 🙀"
      paths:
        /emoji:
          get:
            operationId: getEmoji
            parameters:
            - name: count
              required: false
              in: query
              description: "The number of cats to return. 😽😽😽"
              schema:
                type: integer
            responses:
              '200':
                description: "Returns a random emoji, of a cat, ofc! 😻"
                content:
                  text/plain:
                    schema:
                      type: string
    • 19:10 - CatService openapi-generator-config.yaml

      generate:
        - types
        - server
    • 20:11 - Adding an operation to the OpenAPI document

      openapi: "3.0.3"
      info:
        title: CatService
        version: 1.0.0
      servers:
        - url: http://localhost:8080/api
          description: "Localhost cats 🙀"
      paths:
        /emoji:
          get:
            operationId: getEmoji
            parameters:
            - name: count
              required: false
              in: query
              description: "The number of cats to return. 😽😽😽"
              schema:
                type: integer
            responses:
              '200':
                description: "Returns a random emoji, of a cat, ofc! 😻"
                content:
                  text/plain:
                    schema:
                      type: string
      
        /clip:
          get:
            operationId: getClip
            responses:
              '200':
                description: "Returns a cat video! 😽"
                content:
                  video/mp4:
                    schema:
                      type: string
                      format: binary
    • 20:26 - Adding a new API operation

      import Foundation
      import OpenAPIRuntime
      import OpenAPIVapor
      import Vapor
      
      struct Handler: APIProtocol {
          func getClip(_ input: Operations.getClip.Input) async throws -> Operations.getClip.Output {
              let clipResourceURL = Bundle.module.url(forResource: "cat", withExtension: "mp4")!
              let clipData = try Data(contentsOf: clipResourceURL)
              return .ok(Operations.getClip.Output.Ok(body: .binary(clipData)))
          }
          
          func getEmoji(_ input: Operations.getEmoji.Input) async throws -> Operations.getEmoji.Output {
              let candidates = "🐱😹😻🙀😿😽😸😺😾😼"
              let chosen = String(candidates.randomElement()!)
              let count = input.query.count ?? 1
              let emojis = String(repeating: chosen, count: count)
              return .ok(Operations.getEmoji.Output.Ok(body: .text(emojis)))
          }
      }
      
      @main
      struct CatService {
          public static func main() throws {
              let app = Vapor.Application()
              let transport = VaporTransport(routesBuilder: app)
              let handler = Handler()
              try handler.registerHandlers(on: transport, serverURL: Servers.server1())
              try app.run()
          }
      }

Developer Footer

  • Vídeos
  • WWDC23
  • Meet Swift OpenAPI Generator
  • Open Menu Close Menu
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store
    Open Menu Close Menu
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • Icon Composer
    • SF Symbols
    Open Menu Close Menu
    • Accessibility
    • Accessories
    • Apple Intelligence
    • Audio & Video
    • Augmented Reality
    • Business
    • Design
    • Distribution
    • Education
    • Games
    • Health & Fitness
    • In-App Purchase
    • Localization
    • Maps & Location
    • Machine Learning & AI
    • Security
    • Safari & Web
    Open Menu Close Menu
    • Documentation
    • Downloads
    • Sample Code
    • Videos
    Open Menu Close Menu
    • Help Guides & Articles
    • Contact Us
    • Forums
    • Feedback & Bug Reporting
    • System Status
    Open Menu Close Menu
    • Apple Developer
    • App Store Connect
    • Certificates, IDs, & Profiles
    • Feedback Assistant
    Open Menu Close Menu
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program
    • Mini Apps Partner Program
    • News Partner Program
    • Video Partner Program
    • Security Bounty Program
    • Security Research Device Program
    Open Menu Close Menu
    • Meet with Apple
    • Apple Developer Centers
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Academies
    • WWDC
    Read the latest news.
    Get the Apple Developer app.
    Copyright © 2026 Apple Inc. All rights reserved.
    Terms of Use Privacy Policy Agreements and Guidelines