use crate::EvmSelectors;
use anyhow::Result;
use reqwest::Client;
use std::{fs, path::Path, time::Duration};
impl EvmSelectors {
pub async fn download(timeout: Option<Duration>) -> Result<String> {
let url = "https://api.openchain.xyz/signature-database/v1/export";
let client = Client::new();
let mut request = client.get(url);
if let Some(timeout) = timeout {
request = request.timeout(timeout);
}
let response = request.send().await?;
if !response.status().is_success() {
return Err(anyhow::anyhow!(
"Failed to download from {}: Request returned bad status code {}",
url,
response.status()
));
}
Ok(response.text().await?)
}
pub async fn download_to_file(path: &Path, timeout: Option<Duration>) -> Result<()> {
let raw = Self::download(timeout).await?;
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(path, raw)?;
Ok(())
}
}