use crate::error::{Error, Result};
use std::time::Duration;
const MAX_BODY_BYTES: usize = 2 * 1024 * 1024;
const USER_AGENT: &str = concat!("agentsec/", env!("CARGO_PKG_VERSION"));
pub async fn get(url: &str, timeout_secs: u64) -> Result<String> {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(timeout_secs))
.user_agent(USER_AGENT)
.build()?;
let resp = client.get(url).send().await?;
let status = resp.status();
if !status.is_success() {
return Err(Error::Sanitize(format!(
"fetch {url} returned HTTP {status}"
)));
}
let bytes = resp.bytes().await?;
if bytes.len() > MAX_BODY_BYTES {
return Err(Error::Sanitize(format!(
"fetch {url} body exceeds {MAX_BODY_BYTES} byte cap"
)));
}
let text = String::from_utf8_lossy(&bytes).into_owned();
Ok(text)
}