ccxt_exchanges/bitget/
builder.rs1use super::{Bitget, BitgetOptions};
7use ccxt_core::{ExchangeConfig, Result};
8use serde_json::Value;
9use std::collections::HashMap;
10
11#[derive(Debug, Clone)]
31pub struct BitgetBuilder {
32 config: ExchangeConfig,
34 options: BitgetOptions,
36}
37
38impl Default for BitgetBuilder {
39 fn default() -> Self {
40 Self::new()
41 }
42}
43
44impl BitgetBuilder {
45 pub fn new() -> Self {
55 Self {
56 config: ExchangeConfig {
57 id: "bitget".to_string(),
58 name: "Bitget".to_string(),
59 ..Default::default()
60 },
61 options: BitgetOptions::default(),
62 }
63 }
64
65 pub fn api_key(mut self, key: impl Into<String>) -> Self {
71 self.config.api_key = Some(key.into());
72 self
73 }
74
75 pub fn secret(mut self, secret: impl Into<String>) -> Self {
81 self.config.secret = Some(secret.into());
82 self
83 }
84
85 pub fn passphrase(mut self, passphrase: impl Into<String>) -> Self {
93 self.config.password = Some(passphrase.into());
94 self
95 }
96
97 pub fn sandbox(mut self, enabled: bool) -> Self {
106 self.config.sandbox = enabled;
107 self.options.demo = enabled;
108 self
109 }
110
111 pub fn product_type(mut self, product_type: impl Into<String>) -> Self {
119 self.options.product_type = product_type.into();
120 self
121 }
122
123 pub fn timeout(mut self, seconds: u64) -> Self {
129 self.config.timeout = seconds;
130 self
131 }
132
133 pub fn recv_window(mut self, millis: u64) -> Self {
142 self.options.recv_window = millis;
143 self
144 }
145
146 pub fn enable_rate_limit(mut self, enabled: bool) -> Self {
152 self.config.enable_rate_limit = enabled;
153 self
154 }
155
156 pub fn proxy(mut self, proxy: impl Into<String>) -> Self {
162 self.config.proxy = Some(proxy.into());
163 self
164 }
165
166 pub fn verbose(mut self, enabled: bool) -> Self {
172 self.config.verbose = enabled;
173 self
174 }
175
176 pub fn option(mut self, key: impl Into<String>, value: Value) -> Self {
183 self.config.options.insert(key.into(), value);
184 self
185 }
186
187 pub fn options(mut self, options: HashMap<String, Value>) -> Self {
193 self.config.options.extend(options);
194 self
195 }
196
197 #[cfg(test)]
199 pub fn get_config(&self) -> &ExchangeConfig {
200 &self.config
201 }
202
203 #[cfg(test)]
205 pub fn get_options(&self) -> &BitgetOptions {
206 &self.options
207 }
208
209 pub fn build(self) -> Result<Bitget> {
219 Bitget::new_with_options(self.config, self.options)
220 }
221}
222
223#[cfg(test)]
224mod tests {
225 use super::*;
226
227 #[test]
228 fn test_builder_default() {
229 let builder = BitgetBuilder::new();
230 assert_eq!(builder.config.id, "bitget");
231 assert_eq!(builder.config.name, "Bitget");
232 assert!(!builder.config.sandbox);
233 assert_eq!(builder.options.product_type, "spot");
234 }
235
236 #[test]
237 fn test_builder_api_key() {
238 let builder = BitgetBuilder::new().api_key("test-key");
239 assert_eq!(builder.config.api_key, Some("test-key".to_string()));
240 }
241
242 #[test]
243 fn test_builder_secret() {
244 let builder = BitgetBuilder::new().secret("test-secret");
245 assert_eq!(builder.config.secret, Some("test-secret".to_string()));
246 }
247
248 #[test]
249 fn test_builder_passphrase() {
250 let builder = BitgetBuilder::new().passphrase("test-passphrase");
251 assert_eq!(builder.config.password, Some("test-passphrase".to_string()));
252 }
253
254 #[test]
255 fn test_builder_sandbox() {
256 let builder = BitgetBuilder::new().sandbox(true);
257 assert!(builder.config.sandbox);
258 assert!(builder.options.demo);
259 }
260
261 #[test]
262 fn test_builder_product_type() {
263 let builder = BitgetBuilder::new().product_type("umcbl");
264 assert_eq!(builder.options.product_type, "umcbl");
265 }
266
267 #[test]
268 fn test_builder_timeout() {
269 let builder = BitgetBuilder::new().timeout(60);
270 assert_eq!(builder.config.timeout, 60);
271 }
272
273 #[test]
274 fn test_builder_recv_window() {
275 let builder = BitgetBuilder::new().recv_window(10000);
276 assert_eq!(builder.options.recv_window, 10000);
277 }
278
279 #[test]
280 fn test_builder_chaining() {
281 let builder = BitgetBuilder::new()
282 .api_key("key")
283 .secret("secret")
284 .passphrase("pass")
285 .sandbox(true)
286 .timeout(30)
287 .recv_window(5000)
288 .product_type("spot");
289
290 assert_eq!(builder.config.api_key, Some("key".to_string()));
291 assert_eq!(builder.config.secret, Some("secret".to_string()));
292 assert_eq!(builder.config.password, Some("pass".to_string()));
293 assert!(builder.config.sandbox);
294 assert_eq!(builder.config.timeout, 30);
295 assert_eq!(builder.options.recv_window, 5000);
296 assert_eq!(builder.options.product_type, "spot");
297 }
298
299 #[test]
300 fn test_builder_build() {
301 let result = BitgetBuilder::new().build();
302 assert!(result.is_ok());
303
304 let bitget = result.unwrap();
305 assert_eq!(bitget.id(), "bitget");
306 assert_eq!(bitget.name(), "Bitget");
307 }
308
309 #[test]
310 fn test_builder_build_with_credentials() {
311 let result = BitgetBuilder::new()
312 .api_key("test-key")
313 .secret("test-secret")
314 .passphrase("test-passphrase")
315 .build();
316
317 assert!(result.is_ok());
318 }
319}