exc_binance/websocket/request/
mod.rs

1use std::time::Duration;
2
3use super::protocol::{
4    frame::{Name, RequestFrame},
5    stream::MultiplexRequest,
6};
7use async_stream::stream;
8
9pub(crate) enum RequestKind {
10    DispatchSubscribe(Name),
11    DispatchTrades(exc_core::types::SubscribeTrades),
12    DispatchBidAsk(exc_core::types::SubscribeBidAsk),
13    Multiplex(MultiplexRequest),
14    Reconnect,
15}
16
17impl RequestKind {
18    fn timeout(self, duration: Duration) -> Self {
19        match self {
20            Self::Multiplex(req) => Self::Multiplex(req.timeout(duration)),
21            Self::Reconnect => Self::Reconnect,
22            Self::DispatchTrades(req) => Self::DispatchTrades(req),
23            Self::DispatchBidAsk(req) => Self::DispatchBidAsk(req),
24            Self::DispatchSubscribe(req) => Self::DispatchSubscribe(req),
25        }
26    }
27}
28
29/// Binance websocket request.
30pub struct WsRequest {
31    pub(crate) stream: bool,
32    pub(crate) inner: RequestKind,
33}
34
35impl WsRequest {
36    /// Subscribe to a stream. No matter whether the stream is main or sub.
37    pub fn subscribe_stream(name: Name) -> Self {
38        Self {
39            stream: true,
40            inner: RequestKind::DispatchSubscribe(name),
41        }
42    }
43
44    /// Subscribe to a sub stream.
45    #[deprecated(note = "Use `subscribe_stream` instead")]
46    pub fn subscribe(stream: Name) -> Self {
47        Self::sub_stream(stream)
48    }
49
50    /// Subscribe to a sub stream.
51    pub fn sub_stream(stream: Name) -> Self {
52        Self {
53            stream: true,
54            inner: RequestKind::Multiplex(MultiplexRequest::new(|token| {
55                stream! {
56                    yield RequestFrame::subscribe(0, stream.clone());
57                    let _ = token.await;
58                    yield RequestFrame::unsubscribe(0, stream);
59                }
60            })),
61        }
62    }
63
64    /// Set stream timeout. Default to the `default_stream_timeout` in protocol config.
65    pub fn timeout(mut self, duration: Duration) -> Self {
66        self.inner = self.inner.timeout(duration);
67        self
68    }
69
70    /// Subscribe to a main stream topic.
71    pub fn main_stream(stream: Name) -> Self {
72        Self {
73            stream: true,
74            inner: RequestKind::Multiplex(MultiplexRequest::main_stream(stream)),
75        }
76    }
77
78    /// Reconnect.
79    pub fn reconnect() -> Self {
80        Self {
81            stream: false,
82            inner: RequestKind::Reconnect,
83        }
84    }
85
86    /// Dispatch trades.
87    pub fn dispatch_trades(trades: exc_core::types::SubscribeTrades) -> Self {
88        Self {
89            stream: true,
90            inner: RequestKind::DispatchTrades(trades),
91        }
92    }
93
94    /// Dispatch bid ask.
95    pub fn dispatch_bid_ask(bid_ask: exc_core::types::SubscribeBidAsk) -> Self {
96        Self {
97            stream: true,
98            inner: RequestKind::DispatchBidAsk(bid_ask),
99        }
100    }
101}
102
103// impl From<WsRequest> for MultiplexRequest {
104//     fn from(req: WsRequest) -> Self {
105//         req.inner
106//     }
107// }