-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.rs
More file actions
92 lines (76 loc) · 2.72 KB
/
client.rs
File metadata and controls
92 lines (76 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use crate::api::resources::APIClient;
use crate::{ApiError, ClientConfig};
use std::collections::HashMap;
use std::time::Duration;
/*
Things to know:
`impl Into<String>` is a generic parameter constraint in Rust that means "accept any type that can be converted into a String."
It's essentially Rust's way of saying "I'll take a string in any form you give it to me."
Types that implement Into<String>:
// All of these work:
builder.api_key("hello") // &str
builder.api_key("hello".to_string()) // String
builder.api_key(format!("key_{}", id)) // String from format!
builder.api_key(my_string_variable) // String variable
*/
/// Builder for creating API clients with custom configuration
pub struct ApiClientBuilder {
config: ClientConfig,
}
impl ApiClientBuilder {
/// Create a new builder with the specified base URL
pub fn new(base_url: impl Into<String>) -> Self {
let mut config = ClientConfig::default();
config.base_url = base_url.into();
Self { config }
}
/// Set the API key for authentication
pub fn api_key(mut self, key: impl Into<String>) -> Self {
self.config.api_key = Some(key.into());
self
}
/// Set the bearer token for authentication
pub fn token(mut self, token: impl Into<String>) -> Self {
self.config.token = Some(token.into());
self
}
/// Set the username for basic authentication
pub fn username(mut self, username: impl Into<String>) -> Self {
self.config.username = Some(username.into());
self
}
/// Set the password for basic authentication
pub fn password(mut self, password: impl Into<String>) -> Self {
self.config.password = Some(password.into());
self
}
/// Set the request timeout
pub fn timeout(mut self, timeout: Duration) -> Self {
self.config.timeout = timeout;
self
}
/// Set the maximum number of retries
pub fn max_retries(mut self, retries: u32) -> Self {
self.config.max_retries = retries;
self
}
/// Add a custom header
pub fn custom_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.config.custom_headers.insert(key.into(), value.into());
self
}
/// Add multiple custom headers
pub fn custom_headers(mut self, headers: HashMap<String, String>) -> Self {
self.config.custom_headers.extend(headers);
self
}
/// Set the user agent
pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
self.config.user_agent = user_agent.into();
self
}
/// Build the client with validation
pub fn build(self) -> Result<APIClient, ApiError> {
APIClient::new(self.config)
}
}