ccxt_core/base_exchange/
config.rs1use crate::config::{ProxyConfig, RetryPolicy};
4use crate::credentials::SecretString;
5use serde_json::Value;
6use std::collections::HashMap;
7use std::time::Duration;
8
9#[derive(Debug, Clone)]
11pub struct ExchangeConfig {
12 pub id: String,
14 pub name: String,
16 pub api_key: Option<SecretString>,
18 pub secret: Option<SecretString>,
20 pub password: Option<SecretString>,
22 pub uid: Option<String>,
24 pub account_id: Option<String>,
26 pub enable_rate_limit: bool,
28 pub rate_limit: u32,
30 pub timeout: Duration,
32 pub connect_timeout: Duration,
34 pub retry_policy: Option<RetryPolicy>,
36 pub sandbox: bool,
38 pub user_agent: Option<String>,
40 pub proxy: Option<ProxyConfig>,
42 pub verbose: bool,
44 pub options: HashMap<String, Value>,
46 pub url_overrides: HashMap<String, String>,
48}
49
50impl Default for ExchangeConfig {
51 fn default() -> Self {
52 Self {
53 id: String::new(),
54 name: String::new(),
55 api_key: None,
56 secret: None,
57 password: None,
58 uid: None,
59 account_id: None,
60 enable_rate_limit: true,
61 rate_limit: 10,
62 timeout: Duration::from_secs(30),
63 connect_timeout: Duration::from_secs(10),
64 retry_policy: None,
65 sandbox: false,
66 user_agent: Some(format!("ccxt-rust/{}", env!("CARGO_PKG_VERSION"))),
67 proxy: None,
68 verbose: false,
69 options: HashMap::new(),
70 url_overrides: HashMap::new(),
71 }
72 }
73}
74
75impl ExchangeConfig {
76 pub fn builder() -> ExchangeConfigBuilder {
92 ExchangeConfigBuilder::default()
93 }
94}
95
96#[derive(Debug, Clone, Default)]
115pub struct ExchangeConfigBuilder {
116 config: ExchangeConfig,
117}
118
119impl ExchangeConfigBuilder {
120 pub fn new() -> Self {
122 Self::default()
123 }
124
125 pub fn id(mut self, id: impl Into<String>) -> Self {
127 self.config.id = id.into();
128 self
129 }
130
131 pub fn name(mut self, name: impl Into<String>) -> Self {
133 self.config.name = name.into();
134 self
135 }
136
137 pub fn api_key(mut self, key: impl Into<String>) -> Self {
139 self.config.api_key = Some(SecretString::new(key));
140 self
141 }
142
143 pub fn secret(mut self, secret: impl Into<String>) -> Self {
145 self.config.secret = Some(SecretString::new(secret));
146 self
147 }
148
149 pub fn password(mut self, password: impl Into<String>) -> Self {
151 self.config.password = Some(SecretString::new(password));
152 self
153 }
154
155 pub fn uid(mut self, uid: impl Into<String>) -> Self {
157 self.config.uid = Some(uid.into());
158 self
159 }
160
161 pub fn account_id(mut self, account_id: impl Into<String>) -> Self {
163 self.config.account_id = Some(account_id.into());
164 self
165 }
166
167 pub fn enable_rate_limit(mut self, enabled: bool) -> Self {
169 self.config.enable_rate_limit = enabled;
170 self
171 }
172
173 pub fn rate_limit(mut self, rate_limit: u32) -> Self {
175 self.config.rate_limit = rate_limit;
176 self
177 }
178
179 pub fn timeout(mut self, timeout: Duration) -> Self {
181 self.config.timeout = timeout;
182 self
183 }
184
185 pub fn connect_timeout(mut self, timeout: Duration) -> Self {
187 self.config.connect_timeout = timeout;
188 self
189 }
190
191 pub fn connect_timeout_secs(mut self, seconds: u64) -> Self {
193 self.config.connect_timeout = Duration::from_secs(seconds);
194 self
195 }
196
197 pub fn retry_policy(mut self, policy: RetryPolicy) -> Self {
199 self.config.retry_policy = Some(policy);
200 self
201 }
202
203 pub fn sandbox(mut self, enabled: bool) -> Self {
205 self.config.sandbox = enabled;
206 self
207 }
208
209 pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
211 self.config.user_agent = Some(user_agent.into());
212 self
213 }
214
215 pub fn proxy(mut self, proxy: ProxyConfig) -> Self {
217 self.config.proxy = Some(proxy);
218 self
219 }
220
221 pub fn proxy_url(mut self, url: impl Into<String>) -> Self {
223 self.config.proxy = Some(ProxyConfig::new(url));
224 self
225 }
226
227 pub fn verbose(mut self, enabled: bool) -> Self {
229 self.config.verbose = enabled;
230 self
231 }
232
233 pub fn option(mut self, key: impl Into<String>, value: Value) -> Self {
235 self.config.options.insert(key.into(), value);
236 self
237 }
238
239 pub fn options(mut self, options: HashMap<String, Value>) -> Self {
241 self.config.options.extend(options);
242 self
243 }
244
245 pub fn url_override(mut self, key: impl Into<String>, url: impl Into<String>) -> Self {
247 self.config.url_overrides.insert(key.into(), url.into());
248 self
249 }
250
251 pub fn build(self) -> ExchangeConfig {
253 self.config
254 }
255}