1use crate::api::resources::BrevoClient;
2use crate::Environment;
3use crate::{ApiError, ClientConfig};
4use std::collections::HashMap;
5use std::time::Duration;
6pub 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 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 pub fn environment(mut self, environment: Environment) -> Self {
27 self.config.base_url = environment.url().to_string();
28 self
29 }
30
31 pub fn api_key(mut self, key: impl Into<String>) -> Self {
33 self.config.api_key = Some(key.into());
34 self
35 }
36
37 pub fn token(mut self, token: impl Into<String>) -> Self {
39 self.config.token = Some(token.into());
40 self
41 }
42
43 pub fn username(mut self, username: impl Into<String>) -> Self {
45 self.config.username = Some(username.into());
46 self
47 }
48
49 pub fn password(mut self, password: impl Into<String>) -> Self {
51 self.config.password = Some(password.into());
52 self
53 }
54
55 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 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 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 pub fn timeout(mut self, timeout: Duration) -> Self {
80 self.config.timeout = timeout;
81 self
82 }
83
84 pub fn max_retries(mut self, retries: u32) -> Self {
86 self.config.max_retries = retries;
87 self
88 }
89
90 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 pub fn custom_headers(mut self, headers: HashMap<String, String>) -> Self {
98 self.config.custom_headers.extend(headers);
99 self
100 }
101
102 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 pub fn reqwest_client(mut self, client: reqwest::Client) -> Self {
114 self.config.reqwest_client = Some(client);
115 self
116 }
117
118 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}