Skip to main content

mkt_core/http/
client.rs

1//! Shared HTTP client builder.
2
3use std::time::Duration;
4
5use reqwest::Client;
6
7use crate::error::Result;
8
9/// Default request timeout in seconds.
10const DEFAULT_TIMEOUT_SECS: u64 = 30;
11
12/// Build a configured `reqwest::Client` with sensible defaults.
13///
14/// Features:
15/// - `rustls-tls` (no OpenSSL)
16/// - Gzip + Brotli decompression
17/// - Configurable timeout (default: 30s)
18/// - Custom User-Agent
19///
20/// # Errors
21///
22/// Returns an error if the HTTP client cannot be built (e.g. TLS init failure).
23pub fn build_http_client(timeout_secs: Option<u64>) -> Result<Client> {
24    let timeout = Duration::from_secs(timeout_secs.unwrap_or(DEFAULT_TIMEOUT_SECS));
25
26    let client = Client::builder()
27        .timeout(timeout)
28        .user_agent(format!("mkt/{}", env!("CARGO_PKG_VERSION")))
29        .gzip(true)
30        .brotli(true)
31        .build()?;
32
33    Ok(client)
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn build_client_with_defaults() {
42        let client = build_http_client(None);
43        assert!(client.is_ok());
44    }
45
46    #[test]
47    fn build_client_with_custom_timeout() {
48        let client = build_http_client(Some(60));
49        assert!(client.is_ok());
50    }
51}