aliri_reqwest 0.3.0

Background token management and renewal for reqwest based on best practices
Documentation

Middleware to automatically attach authorization to outgoing requests

When using ClientWithMiddleware, include the [AccessTokenMiddleware] in the middleware stack to use the current access token provided by a [TokenWatcher] for each outbound request.

If a request already has specified an Authorization header value by the time that the middleware executes, the existing value will be left in place, allowing overrides to be specified as required.

use aliri_reqwest::AccessTokenMiddleware;
use aliri_tokens::TokenWatcher;
use reqwest::Client;
use reqwest_middleware::ClientBuilder;
# use aliri_tokens::backoff::ErrorBackoffConfig;
# use aliri_tokens::jitter::NullJitter;
# use aliri_tokens::sources::ConstTokenSource;
#
# #[tokio::main(flavor = "current_thread")] async fn main() {
# let (token_source, jitter, backoff)  = (ConstTokenSource::new("token"), NullJitter, ErrorBackoffConfig::default());
# let token_watcher = TokenWatcher::spawn_from_token_source(token_source, jitter, backoff).await.unwrap();

let client = ClientBuilder::new(Client::default())
.with(AccessTokenMiddleware::new(token_watcher))
.build();

let req = client
.get("https://example.com");
# async move { req
.send()
.await
.unwrap();
# };
# }

The middleware can also be configured to add an authorization token only conditionally. This can be useful in the event that you want to use a single common middleware stack with multiple potential backends and want to ensure that specific tokens are used for specific backends.

These predicates can be composed together to evaluate more complex requirements prior to attaching a token to a request.

use aliri_reqwest::{
AccessTokenMiddleware, ExactHostMatch, HttpsOnly
};
use predicates::prelude::PredicateBooleanExt;
# use aliri_tokens::{
#    backoff::ErrorBackoffConfig,
#    jitter::NullJitter,
#    sources::ConstTokenSource,
#    TokenWatcher,
# };
# #[tokio::main(flavor = "current_thread")] async fn main() {
# let (token_source, jitter, backoff)  = (ConstTokenSource::new("token"), NullJitter, ErrorBackoffConfig::default());
# let token_watcher = TokenWatcher::spawn_from_token_source(token_source, jitter, backoff).await.unwrap();

AccessTokenMiddleware::new(token_watcher)
.with_predicate(HttpsOnly.and(ExactHostMatch::new("example.com")));
# }