deribit_base/model/
config.rs1use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct DeribitConfig {
12 pub client_id: String,
14 pub client_secret: String,
16 pub test_net: bool,
18 pub timeout_seconds: u64,
20 pub max_retries: u32,
22 pub rate_limit: Option<u32>,
24 pub user_agent: Option<String>,
26}
27
28impl DeribitConfig {
29 pub fn new(client_id: String, client_secret: String) -> Self {
31 Self {
32 client_id,
33 client_secret,
34 test_net: false,
35 timeout_seconds: 30,
36 max_retries: 3,
37 rate_limit: None,
38 user_agent: None,
39 }
40 }
41
42 pub fn testnet(client_id: String, client_secret: String) -> Self {
44 Self {
45 client_id,
46 client_secret,
47 test_net: true,
48 timeout_seconds: 30,
49 max_retries: 3,
50 rate_limit: None,
51 user_agent: None,
52 }
53 }
54
55 pub fn with_timeout(mut self, timeout_seconds: u64) -> Self {
57 self.timeout_seconds = timeout_seconds;
58 self
59 }
60
61 pub fn with_max_retries(mut self, max_retries: u32) -> Self {
63 self.max_retries = max_retries;
64 self
65 }
66
67 pub fn with_rate_limit(mut self, rate_limit: u32) -> Self {
69 self.rate_limit = Some(rate_limit);
70 self
71 }
72
73 pub fn with_user_agent(mut self, user_agent: String) -> Self {
75 self.user_agent = Some(user_agent);
76 self
77 }
78
79 pub fn base_url(&self) -> &'static str {
81 if self.test_net {
82 DeribitUrls::TEST_BASE_URL
83 } else {
84 DeribitUrls::PROD_BASE_URL
85 }
86 }
87
88 pub fn ws_url(&self) -> &'static str {
90 if self.test_net {
91 DeribitUrls::TEST_WS_URL
92 } else {
93 DeribitUrls::PROD_WS_URL
94 }
95 }
96
97 pub fn api_url(&self) -> String {
99 format!("{}/api/v2", self.base_url())
100 }
101}
102
103impl Default for DeribitConfig {
104 fn default() -> Self {
105 Self {
106 client_id: String::new(),
107 client_secret: String::new(),
108 test_net: true, timeout_seconds: 30,
110 max_retries: 3,
111 rate_limit: None,
112 user_agent: Some("deribit-rust-client/1.0".to_string()),
113 }
114 }
115}
116
117pub struct DeribitUrls;
119
120impl DeribitUrls {
121 pub const PROD_BASE_URL: &'static str = "https://www.deribit.com";
123 pub const TEST_BASE_URL: &'static str = "https://test.deribit.com";
125 pub const PROD_WS_URL: &'static str = "wss://www.deribit.com/ws/api/v2";
127 pub const TEST_WS_URL: &'static str = "wss://test.deribit.com/ws/api/v2";
129}
130
131#[derive(Debug, Clone, Serialize, Deserialize)]
133pub struct WebSocketConfig {
134 pub base: DeribitConfig,
136 pub ping_interval: u64,
138 pub pong_timeout: u64,
140 pub reconnect_attempts: u32,
142 pub reconnect_delay: u64,
144 pub max_message_size: usize,
146 pub compression: bool,
148}
149
150impl WebSocketConfig {
151 pub fn new(base: DeribitConfig) -> Self {
153 Self {
154 base,
155 ping_interval: 30,
156 pong_timeout: 10,
157 reconnect_attempts: 5,
158 reconnect_delay: 5,
159 max_message_size: 1024 * 1024, compression: true,
161 }
162 }
163
164 pub fn with_ping_interval(mut self, ping_interval: u64) -> Self {
166 self.ping_interval = ping_interval;
167 self
168 }
169
170 pub fn with_pong_timeout(mut self, pong_timeout: u64) -> Self {
172 self.pong_timeout = pong_timeout;
173 self
174 }
175
176 pub fn with_reconnect_attempts(mut self, reconnect_attempts: u32) -> Self {
178 self.reconnect_attempts = reconnect_attempts;
179 self
180 }
181
182 pub fn with_reconnect_delay(mut self, reconnect_delay: u64) -> Self {
184 self.reconnect_delay = reconnect_delay;
185 self
186 }
187
188 pub fn with_compression(mut self, compression: bool) -> Self {
190 self.compression = compression;
191 self
192 }
193}
194
195#[derive(Debug, Clone, Serialize, Deserialize)]
197pub struct HttpConfig {
198 pub base: DeribitConfig,
200 pub pool_size: Option<usize>,
202 pub keep_alive: Option<u64>,
204 pub http2: bool,
206 pub gzip: bool,
208}
209
210impl HttpConfig {
211 pub fn new(base: DeribitConfig) -> Self {
213 Self {
214 base,
215 pool_size: None,
216 keep_alive: Some(30),
217 http2: true,
218 gzip: true,
219 }
220 }
221
222 pub fn with_pool_size(mut self, pool_size: usize) -> Self {
224 self.pool_size = Some(pool_size);
225 self
226 }
227
228 pub fn with_keep_alive(mut self, keep_alive: u64) -> Self {
230 self.keep_alive = Some(keep_alive);
231 self
232 }
233
234 pub fn with_http2(mut self, http2: bool) -> Self {
236 self.http2 = http2;
237 self
238 }
239
240 pub fn with_gzip(mut self, gzip: bool) -> Self {
242 self.gzip = gzip;
243 self
244 }
245}