Skip to content

Commit

Permalink
support stream, re-export more
Browse files Browse the repository at this point in the history
  • Loading branch information
4t145 committed Jul 2, 2024
1 parent 6edf5cd commit da7cd37
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "client-util"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["4t145<[email protected]>"]
description = "Help you to build requests and handle responses by several extension trait!"
Expand Down
4 changes: 2 additions & 2 deletions src/client/hyper.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub use hyper_util::client::legacy::connect::HttpConnector;
use hyper_util::client::legacy::{connect::Connect, Client};

type HyperClient = Client<HttpConnector, crate::DynBody>;
pub type HyperClient = Client<HttpConnector, crate::DynBody>;

pub fn build_hyper_client_with_connector<C>(connector: C) -> Client<C, crate::DynBody>
where
Expand Down Expand Up @@ -83,7 +83,7 @@ mod tls {
}
use super::build_hyper_client_with_connector;

type HyperTlsClient = Client<HttpsConnector<HttpConnector>, crate::DynBody>;
pub type HyperTlsClient = Client<HttpsConnector<HttpConnector>, crate::DynBody>;

pub fn build_connector_with_tls_config(
tls_config: rustls::ClientConfig,
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ 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::*;
Expand Down
14 changes: 12 additions & 2 deletions src/request.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -37,6 +38,8 @@ pub trait RequestExt<B>: Sized {
#[cfg(feature = "form")]
#[cfg_attr(docsrs, doc(cfg(feature = "form")))]
fn form<T: Serialize + ?Sized>(self, form: &T) -> crate::Result<Request<Full<Bytes>>>;
#[cfg(feature = "stream")]
fn stream<S: Stream>(self, stream: S) -> crate::Result<Request<StreamBody<S>>>;
fn plain_text(self, body: impl Into<Bytes>) -> crate::Result<Request<Full<Bytes>>>;
fn empty(self) -> crate::Result<Request<Empty<Bytes>>>;
fn collect_into_bytes(self) -> impl Future<Output = crate::Result<Request<Full<Bytes>>>> + Send
Expand Down Expand Up @@ -337,6 +340,13 @@ where
Ok(Request::from_parts(parts, full(body)))
}

/// Set the request body as a stream.
#[cfg(feature = "stream")]
fn stream<S: Stream>(self, stream: S) -> crate::Result<Request<StreamBody<S>>> {
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<Request<Empty<Bytes>>> {
Expand Down

0 comments on commit da7cd37

Please sign in to comment.