reqwest_middleware/lib.rs
1//! This crate provides [`ClientWithMiddleware`], a wrapper around [`reqwest::Client`] with the
2//! ability to attach middleware which runs on every request.
3//!
4//! You'll want to instantiate [`ClientWithMiddleware`] using [`ClientBuilder`], then you can
5//! attach your middleware using [`with`], finalize it with [`build`] and from then on sending
6//! requests is the same as with reqwest:
7//!
8//! ```
9//! use reqwest::{Client, Request, Response};
10//! use reqwest_middleware::{ClientBuilder, Middleware, Next, Result};
11//! use http::Extensions;
12//!
13//! struct LoggingMiddleware;
14//!
15//! #[async_trait::async_trait]
16//! impl Middleware for LoggingMiddleware {
17//! async fn handle(
18//! &self,
19//! req: Request,
20//! extensions: &mut Extensions,
21//! next: Next<'_>,
22//! ) -> Result<Response> {
23//! println!("Request started {:?}", req);
24//! let res = next.run(req, extensions).await;
25//! println!("Result: {:?}", res);
26//! res
27//! }
28//! }
29//!
30//! async fn run() {
31//! let reqwest_client = Client::builder().build().unwrap();
32//! let client = ClientBuilder::new(reqwest_client)
33//! .with(LoggingMiddleware)
34//! .build();
35//! let resp = client.get("https://truelayer.com").send().await.unwrap();
36//! println!("TrueLayer page HTML: {}", resp.text().await.unwrap());
37//! }
38//! ```
39//!
40//! [`build`]: ClientBuilder::build
41//! [`ClientBuilder`]: ClientBuilder
42//! [`ClientWithMiddleware`]: ClientWithMiddleware
43//! [`with`]: ClientBuilder::with
44
45// Test README examples without overriding module docs.
46// We want to keep the in-code docs separate as those allow for automatic linking to crate
47// documentation.
48#[doc = include_str!("../../README.md")]
49#[cfg(doctest)]
50pub struct ReadmeDoctests;
51
52mod client;
53mod error;
54mod middleware;
55mod req_init;
56
57pub use client::{ClientBuilder, ClientWithMiddleware, RequestBuilder};
58pub use error::{Error, Result};
59pub use middleware::{Middleware, Next};
60pub use req_init::{Extension, RequestInitialiser};
61pub use reqwest;