clientix_core/client/
mod.rs

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