deribit_base/model/
config.rs

1/******************************************************************************
2   Author: Joaquín Béjar García
3   Email: jb@taunais.com
4   Date: 21/7/25
5******************************************************************************/
6
7use crate::{impl_json_debug_pretty, impl_json_display};
8use serde::{Deserialize, Serialize};
9
10/// Deribit API configuration
11#[derive(Clone, Serialize, Deserialize)]
12pub struct DeribitConfig {
13    /// Client ID for API authentication
14    pub client_id: String,
15    /// Client secret for API authentication
16    pub client_secret: String,
17    /// Whether to use testnet
18    pub test_net: bool,
19    /// Request timeout in seconds
20    pub timeout_seconds: u64,
21    /// Maximum number of retries
22    pub max_retries: u32,
23    /// Rate limit per second
24    pub rate_limit: Option<u32>,
25    /// User agent string
26    pub user_agent: Option<String>,
27}
28
29impl DeribitConfig {
30    /// Create a new configuration for production
31    pub fn new(client_id: String, client_secret: String) -> Self {
32        Self {
33            client_id,
34            client_secret,
35            test_net: false,
36            timeout_seconds: 30,
37            max_retries: 3,
38            rate_limit: None,
39            user_agent: None,
40        }
41    }
42
43    /// Create a new configuration for testnet
44    pub fn testnet(client_id: String, client_secret: String) -> Self {
45        Self {
46            client_id,
47            client_secret,
48            test_net: true,
49            timeout_seconds: 30,
50            max_retries: 3,
51            rate_limit: None,
52            user_agent: None,
53        }
54    }
55
56    /// Set timeout in seconds
57    pub fn with_timeout(mut self, timeout_seconds: u64) -> Self {
58        self.timeout_seconds = timeout_seconds;
59        self
60    }
61
62    /// Set maximum retries
63    pub fn with_max_retries(mut self, max_retries: u32) -> Self {
64        self.max_retries = max_retries;
65        self
66    }
67
68    /// Set rate limit
69    pub fn with_rate_limit(mut self, rate_limit: u32) -> Self {
70        self.rate_limit = Some(rate_limit);
71        self
72    }
73
74    /// Set user agent
75    pub fn with_user_agent(mut self, user_agent: String) -> Self {
76        self.user_agent = Some(user_agent);
77        self
78    }
79
80    /// Get the base URL for HTTP API
81    pub fn base_url(&self) -> &'static str {
82        if self.test_net {
83            DeribitUrls::TEST_BASE_URL
84        } else {
85            DeribitUrls::PROD_BASE_URL
86        }
87    }
88
89    /// Get the WebSocket URL
90    pub fn ws_url(&self) -> &'static str {
91        if self.test_net {
92            DeribitUrls::TEST_WS_URL
93        } else {
94            DeribitUrls::PROD_WS_URL
95        }
96    }
97
98    /// Get the API URL for HTTP requests
99    pub fn api_url(&self) -> String {
100        format!("{}/api/v2", self.base_url())
101    }
102}
103
104impl Default for DeribitConfig {
105    fn default() -> Self {
106        Self {
107            client_id: String::new(),
108            client_secret: String::new(),
109            test_net: true, // Default to testnet for safety
110            timeout_seconds: 30,
111            max_retries: 3,
112            rate_limit: None,
113            user_agent: Some("deribit-rust-client/1.0".to_string()),
114        }
115    }
116}
117
118/// Deribit API URLs
119pub struct DeribitUrls;
120
121impl DeribitUrls {
122    /// Production base URL
123    pub const PROD_BASE_URL: &'static str = "https://www.deribit.com";
124    /// Test base URL
125    pub const TEST_BASE_URL: &'static str = "https://test.deribit.com";
126    /// Production WebSocket URL
127    pub const PROD_WS_URL: &'static str = "wss://www.deribit.com/ws/api/v2";
128    /// Test WebSocket URL
129    pub const TEST_WS_URL: &'static str = "wss://test.deribit.com/ws/api/v2";
130}
131
132/// Connection configuration for WebSocket
133#[derive(Clone, Serialize, Deserialize)]
134pub struct WebSocketConfig {
135    /// Base configuration
136    pub base: DeribitConfig,
137    /// Ping interval in seconds
138    pub ping_interval: u64,
139    /// Pong timeout in seconds
140    pub pong_timeout: u64,
141    /// Reconnect attempts
142    pub reconnect_attempts: u32,
143    /// Reconnect delay in seconds
144    pub reconnect_delay: u64,
145    /// Maximum message size
146    pub max_message_size: usize,
147    /// Enable compression
148    pub compression: bool,
149}
150
151impl WebSocketConfig {
152    /// Create new WebSocket configuration
153    pub fn new(base: DeribitConfig) -> Self {
154        Self {
155            base,
156            ping_interval: 30,
157            pong_timeout: 10,
158            reconnect_attempts: 5,
159            reconnect_delay: 5,
160            max_message_size: 1024 * 1024, // 1MB
161            compression: true,
162        }
163    }
164
165    /// Set ping interval
166    pub fn with_ping_interval(mut self, ping_interval: u64) -> Self {
167        self.ping_interval = ping_interval;
168        self
169    }
170
171    /// Set pong timeout
172    pub fn with_pong_timeout(mut self, pong_timeout: u64) -> Self {
173        self.pong_timeout = pong_timeout;
174        self
175    }
176
177    /// Set reconnect attempts
178    pub fn with_reconnect_attempts(mut self, reconnect_attempts: u32) -> Self {
179        self.reconnect_attempts = reconnect_attempts;
180        self
181    }
182
183    /// Set reconnect delay
184    pub fn with_reconnect_delay(mut self, reconnect_delay: u64) -> Self {
185        self.reconnect_delay = reconnect_delay;
186        self
187    }
188
189    /// Enable/disable compression
190    pub fn with_compression(mut self, compression: bool) -> Self {
191        self.compression = compression;
192        self
193    }
194}
195
196/// HTTP client configuration
197#[derive(Clone, Serialize, Deserialize)]
198pub struct HttpConfig {
199    /// Base configuration
200    pub base: DeribitConfig,
201    /// Connection pool size
202    pub pool_size: Option<usize>,
203    /// Keep alive timeout
204    pub keep_alive: Option<u64>,
205    /// Enable HTTP/2
206    pub http2: bool,
207    /// Enable gzip compression
208    pub gzip: bool,
209}
210
211impl HttpConfig {
212    /// Create new HTTP configuration
213    pub fn new(base: DeribitConfig) -> Self {
214        Self {
215            base,
216            pool_size: None,
217            keep_alive: Some(30),
218            http2: true,
219            gzip: true,
220        }
221    }
222
223    /// Set connection pool size
224    pub fn with_pool_size(mut self, pool_size: usize) -> Self {
225        self.pool_size = Some(pool_size);
226        self
227    }
228
229    /// Set keep alive timeout
230    pub fn with_keep_alive(mut self, keep_alive: u64) -> Self {
231        self.keep_alive = Some(keep_alive);
232        self
233    }
234
235    /// Enable/disable HTTP/2
236    pub fn with_http2(mut self, http2: bool) -> Self {
237        self.http2 = http2;
238        self
239    }
240
241    /// Enable/disable gzip compression
242    pub fn with_gzip(mut self, gzip: bool) -> Self {
243        self.gzip = gzip;
244        self
245    }
246}
247
248// Debug implementations using pretty JSON formatting
249impl_json_debug_pretty!(DeribitConfig, WebSocketConfig, HttpConfig);
250
251// Display implementations using compact JSON formatting
252impl_json_display!(DeribitConfig, WebSocketConfig, HttpConfig);