use crate::stream_body::{ReqwestStreamBody, StreamBodyRequest};
use futures::Stream;
use http_streams_core::{JsonArrayStreamFormat, JsonNewLineStreamFormat};
use serde::Serialize;
pub trait JsonStreamRequest {
fn json_array_stream_body<S, T>(self, stream: S) -> reqwest::RequestBuilder
where
T: Serialize + Send + 'static,
S: Stream<Item = T> + Send + 'static;
fn try_json_array_stream_body<S, T, E>(self, stream: S) -> reqwest::RequestBuilder
where
T: Serialize + Send + 'static,
S: Stream<Item = Result<T, E>> + Send + 'static,
E: Into<Box<dyn std::error::Error + Send + Sync>> + Send + 'static;
fn json_nl_stream_body<S, T>(self, stream: S) -> reqwest::RequestBuilder
where
T: Serialize + Send + 'static,
S: Stream<Item = T> + Send + 'static;
fn try_json_nl_stream_body<S, T, E>(self, stream: S) -> reqwest::RequestBuilder
where
T: Serialize + Send + 'static,
S: Stream<Item = Result<T, E>> + Send + 'static,
E: Into<Box<dyn std::error::Error + Send + Sync>> + Send + 'static;
}
impl JsonStreamRequest for reqwest::RequestBuilder {
fn json_array_stream_body<S, T>(self, stream: S) -> reqwest::RequestBuilder
where
T: Serialize + Send + 'static,
S: Stream<Item = T> + Send + 'static,
{
self.stream_body(ReqwestStreamBody::new(JsonArrayStreamFormat::new(), stream))
}
fn try_json_array_stream_body<S, T, E>(self, stream: S) -> reqwest::RequestBuilder
where
T: Serialize + Send + 'static,
S: Stream<Item = Result<T, E>> + Send + 'static,
E: Into<Box<dyn std::error::Error + Send + Sync>> + Send + 'static,
{
self.stream_body(ReqwestStreamBody::try_new(
JsonArrayStreamFormat::new(),
stream,
))
}
fn json_nl_stream_body<S, T>(self, stream: S) -> reqwest::RequestBuilder
where
T: Serialize + Send + 'static,
S: Stream<Item = T> + Send + 'static,
{
self.stream_body(ReqwestStreamBody::new(
JsonNewLineStreamFormat::new(),
stream,
))
}
fn try_json_nl_stream_body<S, T, E>(self, stream: S) -> reqwest::RequestBuilder
where
T: Serialize + Send + 'static,
S: Stream<Item = Result<T, E>> + Send + 'static,
E: Into<Box<dyn std::error::Error + Send + Sync>> + Send + 'static,
{
self.stream_body(ReqwestStreamBody::try_new(
JsonNewLineStreamFormat::new(),
stream,
))
}
}