diff --git a/Examples/bidirectional-event-streams-client-example/Package.swift b/Examples/bidirectional-event-streams-client-example/Package.swift index 570e4f16..b6cf1c4f 100644 --- a/Examples/bidirectional-event-streams-client-example/Package.swift +++ b/Examples/bidirectional-event-streams-client-example/Package.swift @@ -20,14 +20,14 @@ let package = Package( 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/swift-server/swift-openapi-async-http-client", from: "1.0.0"), + .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"), ], targets: [ .executableTarget( name: "BidirectionalEventStreamsClient", dependencies: [ .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), - .product(name: "OpenAPIAsyncHTTPClient", package: "swift-openapi-async-http-client"), + .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), ], plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")] ) diff --git a/Examples/bidirectional-event-streams-client-example/Sources/BidirectionalEventStreamsClient/BidirectionalEventStreamsClient.swift b/Examples/bidirectional-event-streams-client-example/Sources/BidirectionalEventStreamsClient/BidirectionalEventStreamsClient.swift index b5b82b26..ced14be7 100644 --- a/Examples/bidirectional-event-streams-client-example/Sources/BidirectionalEventStreamsClient/BidirectionalEventStreamsClient.swift +++ b/Examples/bidirectional-event-streams-client-example/Sources/BidirectionalEventStreamsClient/BidirectionalEventStreamsClient.swift @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// import OpenAPIRuntime -import OpenAPIAsyncHTTPClient +import OpenAPIURLSession import Foundation @main struct BidirectionalEventStreamsClient { @@ -21,10 +21,13 @@ import Foundation "Good evening, %@!", ] static func main() async throws { - let client = Client(serverURL: URL(string: "http://localhost:8080/api")!, transport: AsyncHTTPClientTransport()) + let client = Client(serverURL: URL(string: "http://localhost:8080/api")!, transport: URLSessionTransport()) do { print("Sending and fetching back greetings using JSON Lines") let (stream, continuation) = AsyncStream.makeStream() + /// It is important to note that URLSession will return the stream only after at least some bytes of the body have been received (see [comment](https://github.com/apple/swift-openapi-urlsession/blob/main/Tests/OpenAPIURLSessionTests/URLSessionBidirectionalStreamingTests/URLSessionBidirectionalStreamingTests.swift#L193-L206)). + /// Workaround for now is to send a `connecting` or some other kind of heartbeat message first. + continuation.yield(.init(message: "connecting")) /// 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( diff --git a/Examples/bidirectional-event-streams-server-example/Package.swift b/Examples/bidirectional-event-streams-server-example/Package.swift index 5b079ae6..a5c5777a 100644 --- a/Examples/bidirectional-event-streams-server-example/Package.swift +++ b/Examples/bidirectional-event-streams-server-example/Package.swift @@ -20,16 +20,16 @@ let package = Package( 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/swift-server/swift-openapi-vapor", from: "1.0.0"), + .package(url: "https://github.com/vapor/vapor", from: "4.89.0"), ], targets: [ .executableTarget( name: "BidirectionalEventStreamsServer", dependencies: [ .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), - .product(name: "OpenAPIHummingbird", package: "swift-openapi-hummingbird"), - .product(name: "Hummingbird", package: "hummingbird"), + .product(name: "OpenAPIVapor", package: "swift-openapi-vapor"), + .product(name: "Vapor", package: "vapor"), ], plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")] ) diff --git a/Examples/bidirectional-event-streams-server-example/Sources/BidirectionalEventStreamsServer/BidirectionalEventStreamsServer.swift b/Examples/bidirectional-event-streams-server-example/Sources/BidirectionalEventStreamsServer/BidirectionalEventStreamsServer.swift index 01da6351..bc99046a 100644 --- a/Examples/bidirectional-event-streams-server-example/Sources/BidirectionalEventStreamsServer/BidirectionalEventStreamsServer.swift +++ b/Examples/bidirectional-event-streams-server-example/Sources/BidirectionalEventStreamsServer/BidirectionalEventStreamsServer.swift @@ -12,8 +12,8 @@ // //===----------------------------------------------------------------------===// import OpenAPIRuntime -import OpenAPIHummingbird -import Hummingbird +import OpenAPIVapor +import Vapor import Foundation struct Handler: APIProtocol { @@ -33,10 +33,10 @@ struct Handler: APIProtocol { @main struct BidirectionalEventStreamsServer { static func main() async throws { - let router = Router() + let app = try await Vapor.Application.make() + let transport = VaporTransport(routesBuilder: app) let handler = Handler() - try handler.registerHandlers(on: router, serverURL: URL(string: "/api")!) - let app = Application(router: router, configuration: .init()) - try await app.run() + try handler.registerHandlers(on: transport, serverURL: URL(string: "/api")!) + try await app.execute() } } diff --git a/Examples/bidirectional-event-streams-server-example/Sources/BidirectionalEventStreamsServer/GreetingStream.swift b/Examples/bidirectional-event-streams-server-example/Sources/BidirectionalEventStreamsServer/GreetingStream.swift index 0e87bb13..c7c8cad0 100644 --- a/Examples/bidirectional-event-streams-server-example/Sources/BidirectionalEventStreamsServer/GreetingStream.swift +++ b/Examples/bidirectional-event-streams-server-example/Sources/BidirectionalEventStreamsServer/GreetingStream.swift @@ -51,8 +51,12 @@ actor StreamStorage: Sendable { try Task.checkCancellation() print("Recieved a message \(message)") print("Sending greeting back for \(id)") - let greetingText = String(format: message.message, name) - continuation.yield(.init(message: greetingText)) + let responseText: String = + switch message.message { + case "connecting": "\(name) connected" + default: String(format: message.message, name) + } + continuation.yield(.init(message: responseText)) } continuation.finish() }