reqwest_middleware/
middleware.rs1use http::Extensions;
2use reqwest::{Client, Request, Response};
3
4use crate::error::{Error, Result};
5
6use std::sync::Arc;
7
8#[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 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#[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}