diff --git a/Cargo.lock b/Cargo.lock index 1086b37..2a60726 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -212,7 +212,7 @@ dependencies = [ [[package]] name = "client-util" -version = "0.1.0" +version = "0.1.1" dependencies = [ "base64", "bytes", diff --git a/Cargo.toml b/Cargo.toml index 22ce9f9..41b358f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "client-util" -version = "0.1.0" +version = "0.1.1" edition = "2021" authors = ["4t145"] description = "Help you to build requests and handle responses by several extension trait!" diff --git a/src/client/hyper.rs b/src/client/hyper.rs index 7edd2ec..7d27f23 100644 --- a/src/client/hyper.rs +++ b/src/client/hyper.rs @@ -1,7 +1,7 @@ pub use hyper_util::client::legacy::connect::HttpConnector; use hyper_util::client::legacy::{connect::Connect, Client}; -type HyperClient = Client; +pub type HyperClient = Client; pub fn build_hyper_client_with_connector(connector: C) -> Client where @@ -83,7 +83,7 @@ mod tls { } use super::build_hyper_client_with_connector; - type HyperTlsClient = Client, crate::DynBody>; + pub type HyperTlsClient = Client, crate::DynBody>; pub fn build_connector_with_tls_config( tls_config: rustls::ClientConfig, diff --git a/src/lib.rs b/src/lib.rs index 1b6540f..ee57256 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,6 +43,8 @@ pub use error::{Error, ErrorKind, Result}; // re-export pub use http; +pub use http_body; +pub use http_body_util; pub mod prelude { pub use crate::body::*; diff --git a/src/request.rs b/src/request.rs index ce2b5d0..be16f19 100644 --- a/src/request.rs +++ b/src/request.rs @@ -1,18 +1,19 @@ #[cfg(feature = "multipart")] mod multipart; use bytes::Bytes; +use futures_core::Stream; use http::request::Builder; use http::HeaderValue; use http::Request; use http::Response; use http::{header::CONTENT_TYPE, Uri}; +use http_body_util::StreamBody; use http_body_util::{combinators::UnsyncBoxBody, Empty, Full}; #[cfg(feature = "multipart")] pub use multipart::*; -use std::future::Future; - #[cfg(feature = "serde")] use serde::Serialize; +use std::future::Future; use crate::body::{empty, full}; use crate::client::ClientBody; @@ -37,6 +38,8 @@ pub trait RequestExt: Sized { #[cfg(feature = "form")] #[cfg_attr(docsrs, doc(cfg(feature = "form")))] fn form(self, form: &T) -> crate::Result>>; + #[cfg(feature = "stream")] + fn stream(self, stream: S) -> crate::Result>>; fn plain_text(self, body: impl Into) -> crate::Result>>; fn empty(self) -> crate::Result>>; fn collect_into_bytes(self) -> impl Future>>> + Send @@ -337,6 +340,13 @@ where Ok(Request::from_parts(parts, full(body))) } + /// Set the request body as a stream. + #[cfg(feature = "stream")] + fn stream(self, stream: S) -> crate::Result>> { + let (parts, _) = self.into_parts(); + Ok(Request::from_parts(parts, StreamBody::new(stream))) + } + /// Set the request body as empty. #[inline] fn empty(self) -> crate::Result>> {