use crate::circuit_breaker::CircuitBreakerConfig;
use crate::retry::RetryConfig;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct HttpClientConfig {
pub base_url: Option<String>,
pub timeout: Duration,
pub connect_timeout: Duration,
pub retry: Option<RetryConfig>,
pub circuit_breaker: Option<CircuitBreakerConfig>,
pub pool_idle_timeout: Duration,
pub pool_max_idle_per_host: usize,
pub default_headers: Vec<(String, String)>,
pub user_agent: String,
pub gzip: bool,
pub brotli: bool,
pub follow_redirects: bool,
pub max_redirects: usize,
}
impl Default for HttpClientConfig {
fn default() -> Self {
Self {
base_url: None,
timeout: Duration::from_secs(30),
connect_timeout: Duration::from_secs(10),
retry: None,
circuit_breaker: None,
pool_idle_timeout: Duration::from_secs(90),
pool_max_idle_per_host: 32,
default_headers: Vec::new(),
user_agent: format!("armature-http-client/{}", env!("CARGO_PKG_VERSION")),
gzip: true,
brotli: true,
follow_redirects: true,
max_redirects: 10,
}
}
}
impl HttpClientConfig {
pub fn builder() -> HttpClientConfigBuilder {
HttpClientConfigBuilder::default()
}
}
#[derive(Debug, Default)]
pub struct HttpClientConfigBuilder {
config: HttpClientConfig,
}
impl HttpClientConfigBuilder {
pub fn base_url(mut self, url: impl Into<String>) -> Self {
self.config.base_url = Some(url.into());
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.config.timeout = timeout;
self
}
pub fn connect_timeout(mut self, timeout: Duration) -> Self {
self.config.connect_timeout = timeout;
self
}
pub fn retry(mut self, config: RetryConfig) -> Self {
self.config.retry = Some(config);
self
}
pub fn circuit_breaker(mut self, config: CircuitBreakerConfig) -> Self {
self.config.circuit_breaker = Some(config);
self
}
pub fn pool_idle_timeout(mut self, timeout: Duration) -> Self {
self.config.pool_idle_timeout = timeout;
self
}
pub fn pool_max_idle_per_host(mut self, max: usize) -> Self {
self.config.pool_max_idle_per_host = max;
self
}
pub fn default_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.config
.default_headers
.push((name.into(), value.into()));
self
}
pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
self.config.user_agent = user_agent.into();
self
}
pub fn gzip(mut self, enable: bool) -> Self {
self.config.gzip = enable;
self
}
pub fn brotli(mut self, enable: bool) -> Self {
self.config.brotli = enable;
self
}
pub fn follow_redirects(mut self, enable: bool) -> Self {
self.config.follow_redirects = enable;
self
}
pub fn max_redirects(mut self, max: usize) -> Self {
self.config.max_redirects = max;
self
}
pub fn build(self) -> HttpClientConfig {
self.config
}
}