lago_client/config.rs
1use std::sync::Arc;
2use std::time::Duration;
3
4use super::{
5 credentials::{
6 Credentials, CredentialsProvider, EnvironmentCredentialsProvider, StaticCredentialsProvider,
7 },
8 region::{EnvironmentRegionProvider, Region, RegionProvider, StaticRegionProvider},
9 retry::RetryConfig,
10};
11
12/// Configuration settings for the Lago client
13///
14/// This struct contains all the configuration options needed to create and customize
15/// a Lago client instance, including region settings, credentials, timeouts, and retry policies.
16#[derive(Clone)]
17pub struct Config {
18 pub(crate) region_provider: Arc<dyn RegionProvider>,
19 pub(crate) credentials_provider: Arc<dyn CredentialsProvider>,
20 pub(crate) timeout: Duration,
21 pub(crate) retry_config: RetryConfig,
22 pub(crate) user_agent: String,
23}
24
25impl Config {
26 /// Creates a new configuration builder
27 ///
28 /// # Returns
29 /// A new `ConfigBuilder` instance for constructing a configuration
30 pub fn builder() -> ConfigBuilder {
31 ConfigBuilder::new()
32 }
33
34 /// Gets the configured region for API requests
35 ///
36 /// # Returns
37 /// A `Result` containing the `Region` or an error if the region cannot be determined
38 pub fn region(&self) -> Result<Region, lago_types::error::LagoError> {
39 self.region_provider.provider_region()
40 }
41
42 /// Gets the configured credentials for API authentication
43 ///
44 /// # Returns
45 /// A `Result` containing the `Credentials` or an error if credentials cannot be loaded
46 pub fn credentials(&self) -> Result<Credentials, lago_types::error::LagoError> {
47 self.credentials_provider.provider_credentials()
48 }
49
50 /// Gets the configured timeout duration for HTTP requests
51 ///
52 /// # Returns
53 /// The timeout duration
54 pub fn timeout(&self) -> Duration {
55 self.timeout
56 }
57
58 /// Gets the configured retry settings
59 ///
60 /// # Returns
61 /// A reference to the retry configuration
62 pub fn retry_config(&self) -> &RetryConfig {
63 &self.retry_config
64 }
65
66 /// Gets the configured user agent string
67 ///
68 /// # Returns
69 /// The user agent string used for HTTP requests
70 pub fn user_agent(&self) -> &str {
71 &self.user_agent
72 }
73}
74
75impl Default for Config {
76 /// Creates a default configuration using environment-based providers
77 ///
78 /// This will attempt to load region and credentials from environment variables,
79 /// with sensible defaults for timeout, retry settings, and user agent.
80 fn default() -> Self {
81 Self {
82 region_provider: Arc::new(EnvironmentRegionProvider::new()),
83 credentials_provider: Arc::new(EnvironmentCredentialsProvider::new()),
84 timeout: Duration::from_secs(30),
85 retry_config: RetryConfig::default(),
86 user_agent: format!("lago-rust-client/{}", env!("CARGO_PKG_VERSION")),
87 }
88 }
89}
90
91/// Builder for creating customized configuration instances
92///
93/// This builder allows you to configure various aspects of the Lago client
94/// such as region, credentials, timeout, retry behavior, and user agent.
95pub struct ConfigBuilder {
96 region_provider: Option<Arc<dyn RegionProvider>>,
97 credentials_provider: Option<Arc<dyn CredentialsProvider>>,
98 timeout: Option<Duration>,
99 retry_config: Option<RetryConfig>,
100 user_agent: Option<String>,
101}
102
103impl ConfigBuilder {
104 /// Creates a new configuration builder with default values
105 ///
106 /// # Returns
107 /// A new `ConfigBuilder` instance
108 pub fn new() -> Self {
109 Self {
110 region_provider: None,
111 credentials_provider: None,
112 timeout: None,
113 retry_config: None,
114 user_agent: None,
115 }
116 }
117
118 /// Sets the region for API requests
119 ///
120 /// # Arguments
121 /// * `region` - The region to use for API requests
122 ///
123 /// # Returns
124 /// The builder instance for method chaining
125 pub fn region(mut self, region: Region) -> Self {
126 self.region_provider = Some(Arc::new(StaticRegionProvider::new(region)));
127 self
128 }
129
130 /// Sets a custom region provider
131 ///
132 /// # Arguments
133 /// * `provider` - The region provider to use
134 ///
135 /// # Returns
136 /// The builder instance for method chaining
137 pub fn region_provider(mut self, provider: Arc<dyn RegionProvider>) -> Self {
138 self.region_provider = Some(provider);
139 self
140 }
141
142 /// Sets the credentials for API authentication
143 ///
144 /// # Arguments
145 /// * `credentials` - The credentials to use for authentication
146 ///
147 /// # Returns
148 /// The builder instance for method chaining
149 pub fn credentials(mut self, credentials: Credentials) -> Self {
150 self.credentials_provider = Some(Arc::new(StaticCredentialsProvider::new(credentials)));
151 self
152 }
153
154 /// Sets a custom credentials provider
155 ///
156 /// # Arguments
157 /// * `provider` - The credentials provider to use
158 ///
159 /// # Returns
160 /// The builder instance for method chaining
161 pub fn credentials_provider(mut self, provider: Arc<dyn CredentialsProvider>) -> Self {
162 self.credentials_provider = Some(provider);
163 self
164 }
165
166 /// Sets the timeout duration for HTTP requests
167 ///
168 /// # Arguments
169 /// * `timeout` - The timeout duration
170 ///
171 /// # Returns
172 /// The builder instance for method chaining
173 pub fn timeout(mut self, timeout: Duration) -> Self {
174 self.timeout = Some(timeout);
175 self
176 }
177
178 /// Sets the retry configuration
179 ///
180 /// # Arguments
181 /// * `retry_config` - The retry configuration to use
182 ///
183 /// # Returns
184 /// The builder instance for method chaining
185 pub fn retry_config(mut self, retry_config: RetryConfig) -> Self {
186 self.retry_config = Some(retry_config);
187 self
188 }
189
190 /// Sets the user agent string for HTTP requests
191 ///
192 /// # Arguments
193 /// * `user_agent` - The user agent string
194 ///
195 /// # Returns
196 /// The builder instance for method chaining
197 pub fn user_agent(mut self, user_agent: String) -> Self {
198 self.user_agent = Some(user_agent);
199 self
200 }
201
202 /// Builds the final configuration instance
203 ///
204 /// Any unset values will use the defaults from `Config::default()`.
205 ///
206 /// # Returns
207 /// A new `Config` instance with the specified settings
208 pub fn build(self) -> Config {
209 let default_config = Config::default();
210
211 Config {
212 region_provider: self
213 .region_provider
214 .unwrap_or(default_config.region_provider),
215 credentials_provider: self
216 .credentials_provider
217 .unwrap_or(default_config.credentials_provider),
218 timeout: self.timeout.unwrap_or(default_config.timeout),
219 retry_config: self.retry_config.unwrap_or(default_config.retry_config),
220 user_agent: self.user_agent.unwrap_or(default_config.user_agent),
221 }
222 }
223}
224
225impl Default for ConfigBuilder {
226 /// Creates a default configuration builder
227 ///
228 /// This is equivalent to calling `ConfigBuilder::new()`.
229 fn default() -> Self {
230 Self::new()
231 }
232}