clientix_core/client/
mod.rs1pub mod asynchronous;
2pub mod blocking;
3pub mod result;
4
5use std::collections::HashMap;
6use std::time::Duration;
7use base64::Engine;
8use base64::prelude::BASE64_STANDARD;
9use http::header::AUTHORIZATION;
10use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
11use crate::client::asynchronous::client::AsyncClient;
12use crate::client::blocking::client::BlockingClient;
13
14pub struct Clientix {
15 config: ClientConfig
16}
17
18pub struct ClientixBuilder {
19 config: ClientConfig
20}
21
22#[derive(Clone)]
23pub struct ClientConfig {
24 url: Option<String>,
25 path: Option<String>,
26 user_agent: Option<String>,
27 headers: HeaderMap,
28 timeout: Option<Duration>,
29 read_timeout: Option<Duration>,
30 connect_timeout: Option<Duration>,
31 connection_verbose: bool
32}
33
34#[derive(Clone)]
35struct RequestPath {
36 path_str: String,
37 segments: HashMap<String, String>
38}
39
40impl Clientix {
41
42 pub fn builder() -> ClientixBuilder {
43 ClientixBuilder::new()
44 }
45
46 pub fn config(&self) -> &ClientConfig {
47 &self.config
48 }
49
50 pub fn set_url(&mut self, url: &str) {
51 self.config.url = Some(url.to_string());
52 }
53
54 pub fn set_path(&mut self, path: &str) {
55 self.config.path = Some(path.to_string());
56 }
57
58 pub fn set_user_agent(&mut self, user_agent: &str) {
59 self.config.user_agent = Some(user_agent.to_string());
60 }
61
62 pub fn set_headers(&mut self, headers: HeaderMap) {
63 self.config.headers = headers;
64 }
65
66 pub fn set_timeout(&mut self, timeout: Duration) {
67 self.config.timeout = Some(timeout);
68 }
69
70 pub fn set_read_timeout(&mut self, read_timeout: Duration) {
71 self.config.read_timeout = Some(read_timeout);
72 }
73
74 pub fn set_connect_timeout(&mut self, connect_timeout: Duration) {
75 self.config.connect_timeout = Some(connect_timeout);
76 }
77
78 pub fn set_connection_verbose(&mut self, connection_verbose: bool) {
79 self.config.connection_verbose = connection_verbose;
80 }
81
82 pub fn blocking(&self) -> BlockingClient {
83 BlockingClient::from(self.config.clone())
84 }
85
86 pub fn asynchronous(&self) -> AsyncClient {
87 AsyncClient::from(self.config.clone())
88 }
89
90}
91
92impl ClientixBuilder {
93
94 fn new() -> ClientixBuilder {
95 ClientixBuilder {
96 config: ClientConfig {
97 url: None,
98 path: None,
99 user_agent: None,
100 headers: Default::default(),
101 timeout: None,
102 read_timeout: None,
103 connect_timeout: None,
104 connection_verbose: false,
105 },
106 }
107 }
108
109 pub fn url(mut self, url: &str) -> ClientixBuilder {
110 self.config.url = Some(url.to_string());
111
112 self
113 }
114
115 pub fn path(mut self, path: &str) -> ClientixBuilder {
116 self.config.path = Some(path.to_string());
117
118 self
119 }
120
121 pub fn user_agent(mut self, user_agent: &str) -> ClientixBuilder {
122 self.config.user_agent = Some(user_agent.to_string());
123 self
124 }
125
126 pub fn header(mut self, key: &str, value: &str, sensitive: bool) -> ClientixBuilder {
127 let header_name = if let Ok(name) = HeaderName::from_bytes(key.as_bytes()) {
128 name
129 } else {
130 return self
131 };
132
133 let mut header_value = if let Ok(value) = HeaderValue::from_str(&value) {
134 value
135 } else {
136 return self
137 };
138
139 header_value.set_sensitive(sensitive);
140
141 self.config.headers.insert(header_name, header_value);
142
143 self
144 }
145
146 pub fn headers(mut self, headers: HashMap<String, String>) -> ClientixBuilder {
147 for (key, value) in headers {
148 let header_name = if let Ok(name) = HeaderName::from_bytes(key.as_bytes()) {
149 name
150 } else {
151 continue
152 };
153
154 let header_value = if let Ok(value) = HeaderValue::from_str(&value) {
155 value
156 } else {
157 continue
158 };
159
160 self.config.headers.insert(header_name, header_value);
161 }
162
163 self
164 }
165
166 pub fn basic_auth(self, username: &str, password: &str) -> ClientixBuilder {
167 let basic_token = format!("Basic {}", BASE64_STANDARD.encode(format!("{username}:{password}")));
168 self.header(AUTHORIZATION.as_str(), basic_token.as_str(), true)
169 }
170
171 pub fn bearer_auth(self, token: &str) -> ClientixBuilder {
172 self.header(AUTHORIZATION.as_str(), format!("Bearer {}", token).as_str(), true)
173 }
174
175 pub fn timeout(mut self, timeout: Duration) -> ClientixBuilder {
176 self.config.timeout = Some(timeout);
177 self
178 }
179
180 pub fn read_timeout(mut self, read_timeout: Duration) -> ClientixBuilder {
181 self.config.read_timeout = Some(read_timeout);
182 self
183 }
184
185 pub fn connect_timeout(mut self, connect_timeout: Duration) -> ClientixBuilder {
186 self.config.connect_timeout = Some(connect_timeout);
187 self
188 }
189
190 pub fn connection_verbose(mut self, connection_verbose: bool) -> ClientixBuilder {
191 self.config.connection_verbose = connection_verbose;
192 self
193 }
194
195 pub fn blocking(&self) -> BlockingClient {
196 BlockingClient::from(self.config.clone())
197 }
198
199 pub fn asynchronous(&self) -> AsyncClient {
200 AsyncClient::from(self.config.clone())
201 }
202
203 pub fn build(self) -> Clientix {
204 Clientix { config: self.config }
205 }
206
207}