feat: sse client optionally skip the endpoint event (#187)

This commit is contained in:
4t145 2025-05-18 18:55:02 +08:00 • committed by GitHub
commit 35282ee34c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -113,16 +113,20 @@ impl<C: SseClient> SseClientTransport<C> {
config: SseClientConfig, config: SseClientConfig,
) -> Result<Self, SseTransportError<C::Error>> { ) -> Result<Self, SseTransportError<C::Error>> {
let mut sse_stream = client.get_stream(config.uri.clone(), None, None).await?; let mut sse_stream = client.get_stream(config.uri.clone(), None, None).await?;
// wait the endpoint event let endpoint = if let Some(endpoint) = config.use_endpoint.clone() {
let endpoint = loop { endpoint
let sse = sse_stream } else {
.next() // wait the endpoint event
.await loop {
.ok_or(SseTransportError::UnexpectedEndOfStream)??; let sse = sse_stream
let Some("endpoint") = sse.event.as_deref() else { .next()
continue; .await
}; .ok_or(SseTransportError::UnexpectedEndOfStream)??;
break sse.data.unwrap_or_default(); let Some("endpoint") = sse.event.as_deref() else {
continue;
};
break sse.data.unwrap_or_default();
}
}; };
let post_uri: Arc<str> = format!( let post_uri: Arc<str> = format!(
"{}/{}", "{}/{}",
@ -151,6 +155,8 @@ impl<C: SseClient> SseClientTransport<C> {
pub struct SseClientConfig { pub struct SseClientConfig {
pub uri: Arc<str>, pub uri: Arc<str>,
pub retry_policy: Arc<dyn SseRetryPolicy>, pub retry_policy: Arc<dyn SseRetryPolicy>,
/// if this is settled, the client will use this endpoint to send message and skip get the endpoint event
pub use_endpoint: Option<String>,
} }
impl Default for SseClientConfig { impl Default for SseClientConfig {
@ -158,6 +164,7 @@ impl Default for SseClientConfig {
Self { Self {
uri: "".into(), uri: "".into(),
retry_policy: Arc::new(super::common::client_side_sse::FixedInterval::default()), retry_policy: Arc::new(super::common::client_side_sse::FixedInterval::default()),
use_endpoint: None,
} }
} }
} }