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

feat: added event stream support #448

Closed
wants to merge 4 commits into from
Closed
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
85 changes: 18 additions & 67 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ dyn-clone = { version = "1.0", default-features = false }
either = { version = "1.9.0", default-features = false }
env_logger = { version = "0.10", default-features = false }
escargot = { version = "0.5", default-features = false }
eventsource-stream = { version = "0.2", default-features = false, features = [
"std",
] }
futures = { version = "0.3", default-features = false, features = ["std"] }
getrandom = { version = "0.2", default-features = false }
getset = { version = "0.1", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions crates/components/wick-http-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = "SQL Database component for the wick project."
[features]

[dependencies]
bytes = { workspace = true }
wick-packet = { workspace = true, features = ["rt-tokio", "invocation"] }
wick-interface-http = { workspace = true }
flow-component = { workspace = true, features = ["invocation"] }
Expand All @@ -28,6 +29,7 @@ serde = { workspace = true, features = ["derive"] }
futures = { workspace = true }
thiserror = { workspace = true }
serde_json = { workspace = true }
eventsource-stream = { workspace = true }

#
[dev-dependencies]
Expand Down
62 changes: 61 additions & 1 deletion crates/components/wick-http-client/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::sync::Arc;

use anyhow::anyhow;
use eventsource_stream::Eventsource;
use flow_component::{BoxFuture, Component, ComponentError, RuntimeCallback};
use futures::{Stream, StreamExt, TryStreamExt};
use reqwest::header::CONTENT_TYPE;
Expand All @@ -15,7 +16,7 @@ use wick_config::config::components::{
HttpClientOperationDefinition,
OperationConfig,
};
use wick_config::config::{Codec, HttpMethod, LiquidJsonConfig, Metadata, UrlResource};
use wick_config::config::{Codec, HttpEvent, HttpMethod, LiquidJsonConfig, Metadata, UrlResource};
use wick_config::{ConfigValidation, Resolver};
use wick_interface_types::{ComponentSignature, OperationSignatures};
use wick_packet::{Base64Bytes, FluxChannel, Invocation, Observer, Packet, PacketSender, PacketStream, RuntimeConfig};
Expand Down Expand Up @@ -281,6 +282,9 @@ async fn handle(
}
Codec::FormData => request_builder.form(&body),
Codec::Text => request_builder.body(body.to_string()),
Codec::EventStream => {
unimplemented!("Event stream is not a valid client content-type")
}
}
} else {
request_builder
Expand Down Expand Up @@ -322,9 +326,11 @@ async fn handle(
match value {
"application/json" => Codec::Json,
"application/x-www-form-urlencoded" => Codec::FormData,
"text/event-stream" => Codec::EventStream,
_ => Codec::Raw,
}
});

let (our_response, body_stream) = match crate::conversions::to_wick_response(response) {
Ok(r) => r,
Err(e) => {
Expand Down Expand Up @@ -357,6 +363,22 @@ fn output_task(
) -> BoxFuture<'static, ()> {
let task = async move {
match codec {
Codec::EventStream => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Handling SSE responses is more of a special handling of a response type, vs a codec. The codec would describe how to handle the data in an event.

Since the content-type: text/event-stream is a standard, should this be handled automatically? The data would be processed as whatever the codec dictates before being sent to the output?

let mut stream = body_stream.map(Into::into).eventsource();
while let Some(event) = stream.next().await {
match event {
Ok(event) => {
let wick_event = HttpEvent::new(Some(event.event), event.data, Some(event.id), event.retry);
span.in_scope(|| debug!("{} {}", format!("{:?}", wick_event), "http:client:response_body"));
let _ = tx.send(Packet::encode("body", wick_event));
}
Err(e) => {
let _ = tx.error(wick_packet::Error::component_error(e.to_string()));
return;
}
}
}
}
Codec::Json => {
let bytes: Vec<Base64Bytes> = match body_stream.try_collect().await {
Ok(r) => r,
Expand Down Expand Up @@ -659,5 +681,43 @@ mod test {

Ok(())
}

#[test_logger::test(tokio::test)]
async fn test_event_stream() -> Result<()> {
let (app_config, component_config) = get_config();
let comp = get_component(&app_config, component_config);

let event_stream = "data: {\"id\":\"1\",\"object\":\"event1\"}\n\n\
data: {\"id\":\"2\",\"object\":\"event2\"}\n\n";
let packets = packet_stream!(("input", event_stream));

let invocation = Invocation::test(
"test_event_stream",
Entity::local("event_stream_op"),
packets,
Default::default(),
)?;
let stream = comp
.handle(invocation, Default::default(), panic_callback())
.await?
.filter(|p| futures::future::ready(p.as_ref().map_or(false, |p| p.has_data())))
.collect::<Vec<_>>()
.await;

let packets = stream.into_iter().collect::<Result<Vec<_>, _>>()?;
for packet in packets {
if packet.port() == "body" {
let response: HttpEvent = packet.decode().unwrap();
let response_id = response.get_id().as_ref().unwrap();
let response_event = response.get_event().as_ref().unwrap();
assert!(response_id == "1" && response_event == "event1" || response_id == "2" && response_event == "event2");
} else {
let response: HttpResponse = packet.decode().unwrap();
assert_eq!(response.version, HttpVersion::Http11);
}
}

Ok(())
}
}
}
3 changes: 3 additions & 0 deletions crates/components/wick-http-client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ pub enum Error {

#[error("Proxy and baseurl must not be the same: {0}")]
ProxyLoop(Url),

#[error("Conversion error: {0}")]
Conversion(String),
}
18 changes: 17 additions & 1 deletion crates/interfaces/wick-interface-http/component.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
name: http
kind: wick/types@v1
metadata:
version: 0.4.0
version: 0.5.0
package:
registry:
host: registry.candle.dev
Expand Down Expand Up @@ -242,3 +242,19 @@ types:
types:
- HttpRequest
- HttpResponse
- name: HttpEvent
kind: wick/type/struct@v1
description: HTTP server side event
fields:
- name: event
type: string?
description: The event name if given
- name: data
type: string
description: The event data
- name: id
type: string?
description: The event id if given
- name: retry
type: u32?
description: Retry duration if given
3 changes: 3 additions & 0 deletions crates/wick/wick-config/definitions/v1/manifest.apex
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,9 @@ enum Codec {

"Raw text"
Text = 3 as "text",

"Eventstream"
EventStream = 4 as "event-stream",
}

"Supported HTTP methods"
Expand Down
1 change: 1 addition & 0 deletions crates/wick/wick-config/docs/v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,7 @@ Any one of the following types:
| Raw | unknown type | Raw bytes |
| FormData | unknown type | Form Data |
| Text | unknown type | Raw text |
| EventStream | unknown type | Eventstream |


--------
Expand Down
3 changes: 2 additions & 1 deletion crates/wick/wick-config/json-schema/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2980,7 +2980,8 @@
"Json",
"Raw",
"FormData",
"Text"
"Text",
"EventStream"
]
},
"v1.HttpMethod": {
Expand Down
2 changes: 1 addition & 1 deletion crates/wick/wick-config/json-schema/v1/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2588,7 +2588,7 @@

"v1.Codec": {
"$anchor": "v1.Codec",
"enum": ["Json", "Raw", "FormData", "Text"]
"enum": ["Json", "Raw", "FormData", "Text", "EventStream"]
},

"v1.HttpMethod": {
Expand Down
2 changes: 1 addition & 1 deletion crates/wick/wick-config/src/config/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub use self::error_behavior::ErrorBehavior;
pub use self::exposed_resources::{ExposedVolume, ExposedVolumeBuilder};
pub use self::glob::Glob;
pub use self::host_definition::{HostConfig, HostConfigBuilder, HttpConfig, HttpConfigBuilder};
pub use self::http::{Codec, HttpMethod};
pub use self::http::{Codec, HttpEvent, HttpMethod};
pub use self::import_definition::ImportDefinition;
pub use self::interface::InterfaceDefinition;
pub use self::liquid_json_config::LiquidJsonConfig;
Expand Down
Loading
Loading