e778b7df11
fixes #139 There are other potential long term solutions listed [here](https://github.com/TrueLayer/reqwest-middleware/issues/139#issuecomment-2045946644) > I didn't put much thought into it. Internally there used to be a `Box<[Arc<dyn Middleware>]>` which is cloned about quite often. > > * https://docs.rs/reqwest-middleware/0.2.5/src/reqwest_middleware/client.rs.html#87 > * https://docs.rs/reqwest-middleware/0.2.5/src/reqwest_middleware/client.rs.html#139 > > Because of all the cloning, I decided to flip it to be `Arc<Box>` instead of `Box<Arc>`. > > I see three solutions here. > > 1. The one I'd lean towards is `Arc<Arc>` even if it's a bit silly, but prevents extra unnecessary allocations on each request. > 2. An alternative is to implement Middleware for Arc and then you can use the regular with(...) api. This would mean there's an extra box around your middleware > 3. Revert this particular change In the short term, I think it's best to go with option 3. This will unblock the next release. We can consider the other options for future releasees |
||
---|---|---|
.cargo | ||
.github | ||
reqwest-middleware | ||
reqwest-retry | ||
reqwest-tracing | ||
.gitignore | ||
Cargo.toml | ||
CHANGELOG.md | ||
CODE_OF_CONDUCT.md | ||
CONTRIBUTING.md | ||
LICENSE-APACHE | ||
LICENSE-MIT | ||
README.md |
reqwest-middleware
A crate implementing a wrapper around reqwest to allow for client middleware chains.
This crate provides functionality for building and running middleware but no middleware implementations. This repository also contains a couple of useful concrete middleware crates:
reqwest-retry
: retry failed requests.reqwest-tracing
:tracing
integration, optional opentelemetry support.
Note about browser support: automated tests targeting wasm are disabled. The crate may work with wasm but wasm support is unmaintained. PRs improving wasm are still welcome but you'd need to reintroduce the tests and get them passing before we'd merge it (see https://github.com/TrueLayer/reqwest-middleware/pull/105).
Overview
The reqwest-middleware
client exposes the same interface as a plain reqwest
client, but
ClientBuilder
exposes functionality to attach middleware:
# Cargo.toml
# ...
[dependencies]
reqwest = { version = "0.12", features = ["rustls-tls"] }
reqwest-middleware = "0.3"
reqwest-retry = "0.5"
reqwest-tracing = "0.5"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_retry::{RetryTransientMiddleware, policies::ExponentialBackoff};
use reqwest_tracing::TracingMiddleware;
#[tokio::main]
async fn main() {
// Retry up to 3 times with increasing intervals between attempts.
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
let client = ClientBuilder::new(reqwest::Client::new())
// Trace HTTP requests. See the tracing crate to make use of these traces.
.with(TracingMiddleware::default())
// Retry failed requests.
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build();
run(client).await;
}
async fn run(client: ClientWithMiddleware) {
client
.get("https://truelayer.com")
.header("foo", "bar")
.send()
.await
.unwrap();
}
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Third-party middleware
The following third-party middleware use reqwest-middleware
:
reqwest-conditional-middleware
- Per-request basis middlewarehttp-cache
- HTTP caching rulesreqwest-cache
- HTTP cachingaliri_reqwest
- Background token management and renewalhttp-signature-normalization-reqwest
(not free software) - HTTP Signaturesreqwest-chain
- Apply custom criteria to any reqwest response, deciding when and how to retry.