use crate::catalog::retry::RetryConfig;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct HttpClientConfig {
timeout: Option<Duration>,
connect_timeout: Option<Duration>,
max_retries: usize,
retry_backoff: Duration,
user_agent: Option<String>,
}
impl Default for HttpClientConfig {
fn default() -> Self {
Self {
timeout: Some(Duration::from_secs(30)),
connect_timeout: Some(Duration::from_secs(10)),
max_retries: 2,
retry_backoff: Duration::from_millis(250),
user_agent: Some(default_user_agent()),
}
}
}
impl HttpClientConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
pub fn without_timeout(mut self) -> Self {
self.timeout = None;
self
}
pub fn with_connect_timeout(mut self, timeout: Duration) -> Self {
self.connect_timeout = Some(timeout);
self
}
pub fn without_connect_timeout(mut self) -> Self {
self.connect_timeout = None;
self
}
pub fn with_max_retries(mut self, retries: usize) -> Self {
self.max_retries = retries;
self
}
pub fn with_retry_backoff(mut self, backoff: Duration) -> Self {
self.retry_backoff = backoff;
self
}
pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
self.user_agent = Some(user_agent.into());
self
}
pub fn without_user_agent(mut self) -> Self {
self.user_agent = None;
self
}
pub fn timeout(&self) -> Option<Duration> {
self.timeout
}
pub fn connect_timeout(&self) -> Option<Duration> {
self.connect_timeout
}
pub fn max_retries(&self) -> usize {
self.max_retries
}
pub fn retry_backoff(&self) -> Duration {
self.retry_backoff
}
pub fn user_agent(&self) -> Option<&str> {
self.user_agent.as_deref()
}
}
fn default_user_agent() -> String {
format!("icepick/{}", env!("CARGO_PKG_VERSION"))
}
#[derive(Debug, Clone)]
pub struct CatalogOptions {
http: HttpClientConfig,
reference: String,
retry: Option<RetryConfig>,
}
impl Default for CatalogOptions {
fn default() -> Self {
Self {
http: HttpClientConfig::default(),
reference: "main".to_string(),
retry: None,
}
}
}
impl CatalogOptions {
pub fn new() -> Self {
Self::default()
}
pub fn with_reference(mut self, reference: impl Into<String>) -> Self {
self.reference = reference.into();
self
}
pub fn with_http_config(mut self, http: HttpClientConfig) -> Self {
self.http = http;
self
}
pub fn with_retry_config(mut self, retry: RetryConfig) -> Self {
self.retry = Some(retry);
self
}
pub fn http(&self) -> &HttpClientConfig {
&self.http
}
pub fn reference(&self) -> &str {
&self.reference
}
pub fn retry(&self) -> Option<&RetryConfig> {
self.retry.as_ref()
}
}