armature_http_client/
config.rs1use crate::circuit_breaker::CircuitBreakerConfig;
4use crate::retry::RetryConfig;
5use std::time::Duration;
6
7#[derive(Debug, Clone)]
9pub struct HttpClientConfig {
10 pub base_url: Option<String>,
12 pub timeout: Duration,
14 pub connect_timeout: Duration,
16 pub retry: Option<RetryConfig>,
18 pub circuit_breaker: Option<CircuitBreakerConfig>,
20 pub pool_idle_timeout: Duration,
22 pub pool_max_idle_per_host: usize,
24 pub default_headers: Vec<(String, String)>,
26 pub user_agent: String,
28 pub gzip: bool,
30 pub brotli: bool,
32 pub follow_redirects: bool,
34 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 pub fn builder() -> HttpClientConfigBuilder {
61 HttpClientConfigBuilder::default()
62 }
63}
64
65#[derive(Debug, Default)]
67pub struct HttpClientConfigBuilder {
68 config: HttpClientConfig,
69}
70
71impl HttpClientConfigBuilder {
72 pub fn base_url(mut self, url: impl Into<String>) -> Self {
74 self.config.base_url = Some(url.into());
75 self
76 }
77
78 pub fn timeout(mut self, timeout: Duration) -> Self {
80 self.config.timeout = timeout;
81 self
82 }
83
84 pub fn connect_timeout(mut self, timeout: Duration) -> Self {
86 self.config.connect_timeout = timeout;
87 self
88 }
89
90 pub fn retry(mut self, config: RetryConfig) -> Self {
92 self.config.retry = Some(config);
93 self
94 }
95
96 pub fn circuit_breaker(mut self, config: CircuitBreakerConfig) -> Self {
98 self.config.circuit_breaker = Some(config);
99 self
100 }
101
102 pub fn pool_idle_timeout(mut self, timeout: Duration) -> Self {
104 self.config.pool_idle_timeout = timeout;
105 self
106 }
107
108 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 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 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 pub fn gzip(mut self, enable: bool) -> Self {
130 self.config.gzip = enable;
131 self
132 }
133
134 pub fn brotli(mut self, enable: bool) -> Self {
136 self.config.brotli = enable;
137 self
138 }
139
140 pub fn follow_redirects(mut self, enable: bool) -> Self {
142 self.config.follow_redirects = enable;
143 self
144 }
145
146 pub fn max_redirects(mut self, max: usize) -> Self {
148 self.config.max_redirects = max;
149 self
150 }
151
152 pub fn build(self) -> HttpClientConfig {
154 self.config
155 }
156}