use std::sync::OnceLock;
use std::time::Duration;
use crate::artisan::Artful;
use crate::rocket::HttpOptions;
const DEFAULT_POOL_IDLE_TIMEOUT: u64 = 90;
const DEFAULT_POOL_MAX_IDLE_PER_HOST: usize = 20;
const DEFAULT_USER_AGENT: &str = concat!("yansongda/artful-rs:", env!("CARGO_PKG_VERSION"));
pub fn get_client() -> &'static reqwest::Client {
static CLIENT: OnceLock<reqwest::Client> = OnceLock::new();
CLIENT.get_or_init(|| {
build_client(Artful::get_config().http).unwrap_or_else(|_| fallback_client())
})
}
fn build_client(http: HttpOptions) -> Result<reqwest::Client, reqwest::Error> {
let pool_idle_timeout = http.pool_idle_timeout.unwrap_or(DEFAULT_POOL_IDLE_TIMEOUT);
let pool_max_idle_per_host = http
.pool_max_idle_per_host
.unwrap_or(DEFAULT_POOL_MAX_IDLE_PER_HOST);
let user_agent = http.user_agent.unwrap_or(DEFAULT_USER_AGENT);
reqwest::Client::builder()
.pool_idle_timeout(Some(Duration::from_secs(pool_idle_timeout)))
.pool_max_idle_per_host(pool_max_idle_per_host)
.user_agent(user_agent)
.build()
}
fn fallback_client() -> reqwest::Client {
reqwest::Client::builder()
.user_agent(DEFAULT_USER_AGENT)
.build()
.unwrap_or_else(|_| reqwest::Client::new())
}