Skip to main content

clawdentity_core/
http.rs

1//! Shared HTTP client helpers with a default request timeout.
2
3use std::sync::OnceLock;
4use std::time::Duration;
5
6use crate::error::{CoreError, Result};
7
8pub const HTTP_TIMEOUT_SECONDS: u64 = 30;
9static BLOCKING_CLIENT: OnceLock<reqwest::blocking::Client> = OnceLock::new();
10
11/// TODO(clawdentity): document `blocking_client`.
12pub fn blocking_client() -> Result<reqwest::blocking::Client> {
13    if let Some(client) = BLOCKING_CLIENT.get() {
14        return Ok(client.clone());
15    }
16
17    let client = reqwest::blocking::Client::builder()
18        .timeout(Duration::from_secs(HTTP_TIMEOUT_SECONDS))
19        .build()
20        .map_err(|error| CoreError::Http(error.to_string()))?;
21    let _ = BLOCKING_CLIENT.set(client.clone());
22    Ok(client)
23}
24
25/// TODO(clawdentity): document `client`.
26pub fn client() -> Result<reqwest::Client> {
27    reqwest::Client::builder()
28        .timeout(Duration::from_secs(HTTP_TIMEOUT_SECONDS))
29        .build()
30        .map_err(|error| CoreError::Http(error.to_string()))
31}