#reqwest-middleware #reqwest #http-middleware #traits #http #middleware

reqwest-traits

Traits for reqwest to allow injecting a custom http client like reqwest_middleware::ClientWithMiddleware

2 unstable releases

0.2.0 Jun 22, 2023
0.1.0 Jun 22, 2023

#14 in #reqwest-middleware

Download history 62/week @ 2024-07-19 24/week @ 2024-07-26 6/week @ 2024-08-02 7/week @ 2024-08-09 1/week @ 2024-08-16 7/week @ 2024-09-20 12/week @ 2024-09-27 4/week @ 2024-10-04 16/week @ 2024-10-11 7/week @ 2024-10-18 16/week @ 2024-10-25 16/week @ 2024-11-01

56 downloads per month

MIT license

15KB
249 lines

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

Dependencies

~5–17MB
~227K SLoC