Crate tower_reqwest

source ·
Expand description

§Overview

This library provides adapters to use reqwest client with the [tower-http] layers.

§Warning

This crate is currently in early stage of development and is not ready for production use.

§Example

use http::{header::USER_AGENT, HeaderValue};
use http_body_util::BodyExt;
use serde_json::Value;
use tower::ServiceBuilder;
use tower_http::ServiceBuilderExt;
use tower_reqwest::{util::HttpClientExt, HttpClientLayer};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let client = ServiceBuilder::new()
        // Add some layers.
        .override_request_header(USER_AGENT, HeaderValue::from_static("tower-reqwest"))
        // Make client compatible with the `tower-http` layers.
        .layer(HttpClientLayer)
        .service(reqwest::Client::new());
    // Execute request by using this service.
    let response = client
        .execute(
            http::request::Builder::new()
                .method(http::Method::GET)
                .uri("http://ip.jsontest.com")
                .body("")?,
        )
        .await?;

    let bytes = response.into_body().collect().await?.to_bytes();
    let value: Value = serde_json::from_slice(&bytes)?;
    println!("{value:#?}");

    Ok(())
}

Re-exports§

  • pub use crate::error::Error;

Modules§

  • When something went wrong.
  • utilutil
    Various utilities and extensions for working with Tower http clients.

Structs§

Type Aliases§

  • Body type used in this crate for requests and responses.
  • Response type from http crate with the body from this crate.
  • Alias for a Result with the error type crate::Error.