polyte_core/
client.rs

1use std::time::Duration;
2
3use reqwest::Client;
4use url::Url;
5
6use crate::error::ApiError;
7
8const DEFAULT_TIMEOUT_MS: u64 = 30_000;
9const DEFAULT_POOL_SIZE: usize = 10;
10
11/// Shared client configuration
12#[derive(Debug, Clone)]
13pub struct ClientConfig {
14    pub client: Client,
15    pub base_url: Url,
16}
17
18/// Builder for HTTP client configuration
19pub struct ClientBuilder {
20    base_url: String,
21    timeout_ms: u64,
22    pool_size: usize,
23}
24
25impl ClientBuilder {
26    /// Create a new client builder
27    pub fn new(base_url: impl Into<String>) -> Self {
28        Self {
29            base_url: base_url.into(),
30            timeout_ms: DEFAULT_TIMEOUT_MS,
31            pool_size: DEFAULT_POOL_SIZE,
32        }
33    }
34
35    /// Set request timeout in milliseconds
36    pub fn timeout_ms(mut self, timeout: u64) -> Self {
37        self.timeout_ms = timeout;
38        self
39    }
40
41    /// Set connection pool size
42    pub fn pool_size(mut self, size: usize) -> Self {
43        self.pool_size = size;
44        self
45    }
46
47    /// Build the client configuration
48    pub fn build(self) -> Result<ClientConfig, ApiError> {
49        let client = Client::builder()
50            .timeout(Duration::from_millis(self.timeout_ms))
51            .pool_max_idle_per_host(self.pool_size)
52            .build()?;
53
54        let base_url = Url::parse(&self.base_url)?;
55
56        Ok(ClientConfig { client, base_url })
57    }
58}