use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use super::http_request_body_byte_stream::HttpRequestBodyByteStream;
type HttpRequestBodyStreamFactoryFuture =
Pin<Box<dyn Future<Output = HttpRequestBodyByteStream> + Send + 'static>>;
type HttpRequestBodyStreamFactoryFn =
dyn Fn() -> HttpRequestBodyStreamFactoryFuture + Send + Sync + 'static;
#[derive(Clone)]
pub struct HttpRequestStreamingBody {
factory: Arc<HttpRequestBodyStreamFactoryFn>,
}
impl std::fmt::Debug for HttpRequestStreamingBody {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HttpRequestStreamingBody")
.finish_non_exhaustive()
}
}
impl HttpRequestStreamingBody {
pub fn new<F>(factory: F) -> Self
where
F: Fn() -> HttpRequestBodyStreamFactoryFuture + Send + Sync + 'static,
{
Self {
factory: Arc::new(factory),
}
}
pub(crate) async fn to_reqwest_body(&self) -> reqwest::Body {
let stream = (self.factory)().await;
reqwest::Body::wrap_stream(stream)
}
}