binance/config.rs
1pub static DATA_REST_ENDPOINT: &str = "https://api.binance.com";
2
3#[derive(Clone, Debug, PartialEq, Eq)]
4pub struct Config {
5 pub rest_api_endpoint: String,
6 pub ws_endpoint: String,
7
8 pub futures_rest_api_endpoint: String,
9 pub futures_ws_endpoint: String,
10
11 pub recv_window: u64,
12
13 pub binance_us_api: bool,
14
15 pub timeout: Option<u64>,
16}
17
18impl Config {
19 /// Configure binance with all testnet endpoints
20 /// # Examples
21 /// ```
22 /// use binance::config::Config;
23 /// let config = Config::testnet();
24 /// ```
25 pub fn testnet() -> Config {
26 Config::default()
27 .set_rest_api_endpoint("https://testnet.binance.vision")
28 .set_ws_endpoint("wss://testnet.binance.vision")
29 .set_futures_rest_api_endpoint("https://testnet.binancefuture.com")
30 .set_futures_ws_endpoint("wss://testnet.binancefuture.com")
31 }
32
33 /// Sets the rest api endpoint. Defaults to <https://api.binance.com>.
34 ///
35 /// # Arguments
36 ///
37 /// * `rest_api_endpoint`:
38 ///
39 /// returns: Config
40 ///
41 /// # Examples
42 ///
43 /// ```
44 /// use binance::config::Config;
45 /// let config = Config::default();
46 /// config.set_rest_api_endpoint("http://myendpoint:8080");
47 /// ```
48 pub fn set_rest_api_endpoint<T: Into<String>>(mut self, rest_api_endpoint: T) -> Self {
49 self.rest_api_endpoint = rest_api_endpoint.into();
50 self
51 }
52
53 /// Sets the websocket endpoint. Defaults to "wss://stream.binance.com:9443".
54 ///
55 /// # Arguments
56 ///
57 /// * `ws_endpoint`:
58 ///
59 /// returns: Config
60 ///
61 /// # Examples
62 ///
63 /// ```
64 /// use binance::config::Config;
65 /// let config = Config::default();
66 /// config.set_ws_endpoint("ws://myendpoint:8080");
67 /// ```
68 pub fn set_ws_endpoint<T: Into<String>>(mut self, ws_endpoint: T) -> Self {
69 self.ws_endpoint = ws_endpoint.into();
70 self
71 }
72
73 /// Sets the futures rest api endpoint. Defaults to <https://fapi.binance.com>.
74 ///
75 /// # Arguments
76 ///
77 /// * `futures_rest_api_endpoint`:
78 ///
79 /// returns: Config
80 ///
81 /// # Examples
82 ///
83 /// ```
84 /// use binance::config::Config;
85 /// let config = Config::default();
86 /// config.set_futures_rest_api_endpoint("http://myendpoint:8080");
87 /// ```
88 pub fn set_futures_rest_api_endpoint<T: Into<String>>(mut self, futures_rest_api_endpoint: T) -> Self {
89 self.futures_rest_api_endpoint = futures_rest_api_endpoint.into();
90 self
91 }
92
93 /// Sets the futures websocket endpoint. Defaults to "wss://fstream.binance.com".
94 ///
95 /// # Arguments
96 ///
97 /// * `futures_ws_endpoint`:
98 ///
99 /// returns: Config
100 ///
101 /// # Examples
102 ///
103 /// ```
104 /// use binance::config::Config;
105 /// let config = Config::default();
106 /// config.set_futures_ws_endpoint("ws://myendpoint:8080");
107 /// ```
108 pub fn set_futures_ws_endpoint<T: Into<String>>(mut self, futures_ws_endpoint: T) -> Self {
109 self.futures_ws_endpoint = futures_ws_endpoint.into();
110 self
111 }
112
113 /// Sets the 'receive window'. The receive window is the number of milliseconds after timestamp
114 /// the request is valid for.
115 ///
116 /// # Arguments
117 ///
118 /// * `recv_window`: The receive window, in milliseconds. Defaults to 5000.
119 ///
120 /// returns: Config
121 ///
122 /// # Examples
123 ///
124 /// ```
125 /// use binance::config::Config;
126 /// let config = Config::default();
127 /// config.set_recv_window(300);
128 /// ```
129 pub fn set_recv_window(mut self, recv_window: u64) -> Self {
130 self.recv_window = recv_window;
131 self
132 }
133
134 /// Sets the client timeout
135 ///
136 /// # Arguments
137 ///
138 /// * `timeout`: The timeout, in seconds
139 ///
140 /// returns: Config
141 ///
142 /// # Examples
143 ///
144 /// ```
145 /// use binance::config::Config;
146 /// let config = Config::default();
147 /// config.set_timeout(3);
148 /// ```
149 pub fn set_timeout(mut self, timeout: u64) -> Self {
150 self.timeout = Some(timeout);
151 self
152 }
153}
154
155impl Default for Config {
156 /// Configure binance with default production endpoints
157 /// # Examples
158 /// ```
159 /// use binance::config::Config;
160 /// let config = Config::default();
161 /// ```
162 fn default() -> Config {
163 Config {
164 rest_api_endpoint: "https://api.binance.com".into(),
165 ws_endpoint: "wss://stream.binance.com:9443".into(),
166
167 futures_rest_api_endpoint: "https://fapi.binance.com".into(),
168 futures_ws_endpoint: "wss://fstream.binance.com".into(),
169
170 recv_window: 5000,
171 binance_us_api: false,
172
173 timeout: None,
174 }
175 }
176}