Skip to main content

bybit_api/
config.rs

1//! Configuration for the Bybit API client.
2
3use std::time::Duration;
4
5use crate::constants::{MAINNET, MAINNET_WS_PRIVATE, MAINNET_WS_PUBLIC_LINEAR};
6
7/// Configuration for the Bybit HTTP client.
8#[derive(Debug, Clone)]
9pub struct ClientConfig {
10    /// API key for authentication
11    pub api_key: String,
12    /// API secret for signing requests
13    pub api_secret: String,
14    /// Base URL for REST API
15    pub base_url: String,
16    /// Request timeout
17    pub timeout: Duration,
18    /// Receive window for timestamp validation (milliseconds)
19    pub recv_window: u64,
20    /// Enable debug logging
21    pub debug: bool,
22}
23
24impl ClientConfig {
25    /// Create a new configuration builder.
26    pub fn builder(
27        api_key: impl Into<String>,
28        api_secret: impl Into<String>,
29    ) -> ClientConfigBuilder {
30        ClientConfigBuilder::new(api_key, api_secret)
31    }
32}
33
34impl Default for ClientConfig {
35    fn default() -> Self {
36        Self {
37            api_key: String::new(),
38            api_secret: String::new(),
39            base_url: MAINNET.to_string(),
40            timeout: Duration::from_secs(30),
41            recv_window: 5000,
42            debug: false,
43        }
44    }
45}
46
47/// Builder for ClientConfig.
48#[derive(Debug, Clone)]
49pub struct ClientConfigBuilder {
50    config: ClientConfig,
51}
52
53impl ClientConfigBuilder {
54    /// Create a new builder with required credentials.
55    pub fn new(api_key: impl Into<String>, api_secret: impl Into<String>) -> Self {
56        Self {
57            config: ClientConfig {
58                api_key: api_key.into(),
59                api_secret: api_secret.into(),
60                ..Default::default()
61            },
62        }
63    }
64
65    /// Set the base URL for REST API.
66    pub fn base_url(mut self, url: impl Into<String>) -> Self {
67        self.config.base_url = url.into();
68        self
69    }
70
71    /// Set the request timeout.
72    pub fn timeout(mut self, timeout: Duration) -> Self {
73        self.config.timeout = timeout;
74        self
75    }
76
77    /// Set the receive window (milliseconds).
78    pub fn recv_window(mut self, recv_window: u64) -> Self {
79        self.config.recv_window = recv_window;
80        self
81    }
82
83    /// Enable or disable debug logging.
84    pub fn debug(mut self, debug: bool) -> Self {
85        self.config.debug = debug;
86        self
87    }
88
89    /// Build the configuration.
90    pub fn build(self) -> ClientConfig {
91        self.config
92    }
93}
94
95/// Configuration for WebSocket client.
96#[derive(Debug, Clone)]
97pub struct WsConfig {
98    /// API key for private channels
99    pub api_key: Option<String>,
100    /// API secret for private channels
101    pub api_secret: Option<String>,
102    /// WebSocket URL
103    pub url: String,
104    /// Ping interval in seconds
105    pub ping_interval: u64,
106    /// Maximum reconnection attempts (0 = infinite)
107    pub max_reconnect_attempts: u32,
108    /// Reconnection delay in seconds
109    pub reconnect_delay: u64,
110}
111
112impl Default for WsConfig {
113    fn default() -> Self {
114        Self {
115            api_key: None,
116            api_secret: None,
117            url: MAINNET_WS_PUBLIC_LINEAR.to_string(),
118            ping_interval: 20,
119            max_reconnect_attempts: 10,
120            reconnect_delay: 5,
121        }
122    }
123}
124
125impl WsConfig {
126    /// Create a public WebSocket configuration.
127    pub fn public(url: impl Into<String>) -> Self {
128        Self {
129            url: url.into(),
130            ..Default::default()
131        }
132    }
133
134    /// Create a private WebSocket configuration.
135    pub fn private(api_key: impl Into<String>, api_secret: impl Into<String>) -> Self {
136        Self {
137            api_key: Some(api_key.into()),
138            api_secret: Some(api_secret.into()),
139            url: MAINNET_WS_PRIVATE.to_string(),
140            ..Default::default()
141        }
142    }
143
144    /// Set the WebSocket URL.
145    pub fn with_url(mut self, url: impl Into<String>) -> Self {
146        self.url = url.into();
147        self
148    }
149
150    /// Set the ping interval in seconds.
151    pub fn with_ping_interval(mut self, interval: u64) -> Self {
152        self.ping_interval = interval;
153        self
154    }
155
156    /// Set the maximum reconnection attempts.
157    pub fn with_max_reconnect_attempts(mut self, attempts: u32) -> Self {
158        self.max_reconnect_attempts = attempts;
159        self
160    }
161}