1use std::time::Duration;
2use std::path::PathBuf;
3
4#[derive(Debug, Clone)]
5pub struct Config {
6 pub base_url: String,
7 pub api_key: String,
8 pub timeout: Duration,
9 pub retries: u32,
10 pub backoff: BackoffConfig,
11 pub cache_enabled: bool,
12 pub cache_size: usize,
13 pub cache_ttl: Duration,
14 pub max_connections: usize,
15 pub insecure: bool,
16 pub client_cert: Option<PathBuf>,
17 pub client_key: Option<PathBuf>,
18 pub ca_cert: Option<PathBuf>,
19}
20
21#[derive(Debug, Clone)]
22pub struct BackoffConfig {
23 pub base_delay: Duration,
24 pub max_delay: Duration,
25 pub factor: f64,
26}
27
28impl Default for BackoffConfig {
29 fn default() -> Self {
30 Self {
31 base_delay: Duration::from_millis(100),
32 max_delay: Duration::from_secs(2),
33 factor: 2.0,
34 }
35 }
36}
37
38impl Config {
39 pub fn new(api_key: String) -> Self {
40 Self {
41 base_url: "http://localhost:8090".to_string(),
42 api_key,
43 timeout: Duration::from_millis(800),
44 retries: 2,
45 backoff: BackoffConfig::default(),
46 cache_enabled: false,
47 cache_size: 100,
48 cache_ttl: Duration::from_secs(5),
49 max_connections: 100,
50 insecure: false,
51 client_cert: None,
52 client_key: None,
53 ca_cert: None,
54 }
55 }
56}
57
58pub struct ConfigBuilder {
59 config: Config,
60}
61
62impl ConfigBuilder {
63 pub fn new(api_key: String) -> Self {
64 Self {
65 config: Config::new(api_key),
66 }
67 }
68
69 pub fn base_url(mut self, url: String) -> Self {
70 self.config.base_url = url;
71 self
72 }
73
74 pub fn timeout(mut self, timeout: Duration) -> Self {
75 self.config.timeout = timeout;
76 self
77 }
78
79 pub fn retries(mut self, retries: u32) -> Self {
80 self.config.retries = retries;
81 self
82 }
83
84 pub fn backoff(mut self, backoff: BackoffConfig) -> Self {
85 self.config.backoff = backoff;
86 self
87 }
88
89 pub fn cache_enabled(mut self, enabled: bool) -> Self {
90 self.config.cache_enabled = enabled;
91 self
92 }
93
94 pub fn cache_size(mut self, size: usize) -> Self {
95 self.config.cache_size = size;
96 self
97 }
98
99 pub fn cache_ttl(mut self, ttl: Duration) -> Self {
100 self.config.cache_ttl = ttl;
101 self
102 }
103
104 pub fn max_connections(mut self, max_conns: usize) -> Self {
105 self.config.max_connections = max_conns;
106 self
107 }
108
109 pub fn insecure(mut self, insecure: bool) -> Self {
110 self.config.insecure = insecure;
111 self
112 }
113
114 pub fn client_cert(mut self, cert: PathBuf) -> Self {
115 self.config.client_cert = Some(cert);
116 self
117 }
118
119 pub fn client_key(mut self, key: PathBuf) -> Self {
120 self.config.client_key = Some(key);
121 self
122 }
123
124 pub fn ca_cert(mut self, cert: PathBuf) -> Self {
125 self.config.ca_cert = Some(cert);
126 self
127 }
128
129 pub fn build(self) -> Config {
130 self.config
131 }
132}