reqwest增加中间件支持TrueLayer团队用支持中间件的客户端reqwest-middleware包装reqwest,并暴露相同的简单API,在满足弹性和可观察性要求下,使其建立可重复使用的组件。开发人员可以通过导入几个crate并在客户端设置代码中添加with_middleware调用来加强与远程HTTP的集成,这对任何其他应用的代码都不会造成干扰。示例代码如下:usereqwest_middleware::{ClientBuilder,ClientWithMiddleware};usereqwest_retry::{RetryTransientMiddleware,policies::ExponentialBackoff};usereqwest_tracing:
racingMiddleware;#[tokio::main]asyncfnmain(){letretry_policy=ExponentialBackoff::builder().build_with_max_retries(3);letclient=ClientBuilder::new(reqwest::Client::new()).with(TracingMiddleware).with(RetryTransientMiddleware::new_with_policy(retry_policy)).build();run(client).await;}asyncfnrun(client:ClientWithMiddleware){//freeretries!client.get("