#reqwest-middleware #http-request #retry #middleware #request-response #chain

reqwest-chain

Apply custom criteria to any reqwest response, deciding when and how to retry

2 unstable releases

0.2.0 Apr 15, 2024
0.1.0 Dec 6, 2022

#201 in HTTP client

Download history 1058/week @ 2024-07-23 966/week @ 2024-07-30 1015/week @ 2024-08-06 766/week @ 2024-08-13 554/week @ 2024-08-20 746/week @ 2024-08-27 651/week @ 2024-09-03 700/week @ 2024-09-10 581/week @ 2024-09-17 716/week @ 2024-09-24 636/week @ 2024-10-01 736/week @ 2024-10-08 667/week @ 2024-10-15 598/week @ 2024-10-22 674/week @ 2024-10-29 658/week @ 2024-11-05

2,730 downloads per month

MIT license

12KB
84 lines

reqwest-chain

Crates.io docs.rs GitHub

Apply custom criteria to any reqwest response, deciding when and how to retry.

reqwest-chain builds on reqwest-middleware, to allow you to focus on your core logic without the boilerplate.

Use case

This crate is a more general framework than reqwest-retry. It allows inspection of:

  • The result of the previous request
  • The state of the retry chain
  • The global state of your middleware

Based on this information, it allows updating any aspect of the next request.

If all you need is a simple retry, you should use reqwest-retry.

Getting started

See the tests directory for several examples.

You should implement Chainer for your middleware struct. This uses the chain method to make a decision after each request respones:

  • If another request is required, update the previous request to form the next request in the chain, and return Ok(None).
  • If the response is ready, return it inside Ok(Some(response)).
  • If an error occurs and you cannot continue, return Err(error).

Below is the initial use case; refresh some authorization credential on request failure.

use reqwest::{header::{AUTHORIZATION, HeaderValue}, StatusCode};
use reqwest_chain::{Chainer, ChainMiddleware};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware, Error};

// Mimic some external function that returns a valid token.
fn fetch_token() -> String {
    "valid-token".to_string()
}

struct FetchTokenMiddleware;

#[async_trait::async_trait]
impl Chainer for FetchTokenMiddleware {
    // We don't need it here, but you can choose to keep track of state between
    // chained retries.
    type State = ();

    async fn chain(
        &self,
        result: Result<reqwest::Response, Error>,
        _state: &mut Self::State,
        request: &mut reqwest::Request,
    ) -> Result<Option<reqwest::Response>, Error> {
        let response = result?;
        if response.status() != StatusCode::UNAUTHORIZED {
            return Ok(Some(response))
        };
        request.headers_mut().insert(
            AUTHORIZATION,
            HeaderValue::from_str(&format!("Bearer {}", fetch_token())).expect("invalid header value"),
        );
        Ok(None)
    }
}

async fn run() {
    let client = ClientBuilder::new(reqwest::Client::new())
        .with(ChainMiddleware::new(FetchTokenMiddleware))
        .build();

    client
        .get("https://example.org")
        // If this request fails, this will be automatically retried with an
        // updated header value.
        .header(AUTHORIZATION, "Bearer expired-token")
        .send()
        .await
        .unwrap();
}

Thanks

Many thanks to the prior work in this area, namely:

Dependencies

~4–15MB
~190K SLoC