use anyhow::{Context, Result};
use std::sync::OnceLock;
use crate::provider::DEFAULT_TIMEOUT_SECS;
static HTTP_CLIENT: OnceLock<reqwest::Client> = OnceLock::new();
pub fn get_http_client() -> Result<&'static reqwest::Client> {
if let Some(client) = HTTP_CLIENT.get() {
return Ok(client);
}
let client = create_http_client()?;
Ok(HTTP_CLIENT.get_or_init(|| client))
}
pub fn warmup_http_client() -> Result<()> {
get_http_client()?;
Ok(())
}
pub fn is_http_client_ready() -> bool {
HTTP_CLIENT.get().is_some()
}
fn create_http_client() -> Result<reqwest::Client> {
#[cfg(feature = "mobile-tls")]
{
let mut root_store = rustls::RootCertStore::empty();
root_store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
let tls_config = rustls::ClientConfig::builder()
.with_root_certificates(root_store)
.with_no_client_auth();
reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(DEFAULT_TIMEOUT_SECS))
.use_preconfigured_tls(tls_config)
.build()
.context("Failed to create HTTP client")
}
#[cfg(not(feature = "mobile-tls"))]
{
reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(DEFAULT_TIMEOUT_SECS))
.build()
.context("Failed to create HTTP client")
}
}