#reqwest-middleware #http-request #disable #conditional #wrapper #basis #wrapped

reqwest-conditional-middleware

A middleware wrapper that enables (or disables) a wrapped Reqwest middleware on a per-request basis

4 releases (2 breaking)

0.3.0 May 29, 2024
0.2.1 May 5, 2023
0.2.0 May 5, 2023
0.1.0 Jun 23, 2022

#112 in HTTP server

Download history 5901/week @ 2024-05-20 4515/week @ 2024-05-27 4668/week @ 2024-06-03 4435/week @ 2024-06-10 6482/week @ 2024-06-17 5936/week @ 2024-06-24 3431/week @ 2024-07-01 3833/week @ 2024-07-08 3622/week @ 2024-07-15 3994/week @ 2024-07-22 4399/week @ 2024-07-29 4038/week @ 2024-08-05 6376/week @ 2024-08-12 5782/week @ 2024-08-19 4610/week @ 2024-08-26 3887/week @ 2024-09-02

20,838 downloads per month
Used in 27 crates (21 directly)

MIT license

9KB
117 lines

reqwest-conditional-middleware

CI docs.rs

A middleware wrapper that enables (or disables) a wrapped Reqwest middleware on a per-request basis

Example

Usage of this crate depends on a few crates:

async-trait = "0.1.80"
http = "1"
reqwest = version = "0.12"
reqwest-conditional-middleware = "0.3.0"
reqwest-middleware = "0.3"

This is an example of a conditional middleware that short-circuits a middleware stack and returns OK whenever the request method is GET

use reqwest::{Request, Response};
use reqwest_conditional_middleware::ConditionalMiddleware;
use reqwest_middleware::{Middleware, Next, Result};
use task_local_extensions::Extensions;

struct AlwaysOk;

#[async_trait::async_trait]
impl Middleware for AlwaysOk {
    async fn handle(
        &self,
        _req: Request,
        _extensions: &mut Extensions,
        _next: Next<'_>,
    ) -> Result<Response> {
        let builder = http::Response::builder().status(http::StatusCode::OK);
        Ok(builder.body("").unwrap().into())
    }
}

let conditional = ConditionalMiddleware::new(
    AlwaysOk,
    |req: &Request| req.method() == http::Method::GET
);

Dependencies

~3–14MB
~203K SLoC