deribit_base/model/
config.rs1use crate::{impl_json_debug_pretty, impl_json_display};
8use serde::{Deserialize, Serialize};
9
10#[derive(Clone, Serialize, Deserialize)]
12pub struct DeribitConfig {
13 pub client_id: String,
15 pub client_secret: String,
17 pub test_net: bool,
19 pub timeout_seconds: u64,
21 pub max_retries: u32,
23 pub rate_limit: Option<u32>,
25 pub user_agent: Option<String>,
27}
28
29impl DeribitConfig {
30 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 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 pub fn with_timeout(mut self, timeout_seconds: u64) -> Self {
58 self.timeout_seconds = timeout_seconds;
59 self
60 }
61
62 pub fn with_max_retries(mut self, max_retries: u32) -> Self {
64 self.max_retries = max_retries;
65 self
66 }
67
68 pub fn with_rate_limit(mut self, rate_limit: u32) -> Self {
70 self.rate_limit = Some(rate_limit);
71 self
72 }
73
74 pub fn with_user_agent(mut self, user_agent: String) -> Self {
76 self.user_agent = Some(user_agent);
77 self
78 }
79
80 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 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 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, 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
118pub struct DeribitUrls;
120
121impl DeribitUrls {
122 pub const PROD_BASE_URL: &'static str = "https://www.deribit.com";
124 pub const TEST_BASE_URL: &'static str = "https://test.deribit.com";
126 pub const PROD_WS_URL: &'static str = "wss://www.deribit.com/ws/api/v2";
128 pub const TEST_WS_URL: &'static str = "wss://test.deribit.com/ws/api/v2";
130}
131
132#[derive(Clone, Serialize, Deserialize)]
134pub struct WebSocketConfig {
135 pub base: DeribitConfig,
137 pub ping_interval: u64,
139 pub pong_timeout: u64,
141 pub reconnect_attempts: u32,
143 pub reconnect_delay: u64,
145 pub max_message_size: usize,
147 pub compression: bool,
149}
150
151impl WebSocketConfig {
152 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, compression: true,
162 }
163 }
164
165 pub fn with_ping_interval(mut self, ping_interval: u64) -> Self {
167 self.ping_interval = ping_interval;
168 self
169 }
170
171 pub fn with_pong_timeout(mut self, pong_timeout: u64) -> Self {
173 self.pong_timeout = pong_timeout;
174 self
175 }
176
177 pub fn with_reconnect_attempts(mut self, reconnect_attempts: u32) -> Self {
179 self.reconnect_attempts = reconnect_attempts;
180 self
181 }
182
183 pub fn with_reconnect_delay(mut self, reconnect_delay: u64) -> Self {
185 self.reconnect_delay = reconnect_delay;
186 self
187 }
188
189 pub fn with_compression(mut self, compression: bool) -> Self {
191 self.compression = compression;
192 self
193 }
194}
195
196#[derive(Clone, Serialize, Deserialize)]
198pub struct HttpConfig {
199 pub base: DeribitConfig,
201 pub pool_size: Option<usize>,
203 pub keep_alive: Option<u64>,
205 pub http2: bool,
207 pub gzip: bool,
209}
210
211impl HttpConfig {
212 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 pub fn with_pool_size(mut self, pool_size: usize) -> Self {
225 self.pool_size = Some(pool_size);
226 self
227 }
228
229 pub fn with_keep_alive(mut self, keep_alive: u64) -> Self {
231 self.keep_alive = Some(keep_alive);
232 self
233 }
234
235 pub fn with_http2(mut self, http2: bool) -> Self {
237 self.http2 = http2;
238 self
239 }
240
241 pub fn with_gzip(mut self, gzip: bool) -> Self {
243 self.gzip = gzip;
244 self
245 }
246}
247
248impl_json_debug_pretty!(DeribitConfig, WebSocketConfig, HttpConfig);
250
251impl_json_display!(DeribitConfig, WebSocketConfig, HttpConfig);