Skip to main content

binance_sdk/margin_trading/websocket_streams/
handle.rs

1/*
2 * Binance Margin Trading WebSocket Market Streams
3 *
4 * OpenAPI Specification for the Binance Margin Trading WebSocket Market Streams
5 *
6 * The version of the OpenAPI document: 1.0.0
7 *
8 *
9 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
10 * https://openapi-generator.tech
11 * Do not edit the class manually.
12 */
13
14use crate::{common::config::ConfigurationWebsocketStreams, models::WebsocketStreamsConnectConfig};
15
16use super::WebsocketStreams;
17
18#[derive(Clone)]
19pub struct WebsocketStreamsHandle {
20    configuration: ConfigurationWebsocketStreams,
21}
22
23impl WebsocketStreamsHandle {
24    #[must_use]
25    pub fn new(configuration: ConfigurationWebsocketStreams) -> Self {
26        Self { configuration }
27    }
28
29    /// Connects to a WebSocket stream using default configuration.
30    ///
31    /// # Returns
32    ///
33    /// A `Result` containing a `WebsocketStreams` instance if the connection is successful,
34    /// or an error if the connection fails.
35    ///
36    /// # Errors
37    ///
38    /// Returns an [`anyhow::Error`] if the connection fails.
39    ///
40    /// # Examples
41    ///
42    ///
43    /// let handle = `WebsocketStreamsHandle::new(configuration)`;
44    /// let streams = handle.connect().await?;
45    ///
46    pub async fn connect(&self) -> anyhow::Result<WebsocketStreams> {
47        self.connect_with_config(Default::default()).await
48    }
49
50    /// Connects to a WebSocket stream with a custom configuration.
51    ///
52    /// # Arguments
53    ///
54    /// * `cfg` - A configuration object specifying connection details for the WebSocket stream.
55    ///
56    /// # Returns
57    ///
58    /// A `Result` containing a `WebsocketStreams` instance if the connection is successful,
59    /// or an error if the connection fails.
60    ///
61    /// # Errors
62    ///
63    /// Returns an [`anyhow::Error`] if the connection fails.
64    ///
65    /// # Examples
66    ///
67    ///
68    /// let handle = `WebsocketStreamsHandle::new(configuration)`;
69    /// let `custom_config` = `WebsocketStreamsConnectConfig::default()`;
70    /// let streams = `handle.connect_with_config(custom_config).await`?;
71    ///
72    pub async fn connect_with_config(
73        &self,
74        cfg: WebsocketStreamsConnectConfig,
75    ) -> anyhow::Result<WebsocketStreams> {
76        WebsocketStreams::connect(self.configuration.clone(), cfg.streams, cfg.mode).await
77    }
78}