use reqwest_drive::{CachePolicy, DriveCache, DriveThrottleBackoff, ThrottlePolicy};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use std::{sync::Arc, time::Duration};
use tempfile::TempDir;
use tokio::time::Instant;
#[tokio::main]
async fn main() {
let temp_dir = TempDir::new().unwrap();
let cache_path = temp_dir.path().join("cache.bin");
let cache_policy = CachePolicy {
default_ttl: Duration::from_secs(60), respect_headers: true, cache_status_override: None,
};
let throttle_policy = ThrottlePolicy {
base_delay_ms: 100, adaptive_jitter_ms: 50, max_concurrent: 1, max_retries: 3, };
let cache = Arc::new(DriveCache::new(&cache_path, cache_policy));
let throttle = Arc::new(DriveThrottleBackoff::new(throttle_policy, cache.clone()));
let client: ClientWithMiddleware = ClientBuilder::new(reqwest::Client::new())
.with_arc(cache.clone()) .with_arc(throttle.clone()) .build();
let url = "https://httpbin.org/status/500";
let start_time = Instant::now();
tracing::info!("Sending request to: {}", url);
let response = match client.get(url).send().await {
Ok(resp) => resp,
Err(err) => {
tracing::error!("❌ Request failed after retries: {:?}", err);
return;
}
};
let elapsed = start_time.elapsed();
tracing::info!("✅ Final Response Status: {}", response.status());
if let Ok(body) = response.text().await {
tracing::info!("📜 Response Body: {}", body);
}
tracing::info!(
"⏳ Total Time Taken (including retries & backoff): {:?}",
elapsed
);
}