1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/// Binance websocket API errors.
pub mod error;

/// Binance websocket protocol.
pub mod protocol;

/// Binance websocket request.
pub mod request;

/// Binance websocket resposne.
pub mod response;

/// Binance websocket endpoint.
pub mod endpoint;

pub(crate) mod connect;

use std::task::{Context, Poll};

use futures::future::BoxFuture;
use tower::{util::BoxService, Service};

use self::{
    connect::BinanceWsHost, endpoint::WsEndpoint, error::WsError, protocol::frame::Name,
    request::WsRequest, response::WsResponse,
};

/// Binance websocket api service.
pub struct BinanceWebsocketApi {
    svc: BoxService<WsRequest, WsResponse, WsError>,
}

impl BinanceWebsocketApi {
    /// Endpoint of USD-M Futures API.
    pub fn usd_margin_futures() -> WsEndpoint {
        WsEndpoint::new(
            BinanceWsHost::UsdMarginFutures,
            Name::new("markPrice").inst("bnbusdt"),
        )
    }

    /// Endpoint of Spot API.
    pub fn spot() -> WsEndpoint {
        WsEndpoint::new(BinanceWsHost::Spot, Name::new("miniTicker").inst("btcusdt"))
    }
}

impl Service<WsRequest> for BinanceWebsocketApi {
    type Response = WsResponse;
    type Error = WsError;
    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.svc.poll_ready(cx)
    }

    fn call(&mut self, req: WsRequest) -> Self::Future {
        self.svc.call(req)
    }
}