#reqwest-middleware #http-request #conditional #disable

reqwest-conditional-middleware

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

5 releases (3 breaking)

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

#562 in HTTP server

Download history 5722/week @ 2024-08-17 4466/week @ 2024-08-24 4217/week @ 2024-08-31 3491/week @ 2024-09-07 3683/week @ 2024-09-14 4651/week @ 2024-09-21 4801/week @ 2024-09-28 3655/week @ 2024-10-05 4062/week @ 2024-10-12 4414/week @ 2024-10-19 3912/week @ 2024-10-26 4290/week @ 2024-11-02 3790/week @ 2024-11-09 4669/week @ 2024-11-16 3521/week @ 2024-11-23 5335/week @ 2024-11-30

18,090 downloads per month
Used in 31 crates (24 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.4.0"
reqwest-middleware = "0.4"

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

~4–15MB
~190K SLoC