reqwest_middleware/
middleware.rs

1use http::Extensions;
2use reqwest::{Client, Request, Response};
3
4use crate::error::{Error, Result};
5
6use std::sync::Arc;
7
8/// When attached to a [`ClientWithMiddleware`] (generally using [`with`]), middleware is run
9/// whenever the client issues a request, in the order it was attached.
10///
11/// # Example
12///
13/// ```
14/// use reqwest::{Client, Request, Response};
15/// use reqwest_middleware::{ClientBuilder, Middleware, Next, Result};
16/// use http::Extensions;
17///
18/// struct TransparentMiddleware;
19///
20/// #[async_trait::async_trait]
21/// impl Middleware for TransparentMiddleware {
22///     async fn handle(
23///         &self,
24///         req: Request,
25///         extensions: &mut Extensions,
26///         next: Next<'_>,
27///     ) -> Result<Response> {
28///         next.run(req, extensions).await
29///     }
30/// }
31/// ```
32///
33/// [`ClientWithMiddleware`]: crate::ClientWithMiddleware
34/// [`with`]: crate::ClientBuilder::with
35#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
36#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
37pub trait Middleware: 'static + Send + Sync {
38    /// Invoked with a request before sending it. If you want to continue processing the request,
39    /// you should explicitly call `next.run(req, extensions)`.
40    ///
41    /// If you need to forward data down the middleware stack, you can use the `extensions`
42    /// argument.
43    async fn handle(
44        &self,
45        req: Request,
46        extensions: &mut Extensions,
47        next: Next<'_>,
48    ) -> Result<Response>;
49}
50
51#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
52#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
53impl<F> Middleware for F
54where
55    F: Send
56        + Sync
57        + 'static
58        + for<'a> Fn(Request, &'a mut Extensions, Next<'a>) -> BoxFuture<'a, Result<Response>>,
59{
60    async fn handle(
61        &self,
62        req: Request,
63        extensions: &mut Extensions,
64        next: Next<'_>,
65    ) -> Result<Response> {
66        (self)(req, extensions, next).await
67    }
68}
69
70/// Next encapsulates the remaining middleware chain to run in [`Middleware::handle`]. You can
71/// forward the request down the chain with [`run`].
72///
73/// [`Middleware::handle`]: Middleware::handle
74/// [`run`]: Self::run
75#[derive(Clone)]
76pub struct Next<'a> {
77    client: &'a Client,
78    middlewares: &'a [Arc<dyn Middleware>],
79}
80
81#[cfg(not(target_arch = "wasm32"))]
82pub type BoxFuture<'a, T> = std::pin::Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>;
83#[cfg(target_arch = "wasm32")]
84pub type BoxFuture<'a, T> = std::pin::Pin<Box<dyn std::future::Future<Output = T> + 'a>>;
85
86impl<'a> Next<'a> {
87    pub(crate) fn new(client: &'a Client, middlewares: &'a [Arc<dyn Middleware>]) -> Self {
88        Next {
89            client,
90            middlewares,
91        }
92    }
93
94    pub fn run(
95        mut self,
96        req: Request,
97        extensions: &'a mut Extensions,
98    ) -> BoxFuture<'a, Result<Response>> {
99        if let Some((current, rest)) = self.middlewares.split_first() {
100            self.middlewares = rest;
101            current.handle(req, extensions, self)
102        } else {
103            Box::pin(async move { self.client.execute(req).await.map_err(Error::from) })
104        }
105    }
106}