use std::sync::OnceLock;
use std::time::Duration;
use crate::error::{AcmeError, Error, Result};
pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
pub fn set_user_agent(agent: impl Into<String>) {
let _ = crate::set_user_agent(agent.into());
}
#[must_use]
pub fn user_agent() -> &'static str {
crate::user_agent()
}
pub fn client() -> Result<&'static reqwest::Client> {
static CLIENT: OnceLock<std::result::Result<reqwest::Client, String>> = OnceLock::new();
CLIENT
.get_or_init(|| {
crate::install_default_provider();
reqwest::Client::builder()
.user_agent(user_agent())
.timeout(REQUEST_TIMEOUT)
.build()
.map_err(|error| error.to_string())
})
.as_ref()
.map_err(|reason| Error::Acme(AcmeError::Http(format!("client build: {reason}"))))
}
pub use crate::acme::transport::{HttpRequest, HttpResponse, Method, ReqwestTransport, Transport};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn shared_client_is_pooled_and_stable() {
let first = client().expect("client");
let second = client().expect("client");
assert!(std::ptr::eq(first, second));
assert_eq!(REQUEST_TIMEOUT, Duration::from_secs(30));
}
}