use reqwest::Client;
use std::error::Error;
use std::time::Duration;
pub struct RequestFetcher {
client: Client,
}
impl RequestFetcher {
pub fn new(timeout: Option<Duration>) -> Self {
let timeout = timeout.unwrap_or(Duration::from_secs(30));
let client = Client::builder()
.timeout(timeout)
.user_agent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
.build()
.expect("Failed to create HTTP client");
Self { client }
}
pub async fn fetch(&self, url: &str) -> Result<String, Box<dyn Error + Send + Sync>> {
let response = self.client.get(url).send().await?;
let status = response.status();
if !status.is_success() {
return Err(format!(
"Failed to fetch page: HTTP {} ({})",
status.as_u16(),
status.canonical_reason().unwrap_or("Unknown")
)
.into());
}
let html = response.text().await?;
Ok(html)
}
}