2021-08-10 16:07:08 +00:00
|
|
|
# reqwest-middleware
|
|
|
|
|
|
|
|
A crate implementing a wrapper around [reqwest](https://crates.io/crates/reqwest)
|
|
|
|
to allow for client middleware chains.
|
|
|
|
|
|
|
|
[![Crates.io](https://img.shields.io/crates/v/reqwest-middleware.svg)](https://crates.io/crates/reqwest-middleware)
|
|
|
|
[![Docs.rs](https://docs.rs/reqwest-middleware/badge.svg)](https://docs.rs/reqwest-middleware)
|
|
|
|
[![CI](https://github.com/TrueLayer/reqwest-middleware/workflows/CI/badge.svg)](https://github.com/TrueLayer/reqwest-middleware/actions)
|
|
|
|
[![Coverage Status](https://coveralls.io/repos/github/TrueLayer/reqwest-middleware/badge.svg?branch=main&t=YKhONc)](https://coveralls.io/github/TrueLayer/reqwest-middleware?branch=main)
|
|
|
|
|
2021-09-28 18:26:03 +00:00
|
|
|
This crate provides functionality for building and running middleware but no middleware
|
|
|
|
implementations. This repository also contains a couple of useful concrete middleware crates:
|
|
|
|
|
2022-11-15 16:34:33 +00:00
|
|
|
- [`reqwest-retry`](https://crates.io/crates/reqwest-retry): retry failed requests.
|
|
|
|
- [`reqwest-tracing`](https://crates.io/crates/reqwest-tracing):
|
2021-09-28 18:26:03 +00:00
|
|
|
[`tracing`](https://crates.io/crates/tracing) integration, optional opentelemetry support.
|
|
|
|
|
2021-08-10 16:07:08 +00:00
|
|
|
## Overview
|
|
|
|
|
|
|
|
The `reqwest-middleware` client exposes the same interface as a plain `reqwest` client, but
|
|
|
|
`ClientBuilder` exposes functionality to attach middleware:
|
|
|
|
|
2021-09-28 18:26:03 +00:00
|
|
|
```toml
|
|
|
|
# Cargo.toml
|
|
|
|
# ...
|
|
|
|
[dependencies]
|
|
|
|
reqwest = "0.11"
|
2022-06-30 13:54:54 +00:00
|
|
|
reqwest-middleware = "0.1.6"
|
|
|
|
reqwest-retry = "0.1.5"
|
|
|
|
reqwest-tracing = "0.2.3"
|
2021-09-28 18:26:03 +00:00
|
|
|
tokio = { version = "1.12.0", features = ["macros", "rt-multi-thread"] }
|
2022-11-15 16:34:33 +00:00
|
|
|
tower = "0.4"
|
2021-09-28 18:26:03 +00:00
|
|
|
```
|
|
|
|
|
2021-08-10 16:07:08 +00:00
|
|
|
```rust
|
2022-11-15 16:34:33 +00:00
|
|
|
use reqwest::Response;
|
|
|
|
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware, Error, MiddlewareRequest, RequestInitialiser, ReqwestService};
|
2021-08-10 16:07:08 +00:00
|
|
|
use reqwest_retry::{RetryTransientMiddleware, policies::ExponentialBackoff};
|
|
|
|
use reqwest_tracing::TracingMiddleware;
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2021-09-28 18:26:03 +00:00
|
|
|
// 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.
|
2022-06-30 13:54:54 +00:00
|
|
|
.with(TracingMiddleware::default())
|
2021-09-28 18:26:03 +00:00
|
|
|
// Retry failed requests.
|
|
|
|
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
|
|
|
|
.build();
|
2021-09-29 10:40:21 +00:00
|
|
|
run(client).await;
|
2021-08-10 16:07:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 16:34:33 +00:00
|
|
|
async fn run<M, I>(client: ClientWithMiddleware<M, I>)
|
|
|
|
where
|
|
|
|
M: tower::Layer<ReqwestService>,
|
|
|
|
M::Service: tower::Service<MiddlewareRequest, Response = Response, Error = Error>,
|
|
|
|
I: RequestInitialiser,
|
|
|
|
{
|
2021-09-28 18:26:03 +00:00
|
|
|
client
|
|
|
|
.get("https://truelayer.com")
|
|
|
|
.header("foo", "bar")
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-08-10 16:07:08 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### License
|
|
|
|
|
|
|
|
<sup>
|
|
|
|
Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
|
|
|
|
2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
|
|
|
|
</sup>
|
|
|
|
|
|
|
|
<br>
|
|
|
|
|
|
|
|
<sub>
|
|
|
|
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.
|
|
|
|
</sub>
|