Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Examples and IntegrationTest to use idiomatic naming #706

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -609,15 +609,15 @@
repositoryURL = "https://github.com/apple/swift-openapi-generator.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.0.0;
minimumVersion = 1.6.0;
};
};
3A133D0E2B1F2A600008DD5E /* XCRemoteSwiftPackageReference "swift-openapi-runtime" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/apple/swift-openapi-runtime.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.0.0;
minimumVersion = 1.7.0;
};
};
3A133D112B1F2A730008DD5E /* XCRemoteSwiftPackageReference "swift-openapi-urlsession" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct ContentView: View {

// A mock client used in previews and tests to avoid making live network calls.
struct MockClient: APIProtocol {
func getGreeting(_ input: Operations.getGreeting.Input) async throws -> Operations.getGreeting.Output {
func getGreeting(_ input: Operations.GetGreeting.Input) async throws -> Operations.GetGreeting.Output {
let name = input.query.name ?? "Stranger"
return .ok(.init(body: .json(.init(message: "(Mock) Hello, \(name)!"))))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ generate:
- types
- client
accessModifier: internal
namingStrategy: idiomatic
4 changes: 2 additions & 2 deletions Examples/auth-client-middleware-example/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ let package = Package(
name: "auth-client-middleware-example",
platforms: [.macOS(.v11), .iOS(.v14), .tvOS(.v14), .watchOS(.v7), .visionOS(.v1)],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"),
.package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-http-types", from: "1.0.2"),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ generate:
- types
- client
accessModifier: internal
namingStrategy: idiomatic
4 changes: 2 additions & 2 deletions Examples/auth-server-middleware-example/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ let package = Package(
name: "auth-server-middleware-example",
platforms: [.macOS(.v10_15)],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"),
.package(url: "https://github.com/swift-server/swift-openapi-vapor", from: "1.0.0"),
.package(url: "https://github.com/vapor/vapor", from: "4.89.0"),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Vapor
import AuthenticationServerMiddleware

struct Handler: APIProtocol {
func getGreeting(_ input: Operations.getGreeting.Input) async throws -> Operations.getGreeting.Output {
func getGreeting(_ input: Operations.GetGreeting.Input) async throws -> Operations.GetGreeting.Output {
// Extract the authenticated user, if present.
// If unauthenticated, return the 401 HTTP status code.
// Note that the 401 is defined in the OpenAPI document, allowing the client
Expand All @@ -32,7 +32,7 @@ struct Handler: APIProtocol {

@main struct HelloWorldVaporServer {
static func main() async throws {
let app = Vapor.Application()
let app = try await Vapor.Application.make()
let transport = VaporTransport(routesBuilder: app)
let handler = Handler()
try handler.registerHandlers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ generate:
- types
- server
accessModifier: internal
namingStrategy: idiomatic
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ let package = Package(
name: "bidirectional-event-streams-client-example",
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.2.0"),
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"),
.package(url: "https://github.com/swift-server/swift-openapi-async-http-client", from: "1.0.0"),
],
targets: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import Foundation
let (stream, continuation) = AsyncStream<Components.Schemas.Greeting>.makeStream()
/// To keep it simple, using JSON Lines, as it most straightforward and easy way to have streams.
/// For SSE and JSON Sequences cases please check `event-streams-client-example`.
let requestBody: Operations.getGreetingsStream.Input.Body = .application_jsonl(
let requestBody: Operations.GetGreetingsStream.Input.Body = .applicationJsonl(
.init(stream.asEncodedJSONLines(), length: .unknown, iterationBehavior: .single)
)
let response = try await client.getGreetingsStream(query: .init(name: "Example"), body: requestBody)
let greetingStream = try response.ok.body.application_jsonl.asDecodedJSONLines(
let greetingStream = try response.ok.body.applicationJsonl.asDecodedJSONLines(
of: Components.Schemas.Greeting.self
)
try await withThrowingTaskGroup(of: Void.self) { group in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ generate:
- types
- client
accessModifier: internal
namingStrategy: idiomatic
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import PackageDescription

let package = Package(
name: "bidirectional-event-streams-server-example",
platforms: [.macOS(.v10_15)],
platforms: [.macOS(.v14)],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.2.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-rc.1"),
.package(url: "https://github.com/swift-server/swift-openapi-hummingbird.git", from: "2.0.0-beta.4"),
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.5.0"),
.package(url: "https://github.com/swift-server/swift-openapi-hummingbird.git", from: "2.0.1"),
],
targets: [
.executableTarget(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import Foundation

struct Handler: APIProtocol {
private let storage: StreamStorage = .init()
func getGreetingsStream(_ input: Operations.getGreetingsStream.Input) async throws
-> Operations.getGreetingsStream.Output
func getGreetingsStream(_ input: Operations.GetGreetingsStream.Input) async throws
-> Operations.GetGreetingsStream.Output
{
let eventStream = await self.storage.makeStream(input: input)
/// To keep it simple, using JSON Lines, as it most straightforward and easy way to have streams.
/// For SSE and JSON Sequences cases please check `event-streams-server-example`.
let responseBody = Operations.getGreetingsStream.Output.Ok.Body.application_jsonl(
let responseBody = Operations.GetGreetingsStream.Output.Ok.Body.applicationJsonl(
.init(eventStream.asEncodedJSONLines(), length: .unknown, iterationBehavior: .single)
)
return .ok(.init(body: responseBody))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ actor StreamStorage: Sendable {
task.cancel()
print("Canceled stream \(id)")
}
func makeStream(input: Operations.getGreetingsStream.Input) -> AsyncStream<Components.Schemas.Greeting> {
func makeStream(input: Operations.GetGreetingsStream.Input) -> AsyncStream<Components.Schemas.Greeting> {
let name = input.query.name ?? "Stranger"
let id = UUID().uuidString
print("Creating stream \(id) for name: \(name)")
Expand All @@ -44,7 +44,7 @@ actor StreamStorage: Sendable {
}
let inputStream =
switch input.body {
case .application_jsonl(let body): body.asDecodedJSONLines(of: Components.Schemas.Greeting.self)
case .applicationJsonl(let body): body.asDecodedJSONLines(of: Components.Schemas.Greeting.self)
}
let task = Task<Void, any Error> {
for try await message in inputStream {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ generate:
- types
- server
accessModifier: internal
namingStrategy: idiomatic
4 changes: 2 additions & 2 deletions Examples/command-line-client-example/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ let package = Package(
name: "command-line-client-example",
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"),
.package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.3.0"),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ generate:
- types
- client
accessModifier: internal
namingStrategy: idiomatic
4 changes: 2 additions & 2 deletions Examples/curated-client-library-example/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ let package = Package(
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)],
products: [.library(name: "CuratedLibraryClient", targets: ["CuratedLibraryClient"])],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"),
.package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"),
],
targets: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ generate:
- types
- client
accessModifier: internal
namingStrategy: idiomatic
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct TestClient: APIProtocol {
var name: String?
}

func getGreeting(_ input: Operations.getGreeting.Input) async throws -> Operations.getGreeting.Output {
func getGreeting(_ input: Operations.GetGreeting.Input) async throws -> Operations.GetGreeting.Output {
guard !shouldFail else { throw TestError(name: input.query.name) }
let name = input.query.name ?? "Stranger"
return .ok(.init(body: .json(.init(message: "(Test) Hello, \(name)!"))))
Expand Down
4 changes: 2 additions & 2 deletions Examples/event-streams-client-example/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ let package = Package(
name: "event-streams-client-example",
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.2.0"),
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"),
.package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"),
],
targets: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import Foundation
print("Fetching greetings using JSON Lines")
let response = try await client.getGreetingsStream(
query: .init(name: "Example", count: 3),
headers: .init(accept: [.init(contentType: .application_jsonl)])
headers: .init(accept: [.init(contentType: .applicationJsonl)])
)
let greetingStream = try response.ok.body.application_jsonl.asDecodedJSONLines(
let greetingStream = try response.ok.body.applicationJsonl.asDecodedJSONLines(
of: Components.Schemas.Greeting.self
)
for try await greeting in greetingStream { print("Got greeting: \(greeting.message)") }
Expand All @@ -33,9 +33,9 @@ import Foundation
print("Fetching greetings using JSON Sequence")
let response = try await client.getGreetingsStream(
query: .init(name: "Example", count: 3),
headers: .init(accept: [.init(contentType: .application_json_hyphen_seq)])
headers: .init(accept: [.init(contentType: .applicationJsonSeq)])
)
let greetingStream = try response.ok.body.application_json_hyphen_seq.asDecodedJSONSequence(
let greetingStream = try response.ok.body.applicationJsonSeq.asDecodedJSONSequence(
of: Components.Schemas.Greeting.self
)
for try await greeting in greetingStream { print("Got greeting: \(greeting.message)") }
Expand All @@ -44,9 +44,9 @@ import Foundation
print("Fetching greetings using Server-sent Events")
let response = try await client.getGreetingsStream(
query: .init(name: "Example", count: 3),
headers: .init(accept: [.init(contentType: .text_event_hyphen_stream)])
headers: .init(accept: [.init(contentType: .textEventStream)])
)
let greetingStream = try response.ok.body.text_event_hyphen_stream.asDecodedServerSentEventsWithJSONData(
let greetingStream = try response.ok.body.textEventStream.asDecodedServerSentEventsWithJSONData(
of: Components.Schemas.Greeting.self
)
for try await greeting in greetingStream { print("Got greeting: \(greeting.data?.message ?? "<nil>")") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ generate:
- types
- client
accessModifier: internal
namingStrategy: idiomatic
4 changes: 2 additions & 2 deletions Examples/event-streams-server-example/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ let package = Package(
name: "event-streams-server-example",
platforms: [.macOS(.v10_15)],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.2.0"),
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"),
.package(url: "https://github.com/swift-server/swift-openapi-vapor", from: "1.0.0"),
.package(url: "https://github.com/vapor/vapor", from: "4.89.0"),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ import Vapor

struct Handler: APIProtocol {
private let storage: StreamStorage = .init()
func getGreetingsStream(_ input: Operations.getGreetingsStream.Input) async throws
-> Operations.getGreetingsStream.Output
func getGreetingsStream(_ input: Operations.GetGreetingsStream.Input) async throws
-> Operations.GetGreetingsStream.Output
{
let name = input.query.name ?? "Stranger"
let count = input.query.count ?? 10
let eventStream = storage.makeStream(name: name, count: count)
let responseBody: Operations.getGreetingsStream.Output.Ok.Body
let responseBody: Operations.GetGreetingsStream.Output.Ok.Body
// Default to `application/jsonl`, if no other content type requested through the `Accept` header.
let chosenContentType = input.headers.accept.sortedByQuality().first ?? .init(contentType: .application_jsonl)
let chosenContentType = input.headers.accept.sortedByQuality().first ?? .init(contentType: .applicationJsonl)
switch chosenContentType.contentType {
case .application_jsonl, .other:
responseBody = .application_jsonl(
case .applicationJsonl, .other:
responseBody = .applicationJsonl(
.init(eventStream.asEncodedJSONLines(), length: .unknown, iterationBehavior: .single)
)
case .application_json_hyphen_seq:
responseBody = .application_json_hyphen_seq(
case .applicationJsonSeq:
responseBody = .applicationJsonSeq(
.init(eventStream.asEncodedJSONSequence(), length: .unknown, iterationBehavior: .single)
)
case .text_event_hyphen_stream:
responseBody = .text_event_hyphen_stream(
case .textEventStream:
responseBody = .textEventStream(
.init(
eventStream.map { greeting in
ServerSentEventWithJSONData(
Expand All @@ -58,7 +58,7 @@ struct Handler: APIProtocol {

@main struct EventStreamsServer {
static func main() async throws {
let app = Vapor.Application()
let app = try await Vapor.Application.make()
let transport = VaporTransport(routesBuilder: app)
let handler = Handler()
try handler.registerHandlers(on: transport, serverURL: URL(string: "/api")!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ generate:
- types
- server
accessModifier: internal
namingStrategy: idiomatic
4 changes: 2 additions & 2 deletions Examples/hello-world-async-http-client-example/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ let package = Package(
name: "hello-world-async-http-client-example",
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"),
.package(url: "https://github.com/swift-server/swift-openapi-async-http-client", from: "1.0.0"),
],
targets: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ generate:
- types
- client
accessModifier: internal
namingStrategy: idiomatic
10 changes: 5 additions & 5 deletions Examples/hello-world-hummingbird-server-example/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import PackageDescription

let package = Package(
name: "hello-world-hummingbird-server-example",
platforms: [.macOS(.v10_15)],
platforms: [.macOS(.v14)],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
.package(url: "https://github.com/swift-server/swift-openapi-hummingbird", from: "1.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird", from: "1.10.1"),
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"),
.package(url: "https://github.com/swift-server/swift-openapi-hummingbird", from: "2.0.1"),
.package(url: "https://github.com/hummingbird-project/hummingbird", from: "2.5.0"),
],
targets: [
.executableTarget(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ import Hummingbird
import Foundation

struct Handler: APIProtocol {
func getGreeting(_ input: Operations.getGreeting.Input) async throws -> Operations.getGreeting.Output {
func getGreeting(_ input: Operations.GetGreeting.Input) async throws -> Operations.GetGreeting.Output {
let name = input.query.name ?? "Stranger"
return .ok(.init(body: .json(.init(message: "Hello, \(name)!"))))
}
}

@main struct HelloWorldHummingbirdServer {
static func main() async throws {
let app = Hummingbird.HBApplication()
let transport = HBOpenAPITransport(app)
let router = Router()
let handler = Handler()
try handler.registerHandlers(on: transport, serverURL: URL(string: "/api")!)
try await app.asyncRun()
try handler.registerHandlers(on: router, serverURL: URL(string: "/api")!)
let app = Application(router: router, configuration: .init())
try await app.run()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ generate:
- types
- server
accessModifier: internal
namingStrategy: idiomatic
Loading
Loading