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

Initial message fix #593

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -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"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to keep it using AHC? Since it doesn't require the initial message hack?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@czechboy0 we could, it doesn't matter in the end as there something with transport happening (both for vapor and hbv1), so we anyway need initial message

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there something with transport happening (both for vapor and hbv1)

Can you elaborate what you're seeing? Are you saying even when using AHC, you still don't get the stream established without an initial message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate what you're seeing? Are you saying even when using AHC, you still don't get the stream established without an initial message?

Yep, exactly. I will try to investigate, but for now think we can just update the example.

],
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")]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
import OpenAPIRuntime
import OpenAPIAsyncHTTPClient
import OpenAPIURLSession
import Foundation

@main struct BidirectionalEventStreamsClient {
Expand All @@ -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<Components.Schemas.Greeting>.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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
//
//===----------------------------------------------------------------------===//
import OpenAPIRuntime
import OpenAPIHummingbird
import Hummingbird
import OpenAPIVapor
import Vapor
import Foundation

struct Handler: APIProtocol {
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down