1use std::time::Duration;
4
5use crate::constants::{MAINNET, MAINNET_WS_PRIVATE, MAINNET_WS_PUBLIC_LINEAR};
6
7#[derive(Debug, Clone)]
9pub struct ClientConfig {
10 pub api_key: String,
12 pub api_secret: String,
14 pub base_url: String,
16 pub timeout: Duration,
18 pub recv_window: u64,
20 pub debug: bool,
22}
23
24impl ClientConfig {
25 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#[derive(Debug, Clone)]
49pub struct ClientConfigBuilder {
50 config: ClientConfig,
51}
52
53impl ClientConfigBuilder {
54 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 pub fn base_url(mut self, url: impl Into<String>) -> Self {
67 self.config.base_url = url.into();
68 self
69 }
70
71 pub fn timeout(mut self, timeout: Duration) -> Self {
73 self.config.timeout = timeout;
74 self
75 }
76
77 pub fn recv_window(mut self, recv_window: u64) -> Self {
79 self.config.recv_window = recv_window;
80 self
81 }
82
83 pub fn debug(mut self, debug: bool) -> Self {
85 self.config.debug = debug;
86 self
87 }
88
89 pub fn build(self) -> ClientConfig {
91 self.config
92 }
93}
94
95#[derive(Debug, Clone)]
97pub struct WsConfig {
98 pub api_key: Option<String>,
100 pub api_secret: Option<String>,
102 pub url: String,
104 pub ping_interval: u64,
106 pub max_reconnect_attempts: u32,
108 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 pub fn public(url: impl Into<String>) -> Self {
128 Self {
129 url: url.into(),
130 ..Default::default()
131 }
132 }
133
134 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 pub fn with_url(mut self, url: impl Into<String>) -> Self {
146 self.url = url.into();
147 self
148 }
149
150 pub fn with_ping_interval(mut self, interval: u64) -> Self {
152 self.ping_interval = interval;
153 self
154 }
155
156 pub fn with_max_reconnect_attempts(mut self, attempts: u32) -> Self {
158 self.max_reconnect_attempts = attempts;
159 self
160 }
161}