reqwest-conditional-middleware

Crates.ioreqwest-conditional-middleware
lib.rsreqwest-conditional-middleware
version0.4.0
sourcesrc
created_at2022-06-23 19:25:36.057861
updated_at2024-11-11 20:27:11.305815
descriptionA middleware wrapper that enables (or disables) a wrapped Reqwest middleware on a per-request basis
homepage
repositoryhttps://github.com/oxidecomputer/reqwest-conditional-middleware
max_upload_size
id612040
size10,727
(oxideservicebot)

documentation

https://docs.rs/reqwest-conditional-middleware/

README

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
);

Commit count: 26

cargo fmt