Skip to main content

binance_sdk/spot/
mod.rs

1pub mod rest_api;
2pub mod websocket_api;
3pub mod websocket_streams;
4
5use crate::common::{
6    config::{ConfigurationRestApi, ConfigurationWebsocketApi, ConfigurationWebsocketStreams},
7    constants::{
8        SPOT_REST_API_DEMO_URL, SPOT_REST_API_PROD_URL, SPOT_REST_API_TESTNET_URL,
9        SPOT_WS_API_DEMO_URL, SPOT_WS_API_PROD_URL, SPOT_WS_API_TESTNET_URL,
10        SPOT_WS_STREAMS_DEMO_URL, SPOT_WS_STREAMS_PROD_URL, SPOT_WS_STREAMS_TESTNET_URL,
11    },
12    utils::build_user_agent,
13};
14
15/// Represents the Spot REST API client for interacting with the Binance Spot REST API.
16///
17/// This struct provides methods to create REST API clients for production , demo and testnet environments.
18pub struct SpotRestApi {}
19
20impl SpotRestApi {
21    /// Creates a REST API client with the given configuration.
22    ///
23    /// If no base path is specified in the configuration, defaults to the production Spot REST API URL.
24    ///
25    /// # Arguments
26    ///
27    /// * `config` - Configuration for the REST API client
28    ///
29    /// # Returns
30    ///
31    /// A new REST API client configured with the provided settings
32    #[must_use]
33    pub fn from_config(mut config: ConfigurationRestApi) -> rest_api::RestApi {
34        config.user_agent = build_user_agent("spot");
35        if config.base_path.is_none() {
36            config.base_path = Some(SPOT_REST_API_PROD_URL.to_string());
37        }
38        rest_api::RestApi::new(config)
39    }
40
41    /// Creates a REST API client configured for the production environment.
42    ///
43    /// # Arguments
44    ///
45    /// * `config` - Configuration for the REST API client
46    ///
47    /// # Returns
48    ///
49    /// A new REST API client configured for the production environment
50    #[must_use]
51    pub fn production(mut config: ConfigurationRestApi) -> rest_api::RestApi {
52        config.base_path = Some(SPOT_REST_API_PROD_URL.to_string());
53        SpotRestApi::from_config(config)
54    }
55
56    /// Creates a REST API client configured for the testnet environment.
57    ///
58    /// # Arguments
59    ///
60    /// * `config` - Configuration for the REST API client
61    ///
62    /// # Returns
63    ///
64    /// A new REST API client configured for the testnet environment
65    #[must_use]
66    pub fn testnet(mut config: ConfigurationRestApi) -> rest_api::RestApi {
67        config.base_path = Some(SPOT_REST_API_TESTNET_URL.to_string());
68        SpotRestApi::from_config(config)
69    }
70
71    /// Creates a REST API client configured for the demo environment.
72    ///
73    /// # Arguments
74    ///
75    /// * `config` - Configuration for the REST API client
76    ///
77    /// # Returns
78    ///
79    /// A new REST API client configured for the demo environment
80    #[must_use]
81    pub fn demo(mut config: ConfigurationRestApi) -> rest_api::RestApi {
82        config.base_path = Some(SPOT_REST_API_DEMO_URL.to_string());
83        SpotRestApi::from_config(config)
84    }
85}
86
87/// Represents the Spot WebSocket API client for interacting with the Binance Spot WebSocket API.
88///
89/// This struct provides methods to create WebSocket API clients for production , demo and testnet environments.
90pub struct SpotWsApi {}
91
92impl SpotWsApi {
93    /// Creates a WebSocket API client with the given configuration.
94    ///
95    /// If no WS URL is specified in the configuration, defaults to the production Spot WebSocket API URL.
96    ///
97    /// # Arguments
98    ///
99    /// * `config` - Configuration for the WebSocket API client
100    ///
101    /// # Returns
102    ///
103    /// A new WebSocket API client configured with the provided settings
104    #[must_use]
105    pub fn from_config(mut config: ConfigurationWebsocketApi) -> websocket_api::WebsocketApiHandle {
106        config.user_agent = build_user_agent("spot");
107        if config.ws_url.is_none() {
108            config.ws_url = Some(SPOT_WS_API_PROD_URL.to_string());
109        }
110        websocket_api::WebsocketApiHandle::new(config)
111    }
112
113    /// Creates a WebSocket API client configured for the production environment.
114    ///
115    /// # Arguments
116    ///
117    /// * `config` - Configuration for the WebSocket API client
118    ///
119    /// # Returns
120    ///
121    /// A new WebSocket API client configured for the production environment
122    #[must_use]
123    pub fn production(mut config: ConfigurationWebsocketApi) -> websocket_api::WebsocketApiHandle {
124        config.ws_url = Some(SPOT_WS_API_PROD_URL.to_string());
125        SpotWsApi::from_config(config)
126    }
127
128    /// Creates a WebSocket API client configured for the testnet environment.
129    ///
130    /// # Arguments
131    ///
132    /// * `config` - Configuration for the WebSocket API client
133    ///
134    /// # Returns
135    ///
136    /// A new WebSocket API client configured for the testnet environment
137    #[must_use]
138    pub fn testnet(mut config: ConfigurationWebsocketApi) -> websocket_api::WebsocketApiHandle {
139        config.ws_url = Some(SPOT_WS_API_TESTNET_URL.to_string());
140        SpotWsApi::from_config(config)
141    }
142
143    /// Creates a WebSocket API client configured for the demo environment.
144    ///
145    /// # Arguments
146    ///
147    /// * `config` - Configuration for the WebSocket API client
148    ///
149    /// # Returns
150    ///
151    /// A new WebSocket API client configured for the demo environment
152    #[must_use]
153    pub fn demo(mut config: ConfigurationWebsocketApi) -> websocket_api::WebsocketApiHandle {
154        config.ws_url = Some(SPOT_WS_API_DEMO_URL.to_string());
155        SpotWsApi::from_config(config)
156    }
157}
158
159/// Represents the Spot WebSocket Streams client for interacting with the Binance Spot WebSocket Streams.
160///
161/// This struct provides methods to create WebSocket Streams clients for production , demo and testnet environments.
162pub struct SpotWsStreams {}
163
164impl SpotWsStreams {
165    /// Creates a WebSocket streams client configured with the given settings.
166    ///
167    /// If no WS URL is specified in the configuration, defaults to the production Spot WebSocket Streams URL.
168    ///
169    /// # Arguments
170    ///
171    /// * `config` - Configuration for the WebSocket streams client
172    ///
173    /// # Returns
174    ///
175    /// A new WebSocket streams client configured with the provided settings
176    #[must_use]
177    pub fn from_config(
178        mut config: ConfigurationWebsocketStreams,
179    ) -> websocket_streams::WebsocketStreamsHandle {
180        config.user_agent = build_user_agent("spot");
181        if config.ws_url.is_none() {
182            config.ws_url = Some(SPOT_WS_STREAMS_PROD_URL.to_string());
183        }
184        websocket_streams::WebsocketStreamsHandle::new(config)
185    }
186
187    /// Creates a WebSocket streams client configured for the production environment.
188    ///
189    /// # Arguments
190    ///
191    /// * `config` - Configuration for the WebSocket streams client
192    ///
193    /// # Returns
194    ///
195    /// A new WebSocket streams client configured for the production environment
196    #[must_use]
197    pub fn production(
198        mut config: ConfigurationWebsocketStreams,
199    ) -> websocket_streams::WebsocketStreamsHandle {
200        config.ws_url = Some(SPOT_WS_STREAMS_PROD_URL.to_string());
201        SpotWsStreams::from_config(config)
202    }
203
204    /// Creates a WebSocket streams client configured for the testnet environment.
205    ///
206    /// # Arguments
207    ///
208    /// * `config` - Configuration for the WebSocket streams client
209    ///
210    /// # Returns
211    ///
212    /// A new WebSocket streams client configured for the testnet environment
213    #[must_use]
214    pub fn testnet(
215        mut config: ConfigurationWebsocketStreams,
216    ) -> websocket_streams::WebsocketStreamsHandle {
217        config.ws_url = Some(SPOT_WS_STREAMS_TESTNET_URL.to_string());
218        SpotWsStreams::from_config(config)
219    }
220
221    /// Creates a WebSocket streams client configured for the demo environment.
222    ///
223    /// # Arguments
224    ///
225    /// * `config` - Configuration for the WebSocket streams client
226    ///
227    /// # Returns
228    ///
229    /// A new WebSocket streams client configured for the demo environment
230    #[must_use]
231    pub fn demo(
232        mut config: ConfigurationWebsocketStreams,
233    ) -> websocket_streams::WebsocketStreamsHandle {
234        config.ws_url = Some(SPOT_WS_STREAMS_DEMO_URL.to_string());
235        SpotWsStreams::from_config(config)
236    }
237}