-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathconfig.rs
More file actions
344 lines (289 loc) · 10 KB
/
Copy pathconfig.rs
File metadata and controls
344 lines (289 loc) · 10 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use derive_builder::Builder;
use reqwest::{Client, ClientBuilder};
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;
use tokio_tungstenite::Connector;
use super::models::{ConfigBuildError, TimeUnit, WebsocketMode};
use super::utils::{SignatureGenerator, build_client};
#[derive(Clone)]
pub struct AgentConnector(pub Connector);
impl fmt::Debug for AgentConnector {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Connector(…)")
}
}
#[derive(Clone)]
pub struct HttpAgent(pub Arc<dyn Fn(ClientBuilder) -> ClientBuilder + Send + Sync>);
impl fmt::Debug for HttpAgent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "HttpAgent(<custom agent fn>)")
}
}
#[derive(Clone)]
pub struct ProxyAuth {
pub username: String,
pub password: String,
}
#[derive(Debug, Clone)]
pub struct ProxyConfig {
pub host: String,
pub port: u16,
pub protocol: Option<String>,
pub auth: Option<ProxyAuth>,
}
impl fmt::Debug for ProxyAuth {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ProxyAuth")
.field("username", &self.username)
.field("password", &"[REDACTED]")
.finish()
}
}
#[derive(Clone)]
pub enum PrivateKey {
File(String),
Raw(Vec<u8>),
}
impl fmt::Debug for PrivateKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PrivateKey::File(_) => write!(f, "PrivateKey::File([REDACTED])"),
PrivateKey::Raw(_) => write!(f, "PrivateKey::Raw([REDACTED])"),
}
}
}
#[derive(Clone, Builder)]
#[builder(
pattern = "owned",
build_fn(name = "try_build", error = "ConfigBuildError")
)]
pub struct ConfigurationRestApi {
#[builder(setter(into, strip_option), default)]
pub api_key: Option<String>,
#[builder(setter(into, strip_option), default)]
pub api_secret: Option<String>,
#[builder(setter(into, strip_option), default)]
pub base_path: Option<String>,
#[builder(default = "1000")]
pub timeout: u64,
#[builder(default = "true")]
pub keep_alive: bool,
#[builder(default = "true")]
pub compression: bool,
#[builder(default = "3")]
pub retries: u32,
#[builder(default = "1000")]
pub backoff: u64,
#[builder(setter(strip_option), default)]
pub proxy: Option<ProxyConfig>,
#[builder(setter(strip_option, into), default)]
pub custom_headers: Option<HashMap<String, String>>,
#[builder(setter(strip_option), default)]
pub agent: Option<HttpAgent>,
#[builder(setter(strip_option), default)]
pub private_key: Option<PrivateKey>,
#[builder(setter(strip_option), default)]
pub private_key_passphrase: Option<String>,
#[builder(setter(strip_option), default)]
pub time_unit: Option<TimeUnit>,
#[builder(setter(skip))]
pub(crate) client: Client,
#[builder(setter(skip))]
pub(crate) user_agent: String,
#[builder(setter(skip))]
pub(crate) signature_gen: SignatureGenerator,
}
impl fmt::Debug for ConfigurationRestApi {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ConfigurationRestApi")
.field("api_key", &self.api_key.as_ref().map(|_| "[REDACTED]"))
.field(
"api_secret",
&self.api_secret.as_ref().map(|_| "[REDACTED]"),
)
.field("base_path", &self.base_path)
.field("timeout", &self.timeout)
.field("keep_alive", &self.keep_alive)
.field("compression", &self.compression)
.field("retries", &self.retries)
.field("backoff", &self.backoff)
.field("proxy", &self.proxy)
.field("custom_headers", &self.custom_headers)
.field("agent", &self.agent)
.field(
"private_key",
&self.private_key.as_ref().map(|_| "[REDACTED]"),
)
.field(
"private_key_passphrase",
&self.private_key_passphrase.as_ref().map(|_| "[REDACTED]"),
)
.field("time_unit", &self.time_unit)
.field("client", &"<reqwest::Client>")
.field("user_agent", &self.user_agent)
.field("signature_gen", &self.signature_gen)
.finish()
}
}
impl ConfigurationRestApi {
#[must_use]
pub fn builder() -> ConfigurationRestApiBuilder {
ConfigurationRestApiBuilder::default()
}
}
impl ConfigurationRestApiBuilder {
/// Builds a `ConfigurationRestApi` instance with configured HTTP client and signature generator.
///
/// # Returns
///
/// A `Result` containing the fully configured `ConfigurationRestApi` or a `ConfigBuildError` if configuration fails.
///
/// # Errors
///
/// Returns a `ConfigBuildError` if the initial configuration build fails or if client setup encounters issues.
pub fn build(self) -> Result<ConfigurationRestApi, ConfigBuildError> {
let mut cfg = self.try_build()?;
cfg.client = build_client(
cfg.timeout,
cfg.keep_alive,
cfg.proxy.as_ref(),
cfg.agent.clone(),
);
cfg.signature_gen = SignatureGenerator::new(
cfg.api_secret.clone(),
cfg.private_key.clone(),
cfg.private_key_passphrase.clone(),
);
Ok(cfg)
}
}
#[derive(Clone, Builder)]
#[builder(
pattern = "owned",
build_fn(name = "try_build", error = "ConfigBuildError")
)]
pub struct ConfigurationWebsocketApi {
#[builder(setter(into, strip_option), default)]
pub api_key: Option<String>,
#[builder(setter(into, strip_option), default)]
pub api_secret: Option<String>,
#[builder(setter(into, strip_option), default)]
pub ws_url: Option<String>,
#[builder(default = "5000")]
pub timeout: u64,
#[builder(default = "5000")]
pub reconnect_delay: u64,
#[builder(default = "WebsocketMode::Single")]
pub mode: WebsocketMode,
#[builder(setter(strip_option), default)]
pub agent: Option<AgentConnector>,
#[builder(setter(strip_option), default)]
pub private_key: Option<PrivateKey>,
#[builder(setter(strip_option), default)]
pub private_key_passphrase: Option<String>,
#[builder(setter(strip_option), default)]
pub time_unit: Option<TimeUnit>,
#[builder(default = "true")]
pub auto_session_relogon: bool,
#[builder(setter(skip))]
pub(crate) user_agent: String,
#[builder(setter(skip))]
pub(crate) signature_gen: SignatureGenerator,
}
impl fmt::Debug for ConfigurationWebsocketApi {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ConfigurationWebsocketApi")
.field("api_key", &self.api_key.as_ref().map(|_| "[REDACTED]"))
.field(
"api_secret",
&self.api_secret.as_ref().map(|_| "[REDACTED]"),
)
.field("ws_url", &self.ws_url)
.field("timeout", &self.timeout)
.field("reconnect_delay", &self.reconnect_delay)
.field("mode", &self.mode)
.field("agent", &self.agent)
.field(
"private_key",
&self.private_key.as_ref().map(|_| "[REDACTED]"),
)
.field(
"private_key_passphrase",
&self.private_key_passphrase.as_ref().map(|_| "[REDACTED]"),
)
.field("time_unit", &self.time_unit)
.field("auto_session_relogon", &self.auto_session_relogon)
.field("user_agent", &self.user_agent)
.field("signature_gen", &self.signature_gen)
.finish()
}
}
impl ConfigurationWebsocketApi {
/// Creates a builder for `ConfigurationWebsocketApi` with the specified API key.
///
/// # Arguments
///
/// * `api_key` - The API key to be used for the WebSocket API configuration
///
/// # Returns
///
/// A `ConfigurationWebsocketApiBuilder` initialized with the provided API key
#[must_use]
pub fn builder() -> ConfigurationWebsocketApiBuilder {
ConfigurationWebsocketApiBuilder::default()
}
}
impl ConfigurationWebsocketApiBuilder {
/// Builds the `ConfigurationWebsocketApi` with a generated signature generator.
///
/// This method attempts to build the configuration using the builder's settings
/// and then initializes the signature generator with the API secret, private key,
/// and private key passphrase.
///
/// # Returns
///
/// A `Result` containing the fully configured `ConfigurationWebsocketApi` or a
/// `ConfigBuildError` if the build process fails.
///
/// # Errors
///
/// Returns a `ConfigBuildError` if the initial configuration build fails or if signature generation fails.
///
pub fn build(self) -> Result<ConfigurationWebsocketApi, ConfigBuildError> {
let mut cfg = self.try_build()?;
cfg.signature_gen = SignatureGenerator::new(
cfg.api_secret.clone(),
cfg.private_key.clone(),
cfg.private_key_passphrase.clone(),
);
Ok(cfg)
}
}
#[derive(Debug, Clone, Builder)]
#[builder(pattern = "owned", build_fn(error = "ConfigBuildError"))]
pub struct ConfigurationWebsocketStreams {
#[builder(setter(into, strip_option), default)]
pub ws_url: Option<String>,
#[builder(default = "5000")]
pub reconnect_delay: u64,
#[builder(default = "WebsocketMode::Single")]
pub mode: WebsocketMode,
#[builder(setter(strip_option), default)]
pub agent: Option<AgentConnector>,
#[builder(setter(strip_option), default)]
pub time_unit: Option<TimeUnit>,
#[builder(setter(skip))]
pub(crate) user_agent: String,
}
impl ConfigurationWebsocketStreams {
#[must_use]
/// Creates a builder for `ConfigurationWebsocketStreams` with default settings.
///
/// # Returns
///
/// A `ConfigurationWebsocketStreamsBuilder` initialized with default values
pub fn builder() -> ConfigurationWebsocketStreamsBuilder {
ConfigurationWebsocketStreamsBuilder::default()
}
}