use std::pin::Pin;
use std::sync::Arc;
use async_trait::async_trait;
use futures_util::Stream;
use tokio::sync::Mutex as TokioMutex;
use crate::core::types::{
AccountType, ConnectionStatus, OrderbookCapabilities, StreamEvent, StreamType,
SubscriptionRequest, Symbol, WebSocketResult,
};
#[async_trait]
pub trait WebSocketConnector: Send + Sync {
async fn connect(&self, account_type: AccountType) -> WebSocketResult<()> {
let _ = account_type;
Err(crate::core::types::WebSocketError::UnsupportedOperation(
"WebSocket not supported".into(),
))
}
async fn disconnect(&self) -> WebSocketResult<()> {
Err(crate::core::types::WebSocketError::UnsupportedOperation(
"WebSocket not supported".into(),
))
}
fn connection_status(&self) -> ConnectionStatus {
ConnectionStatus::Disconnected
}
async fn subscribe(&self, request: SubscriptionRequest) -> WebSocketResult<()> {
let _ = request;
Err(crate::core::types::WebSocketError::UnsupportedOperation(
"WebSocket not supported".into(),
))
}
async fn unsubscribe(&self, request: SubscriptionRequest) -> WebSocketResult<()> {
let _ = request;
Err(crate::core::types::WebSocketError::UnsupportedOperation(
"WebSocket not supported".into(),
))
}
fn event_stream(&self) -> Pin<Box<dyn Stream<Item = WebSocketResult<StreamEvent>> + Send>> {
Box::pin(futures_util::stream::empty())
}
fn active_subscriptions(&self) -> Vec<SubscriptionRequest> {
Vec::new()
}
fn has_subscription(&self, request: &SubscriptionRequest) -> bool {
self.active_subscriptions().contains(request)
}
fn ping_rtt_handle(&self) -> Option<Arc<TokioMutex<u64>>> {
None
}
fn orderbook_capabilities(&self, account_type: AccountType) -> OrderbookCapabilities {
let _ = account_type;
OrderbookCapabilities::permissive()
}
}
#[async_trait]
pub trait WebSocketExt: WebSocketConnector {
async fn subscribe_ticker(&self, symbol: String) -> WebSocketResult<()> {
let sym = Symbol::with_raw("", "", symbol);
self.subscribe(SubscriptionRequest::ticker(sym)).await
}
async fn subscribe_trades(&self, symbol: String) -> WebSocketResult<()> {
let sym = Symbol::with_raw("", "", symbol);
self.subscribe(SubscriptionRequest::trade(sym)).await
}
async fn subscribe_orderbook(&self, symbol: String) -> WebSocketResult<()> {
let sym = Symbol::with_raw("", "", symbol);
self.subscribe(SubscriptionRequest::orderbook(sym)).await
}
async fn subscribe_klines(&self, symbol: String, interval: &str) -> WebSocketResult<()> {
let sym = Symbol::with_raw("", "", symbol);
self.subscribe(SubscriptionRequest::kline(sym, interval)).await
}
async fn subscribe_orders(&self) -> WebSocketResult<()> {
self.subscribe(SubscriptionRequest::new(
Symbol::with_raw("", "", String::new()),
StreamType::OrderUpdate,
))
.await
}
async fn subscribe_balance(&self) -> WebSocketResult<()> {
self.subscribe(SubscriptionRequest::new(
Symbol::with_raw("", "", String::new()),
StreamType::BalanceUpdate,
))
.await
}
async fn subscribe_positions(&self) -> WebSocketResult<()> {
self.subscribe(SubscriptionRequest::new(
Symbol::with_raw("", "", String::new()),
StreamType::PositionUpdate,
))
.await
}
}
impl<T: WebSocketConnector> WebSocketExt for T {}