armature_http_client/
config.rs

1//! HTTP client configuration.
2
3use crate::circuit_breaker::CircuitBreakerConfig;
4use crate::retry::RetryConfig;
5use std::time::Duration;
6
7/// HTTP client configuration.
8#[derive(Debug, Clone)]
9pub struct HttpClientConfig {
10    /// Base URL for all requests.
11    pub base_url: Option<String>,
12    /// Default request timeout.
13    pub timeout: Duration,
14    /// Connection timeout.
15    pub connect_timeout: Duration,
16    /// Retry configuration.
17    pub retry: Option<RetryConfig>,
18    /// Circuit breaker configuration.
19    pub circuit_breaker: Option<CircuitBreakerConfig>,
20    /// Maximum number of idle connections per host.
21    pub pool_idle_timeout: Duration,
22    /// Maximum idle connections.
23    pub pool_max_idle_per_host: usize,
24    /// Default headers for all requests.
25    pub default_headers: Vec<(String, String)>,
26    /// User agent string.
27    pub user_agent: String,
28    /// Enable gzip compression.
29    pub gzip: bool,
30    /// Enable brotli compression.
31    pub brotli: bool,
32    /// Follow redirects.
33    pub follow_redirects: bool,
34    /// Maximum redirects to follow.
35    pub max_redirects: usize,
36}
37
38impl Default for HttpClientConfig {
39    fn default() -> Self {
40        Self {
41            base_url: None,
42            timeout: Duration::from_secs(30),
43            connect_timeout: Duration::from_secs(10),
44            retry: None,
45            circuit_breaker: None,
46            pool_idle_timeout: Duration::from_secs(90),
47            pool_max_idle_per_host: 32,
48            default_headers: Vec::new(),
49            user_agent: format!("armature-http-client/{}", env!("CARGO_PKG_VERSION")),
50            gzip: true,
51            brotli: true,
52            follow_redirects: true,
53            max_redirects: 10,
54        }
55    }
56}
57
58impl HttpClientConfig {
59    /// Create a new configuration builder.
60    pub fn builder() -> HttpClientConfigBuilder {
61        HttpClientConfigBuilder::default()
62    }
63}
64
65/// Builder for HTTP client configuration.
66#[derive(Debug, Default)]
67pub struct HttpClientConfigBuilder {
68    config: HttpClientConfig,
69}
70
71impl HttpClientConfigBuilder {
72    /// Set the base URL for all requests.
73    pub fn base_url(mut self, url: impl Into<String>) -> Self {
74        self.config.base_url = Some(url.into());
75        self
76    }
77
78    /// Set the default request timeout.
79    pub fn timeout(mut self, timeout: Duration) -> Self {
80        self.config.timeout = timeout;
81        self
82    }
83
84    /// Set the connection timeout.
85    pub fn connect_timeout(mut self, timeout: Duration) -> Self {
86        self.config.connect_timeout = timeout;
87        self
88    }
89
90    /// Set retry configuration.
91    pub fn retry(mut self, config: RetryConfig) -> Self {
92        self.config.retry = Some(config);
93        self
94    }
95
96    /// Set circuit breaker configuration.
97    pub fn circuit_breaker(mut self, config: CircuitBreakerConfig) -> Self {
98        self.config.circuit_breaker = Some(config);
99        self
100    }
101
102    /// Set the connection pool idle timeout.
103    pub fn pool_idle_timeout(mut self, timeout: Duration) -> Self {
104        self.config.pool_idle_timeout = timeout;
105        self
106    }
107
108    /// Set the maximum idle connections per host.
109    pub fn pool_max_idle_per_host(mut self, max: usize) -> Self {
110        self.config.pool_max_idle_per_host = max;
111        self
112    }
113
114    /// Add a default header for all requests.
115    pub fn default_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
116        self.config
117            .default_headers
118            .push((name.into(), value.into()));
119        self
120    }
121
122    /// Set the user agent string.
123    pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
124        self.config.user_agent = user_agent.into();
125        self
126    }
127
128    /// Enable or disable gzip compression.
129    pub fn gzip(mut self, enable: bool) -> Self {
130        self.config.gzip = enable;
131        self
132    }
133
134    /// Enable or disable brotli compression.
135    pub fn brotli(mut self, enable: bool) -> Self {
136        self.config.brotli = enable;
137        self
138    }
139
140    /// Enable or disable following redirects.
141    pub fn follow_redirects(mut self, enable: bool) -> Self {
142        self.config.follow_redirects = enable;
143        self
144    }
145
146    /// Set the maximum number of redirects to follow.
147    pub fn max_redirects(mut self, max: usize) -> Self {
148        self.config.max_redirects = max;
149        self
150    }
151
152    /// Build the configuration.
153    pub fn build(self) -> HttpClientConfig {
154        self.config
155    }
156}