Skip to main content

brevo_rust/
client.rs

1use crate::api::resources::BrevoClient;
2use crate::Environment;
3use crate::{ApiError, ClientConfig};
4use std::collections::HashMap;
5use std::time::Duration;
6/// Builder for creating API clients with custom configuration
7pub struct ApiClientBuilder {
8    config: ClientConfig,
9}
10impl Default for ApiClientBuilder {
11    fn default() -> Self {
12        Self {
13            config: ClientConfig::default(),
14        }
15    }
16}
17impl ApiClientBuilder {
18    /// Create a new builder with the specified base URL
19    pub fn new(base_url: impl Into<String>) -> Self {
20        let mut config = ClientConfig::default();
21        config.base_url = base_url.into();
22        Self { config }
23    }
24
25    /// Set the environment, updating the base URL
26    pub fn environment(mut self, environment: Environment) -> Self {
27        self.config.base_url = environment.url().to_string();
28        self
29    }
30
31    /// Set the API key for authentication
32    pub fn api_key(mut self, key: impl Into<String>) -> Self {
33        self.config.api_key = Some(key.into());
34        self
35    }
36
37    /// Set the bearer token for authentication
38    pub fn token(mut self, token: impl Into<String>) -> Self {
39        self.config.token = Some(token.into());
40        self
41    }
42
43    /// Set the username for basic authentication
44    pub fn username(mut self, username: impl Into<String>) -> Self {
45        self.config.username = Some(username.into());
46        self
47    }
48
49    /// Set the password for basic authentication
50    pub fn password(mut self, password: impl Into<String>) -> Self {
51        self.config.password = Some(password.into());
52        self
53    }
54
55    /// Set the OAuth client ID for client credentials authentication
56    pub fn client_id(mut self, client_id: impl Into<String>) -> Self {
57        self.config.client_id = Some(client_id.into());
58        self
59    }
60
61    /// Set the OAuth client secret for client credentials authentication
62    pub fn client_secret(mut self, client_secret: impl Into<String>) -> Self {
63        self.config.client_secret = Some(client_secret.into());
64        self
65    }
66
67    /// Set OAuth credentials (client_id and client_secret) for client credentials authentication
68    pub fn oauth_credentials(
69        mut self,
70        client_id: impl Into<String>,
71        client_secret: impl Into<String>,
72    ) -> Self {
73        self.config.client_id = Some(client_id.into());
74        self.config.client_secret = Some(client_secret.into());
75        self
76    }
77
78    /// Set the request timeout
79    pub fn timeout(mut self, timeout: Duration) -> Self {
80        self.config.timeout = timeout;
81        self
82    }
83
84    /// Set the maximum number of retries
85    pub fn max_retries(mut self, retries: u32) -> Self {
86        self.config.max_retries = retries;
87        self
88    }
89
90    /// Add a custom header
91    pub fn custom_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
92        self.config.custom_headers.insert(key.into(), value.into());
93        self
94    }
95
96    /// Add multiple custom headers
97    pub fn custom_headers(mut self, headers: HashMap<String, String>) -> Self {
98        self.config.custom_headers.extend(headers);
99        self
100    }
101
102    /// Set the user agent
103    pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
104        self.config.user_agent = user_agent.into();
105        self
106    }
107
108    /// Use a custom `reqwest` client for every request
109    ///
110    /// The supplied client is used as-is, so it owns all transport-level
111    /// configuration — custom certificates, proxies, timeouts and user agent.
112    /// Authentication, custom headers and retries are still applied by the SDK.
113    pub fn reqwest_client(mut self, client: reqwest::Client) -> Self {
114        self.config.reqwest_client = Some(client);
115        self
116    }
117
118    /// Build the client with validation
119    pub fn build(self) -> Result<BrevoClient, ApiError> {
120        BrevoClient::new(self.config)
121    }
122}
123#[cfg(test)]
124mod tests {
125    use super::*;
126
127    #[test]
128    fn test_environment() {
129        let builder = ApiClientBuilder::default().environment(Environment::default());
130        assert_eq!(
131            builder.config.base_url,
132            Environment::default().url().to_string()
133        );
134    }
135
136    #[test]
137    fn test_new_sets_base_url() {
138        let builder = ApiClientBuilder::new("https://api.example.com");
139        assert_eq!(builder.config.base_url, "https://api.example.com");
140    }
141
142    #[test]
143    fn test_api_key() {
144        let builder = ApiClientBuilder::new("https://api.example.com").api_key("my-key");
145        assert_eq!(builder.config.api_key, Some("my-key".to_string()));
146    }
147
148    #[test]
149    fn test_token() {
150        let builder = ApiClientBuilder::new("https://api.example.com").token("my-token");
151        assert_eq!(builder.config.token, Some("my-token".to_string()));
152    }
153
154    #[test]
155    fn test_username() {
156        let builder = ApiClientBuilder::new("https://api.example.com").username("user");
157        assert_eq!(builder.config.username, Some("user".to_string()));
158    }
159
160    #[test]
161    fn test_password() {
162        let builder = ApiClientBuilder::new("https://api.example.com").password("pass");
163        assert_eq!(builder.config.password, Some("pass".to_string()));
164    }
165
166    #[test]
167    fn test_client_id() {
168        let builder = ApiClientBuilder::new("https://api.example.com").client_id("cid");
169        assert_eq!(builder.config.client_id, Some("cid".to_string()));
170    }
171
172    #[test]
173    fn test_client_secret() {
174        let builder = ApiClientBuilder::new("https://api.example.com").client_secret("secret");
175        assert_eq!(builder.config.client_secret, Some("secret".to_string()));
176    }
177
178    #[test]
179    fn test_oauth_credentials() {
180        let builder =
181            ApiClientBuilder::new("https://api.example.com").oauth_credentials("cid", "secret");
182        assert_eq!(builder.config.client_id, Some("cid".to_string()));
183        assert_eq!(builder.config.client_secret, Some("secret".to_string()));
184    }
185
186    #[test]
187    fn test_timeout() {
188        let builder =
189            ApiClientBuilder::new("https://api.example.com").timeout(Duration::from_secs(120));
190        assert_eq!(builder.config.timeout, Duration::from_secs(120));
191    }
192
193    #[test]
194    fn test_max_retries() {
195        let builder = ApiClientBuilder::new("https://api.example.com").max_retries(5);
196        assert_eq!(builder.config.max_retries, 5);
197    }
198
199    #[test]
200    fn test_custom_header() {
201        let builder =
202            ApiClientBuilder::new("https://api.example.com").custom_header("X-Custom", "value");
203        assert_eq!(
204            builder.config.custom_headers.get("X-Custom"),
205            Some(&"value".to_string())
206        );
207    }
208
209    #[test]
210    fn test_custom_headers_multiple() {
211        let mut headers = HashMap::new();
212        headers.insert("X-One".to_string(), "1".to_string());
213        headers.insert("X-Two".to_string(), "2".to_string());
214        let builder = ApiClientBuilder::new("https://api.example.com").custom_headers(headers);
215        assert_eq!(
216            builder.config.custom_headers.get("X-One"),
217            Some(&"1".to_string())
218        );
219        assert_eq!(
220            builder.config.custom_headers.get("X-Two"),
221            Some(&"2".to_string())
222        );
223    }
224
225    #[test]
226    fn test_user_agent() {
227        let builder = ApiClientBuilder::new("https://api.example.com").user_agent("my-sdk/1.0");
228        assert_eq!(builder.config.user_agent, "my-sdk/1.0");
229    }
230
231    #[test]
232    fn test_full_builder_chain() {
233        let builder = ApiClientBuilder::new("https://api.example.com")
234            .api_key("key")
235            .token("tok")
236            .username("user")
237            .password("pass")
238            .timeout(Duration::from_secs(60))
239            .max_retries(3)
240            .custom_header("X-Foo", "bar")
241            .user_agent("test/1.0");
242        assert_eq!(builder.config.base_url, "https://api.example.com");
243        assert_eq!(builder.config.api_key, Some("key".to_string()));
244        assert_eq!(builder.config.token, Some("tok".to_string()));
245        assert_eq!(builder.config.username, Some("user".to_string()));
246        assert_eq!(builder.config.password, Some("pass".to_string()));
247        assert_eq!(builder.config.timeout, Duration::from_secs(60));
248        assert_eq!(builder.config.max_retries, 3);
249        assert_eq!(builder.config.user_agent, "test/1.0");
250    }
251
252    #[test]
253    fn test_reqwest_client() {
254        let builder =
255            ApiClientBuilder::new("https://api.example.com").reqwest_client(reqwest::Client::new());
256        assert!(builder.config.reqwest_client.is_some());
257    }
258
259    #[test]
260    fn test_reqwest_client_defaults_to_none() {
261        let builder = ApiClientBuilder::new("https://api.example.com");
262        assert!(builder.config.reqwest_client.is_none());
263    }
264
265    #[test]
266    fn test_build_succeeds() {
267        let result = ApiClientBuilder::new("https://api.example.com").build();
268        assert!(result.is_ok());
269    }
270}