Skip to main content

par_term_update/
http.rs

1//! HTTP client helper with native-tls support.
2
3use std::io::Read;
4use ureq::Agent;
5use ureq::tls::{RootCerts, TlsConfig, TlsProvider};
6
7/// Create a new HTTP agent configured with native-tls.
8pub fn agent() -> Agent {
9    let tls_config = TlsConfig::builder()
10        .provider(TlsProvider::NativeTls)
11        .root_certs(RootCerts::PlatformVerifier)
12        .build();
13
14    Agent::config_builder()
15        .tls_config(tls_config)
16        .build()
17        .into()
18}
19
20/// Download a file from a URL and return its bytes.
21pub fn download_file(url: &str) -> Result<Vec<u8>, String> {
22    let mut body = agent()
23        .get(url)
24        .header("User-Agent", "par-term")
25        .call()
26        .map_err(|e| format!("Failed to download file: {}", e))?
27        .into_body();
28
29    let mut bytes = Vec::new();
30    body.as_reader()
31        .read_to_end(&mut bytes)
32        .map_err(|e| format!("Failed to read download: {}", e))?;
33
34    Ok(bytes)
35}