Skip to main content

binance_sdk/spot/websocket_api/
handle.rs

1/*
2 * Binance Spot WebSocket API
3 *
4 * OpenAPI Specifications for the Binance Spot WebSocket API
5 *
6 * API documents:
7 * - [Github web-socket-api documentation file](https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-api.md)
8 * - [General API information for web-socket-api on website](https://developers.binance.com/docs/binance-spot-api-docs/web-socket-api/general-api-information)
9 *
10 *
11 * The version of the OpenAPI document: 1.0.0
12 *
13 *
14 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
15 * https://openapi-generator.tech
16 * Do not edit the class manually.
17 */
18
19use crate::{common::config::ConfigurationWebsocketApi, models::WebsocketApiConnectConfig};
20
21use super::WebsocketApi;
22
23#[derive(Clone)]
24pub struct WebsocketApiHandle {
25    configuration: ConfigurationWebsocketApi,
26}
27
28impl WebsocketApiHandle {
29    pub fn new(configuration: ConfigurationWebsocketApi) -> Self {
30        Self { configuration }
31    }
32
33    /// Connects to the WebSocket API using default configuration.
34    ///
35    /// # Returns
36    ///
37    /// A `Result` containing the connected `WebsocketApi` instance if successful,
38    /// or an error if the connection fails.
39    ///
40    /// # Errors
41    ///
42    /// Returns an [`anyhow::Error`] if the connection fails.
43    ///
44    pub async fn connect(&self) -> anyhow::Result<WebsocketApi> {
45        self.connect_with_config(Default::default()).await
46    }
47
48    /// Connects to the WebSocket API with a custom configuration.
49    ///
50    /// # Arguments
51    ///
52    /// * `cfg` - A configuration object specifying connection parameters.
53    ///
54    /// # Returns
55    ///
56    /// A `Result` containing the connected `WebsocketApi` instance if successful,
57    /// or an error if the connection fails.
58    ///
59    /// # Errors
60    ///
61    /// Returns an [`anyhow::Error`] if the connection fails.
62    ///
63    pub async fn connect_with_config(
64        &self,
65        cfg: WebsocketApiConnectConfig,
66    ) -> anyhow::Result<WebsocketApi> {
67        WebsocketApi::connect(self.configuration.clone(), cfg.mode).await
68    }
69}