-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extensions middleware and request extensions (#56)
* extensions middleware and request extensions * fix publish * request initialisers * simplify example * docs * changelog * Apply suggestions from code review Co-authored-by: Alex Butler <alexheretic@gmail.com> Co-authored-by: Alex Butler <alexheretic@gmail.com>
- Loading branch information
1 parent
3c00388
commit 85e520f
Showing
4 changed files
with
172 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use crate::RequestBuilder; | ||
|
||
/// When attached to a [`ClientWithMiddleware`] (generally using [`with_init`]), it is run | ||
/// whenever the client starts building a request, in the order it was attached. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use reqwest_middleware::{RequestInitialiser, RequestBuilder}; | ||
/// | ||
/// struct AuthInit; | ||
/// | ||
/// impl RequestInitialiser for AuthInit { | ||
/// fn init(&self, req: RequestBuilder) -> RequestBuilder { | ||
/// req.bearer_auth("my_auth_token") | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// [`ClientWithMiddleware`]: crate::ClientWithMiddleware | ||
/// [`with_init`]: crate::ClientBuilder::with_init | ||
pub trait RequestInitialiser: 'static + Send + Sync { | ||
fn init(&self, req: RequestBuilder) -> RequestBuilder; | ||
} | ||
|
||
impl<F> RequestInitialiser for F | ||
where | ||
F: Send + Sync + 'static + Fn(RequestBuilder) -> RequestBuilder, | ||
{ | ||
fn init(&self, req: RequestBuilder) -> RequestBuilder { | ||
(self)(req) | ||
} | ||
} | ||
|
||
/// A middleware that inserts the value into the [`Extensions`](task_local_extensions::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 task_local_extensions::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()); | ||
/// } | ||
/// ``` | ||
pub struct Extension<T>(pub T); | ||
|
||
impl<T: Send + Sync + Clone + 'static> RequestInitialiser for Extension<T> { | ||
fn init(&self, req: RequestBuilder) -> RequestBuilder { | ||
req.with_extension(self.0.clone()) | ||
} | ||
} |