use std::time::Duration; use reqwest::blocking::Client; pub struct Downloader { client: Client, } impl Downloader { pub fn new() -> Self { let client = Client::builder() .timeout(Duration::from_secs(300)) .user_agent("nix-js/0.1") .build() .expect("Failed to create HTTP client"); Self { client } } pub fn download(&self, url: &str) -> Result, DownloadError> { let response = self .client .get(url) .send() .map_err(|e| DownloadError::NetworkError(e.to_string()))?; if !response.status().is_success() { return Err(DownloadError::HttpError { url: url.to_string(), status: response.status().as_u16(), }); } response .bytes() .map(|b| b.to_vec()) .map_err(|e| DownloadError::NetworkError(e.to_string())) } } impl Default for Downloader { fn default() -> Self { Self::new() } } #[derive(Debug)] pub enum DownloadError { NetworkError(String), HttpError { url: String, status: u16 }, } impl std::fmt::Display for DownloadError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { DownloadError::NetworkError(msg) => write!(f, "Network error: {}", msg), DownloadError::HttpError { url, status } => { write!(f, "HTTP error {} for URL: {}", status, url) } } } } impl std::error::Error for DownloadError {}