Crate client_util

Source
Expand description

§client-util

Crates.io Version Release status docs.rs

Help you to build requests and handle responses by several extension trait!

§Usage

cargo add client-util

§Make it easier to use hyper http client

use client_util::prelude::{RequestBuilderExt, RequestExt, ResponseExt, hyper_tls_client};
#[tokio::main]
async fn main() -> client_util::Result<()> {
    let mut client = hyper_tls_client();

    let request = http::Request::get("https://httpbin.org/json")
        .version(http::Version::HTTP_11)
        .json("hello client-util")?;

    let (parts, response) = request
        .send(&mut client)
        .await?
        .json::<serde_json::Value>()
        .await?
        .into_parts();
    println!("{:?}", parts);
    println!("{:?}", response);

    Ok(())
}

§Customize your own client

In RequestExt trait, we send the request by a tower service, so you can add any middle layer on the top of the existed client.

§What about…

Theoretically, you can add any feature to any client by using tower.

§What about trace, metrics, following redirect and more features?

You can find those features in tower-http crate as tower layers.

§What about cookies?

Implement a tower layer to manage cookies is a good idea, and we may implement in another crate in the future.

§Feature Flags

flagdescription
jsonjson body
formform body
multipartmultipart form body
queryserialize into and append url’s query
authmethod to append auth header
decompression-deflatedeflate decompression, need tokio runtime
decompression-gzipgzip decompression, need tokio runtime
decompression-brbr decompression, need tokio runtime
decompression-zstdzstd decompression, need tokio runtime
decompression-allall decompression support upon
hyper-clientshortcut to create a hyper http client
hyper-client-rustlshyper-client with rustls
rt-tokiorun with tokio runtime, which allows you use tower-http

Re-exports§

pub use body::empty;
pub use body::full;
pub use body::DynBody;
pub use error::Error;
pub use error::ErrorKind;
pub use error::Result;
pub use http;
pub use http_body;
pub use http_body_util;

Modules§

body
client
This module contains the client implementations for the various HTTP clients.
error
prelude
request
response

Macros§

shared_client