Struct reqwest_middleware::Extension
source · pub struct Extension<T>(pub T);
Expand description
A middleware that inserts the value into the Extensions
during the call.
This is a good way to inject extensions to middleware deeper in the stack
use reqwest::{Client, Request, Response};
use reqwest_middleware::{ClientBuilder, Middleware, Next, Result, Extension};
use http::Extensions;
#[derive(Clone)]
struct LogName(&'static str);
struct LoggingMiddleware;
#[async_trait::async_trait]
impl Middleware for LoggingMiddleware {
async fn handle(
&self,
req: Request,
extensions: &mut Extensions,
next: Next<'_>,
) -> Result<Response> {
// get the log name or default to "unknown"
let name = extensions
.get()
.map(|&LogName(name)| name)
.unwrap_or("unknown");
println!("[{name}] Request started {req:?}");
let res = next.run(req, extensions).await;
println!("[{name}] Result: {res:?}");
res
}
}
async fn run() {
let reqwest_client = Client::builder().build().unwrap();
let client = ClientBuilder::new(reqwest_client)
.with_init(Extension(LogName("my-client")))
.with(LoggingMiddleware)
.build();
let resp = client.get("https://truelayer.com").send().await.unwrap();
println!("TrueLayer page HTML: {}", resp.text().await.unwrap());
}
Tuple Fields§
§0: T
Trait Implementations§
source§impl<T: Send + Sync + Clone + 'static> RequestInitialiser for Extension<T>
impl<T: Send + Sync + Clone + 'static> RequestInitialiser for Extension<T>
fn init(&self, req: RequestBuilder) -> RequestBuilder
Auto Trait Implementations§
impl<T> Freeze for Extension<T>where
T: Freeze,
impl<T> RefUnwindSafe for Extension<T>where
T: RefUnwindSafe,
impl<T> Send for Extension<T>where
T: Send,
impl<T> Sync for Extension<T>where
T: Sync,
impl<T> Unpin for Extension<T>where
T: Unpin,
impl<T> UnwindSafe for Extension<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more