reqwest-traits

Crates.ioreqwest-traits
lib.rsreqwest-traits
version0.2.0
sourcesrc
created_at2023-06-22 12:42:00.817982
updated_at2023-06-22 13:34:03.897176
descriptionTraits for reqwest to allow injecting a custom http client like reqwest_middleware::ClientWithMiddleware
homepage
repositoryhttps://github.com/ernesto-jimenez/reqwest-traits
max_upload_size
id897295
size16,829
Ernesto Jiménez (ernesto-jimenez)

documentation

README

reqwest-traits

reqwest-traits

This crate provides traits for the reqwest crate. It is intended to be used by libraries that need to make HTTP requests, want to allow users to inject their own reqwest client, but don't want to force users to use reqwest::Client.

Example use cases

  • Use reqwest::Client in your program, but inject client with rvcr for testing to avoid making real HTTP requests.
  • Let users of your library inject their own reqwest::Client, if they want a caching or tracing middleware, a reqwest_middleware::ClientWithMiddleware.

Example

use reqwest_traits::Client;

struct MyClient<C: Client> {
    http: C,
}

async fn plain_reqwest() {
    let http = reqwest::Client::new();
    let client = MyClient { http };
    let req = client.http.get("https://example.com");
    let response = req.send().await.unwrap();
    assert_eq!(response.status(), 200);
}

async fn reqwest_middleware() {
    let http = reqwest_middleware::ClientBuilder::new(
        reqwest::Client::new(),
    ).build();
    let client = MyClient { http };
    let req = client.http.get("https://example.com");
    let response = req.send().await.unwrap();
    assert_eq!(response.status(), 200);
}

License: MIT

Commit count: 6

cargo fmt