use self::{
mapper::{SubscriptionMapper, WebSocketSubMapper},
validator::SubscriptionValidator,
};
use crate::{
exchange::Connector,
subscription::{Map, SubKind, Subscription, SubscriptionMeta},
Identifier,
};
use async_trait::async_trait;
use barter_integration::{
error::SocketError,
model::instrument::Instrument,
protocol::websocket::{connect, WebSocket},
};
use futures::SinkExt;
use serde::{Deserialize, Serialize};
use tracing::{debug, info};
pub mod mapper;
pub mod validator;
#[async_trait]
pub trait Subscriber {
type SubMapper: SubscriptionMapper;
async fn subscribe<Exchange, Kind>(
subscriptions: &[Subscription<Exchange, Kind>],
) -> Result<(WebSocket, Map<Instrument>), SocketError>
where
Exchange: Connector + Send + Sync,
Kind: SubKind + Send + Sync,
Subscription<Exchange, Kind>: Identifier<Exchange::Channel> + Identifier<Exchange::Market>;
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
pub struct WebSocketSubscriber;
#[async_trait]
impl Subscriber for WebSocketSubscriber {
type SubMapper = WebSocketSubMapper;
async fn subscribe<Exchange, Kind>(
subscriptions: &[Subscription<Exchange, Kind>],
) -> Result<(WebSocket, Map<Instrument>), SocketError>
where
Exchange: Connector + Send + Sync,
Kind: SubKind + Send + Sync,
Subscription<Exchange, Kind>: Identifier<Exchange::Channel> + Identifier<Exchange::Market>,
{
let exchange = Exchange::ID;
let url = Exchange::url()?;
debug!(%exchange, %url, ?subscriptions, "subscribing to WebSocket");
let mut websocket = connect(url).await?;
debug!(%exchange, ?subscriptions, "connected to WebSocket");
let SubscriptionMeta {
instrument_map,
subscriptions,
} = Self::SubMapper::map::<Exchange, Kind>(subscriptions);
for subscription in subscriptions {
debug!(%exchange, payload = ?subscription, "sending exchange subscription");
websocket.send(subscription).await?;
}
let map =
Exchange::SubValidator::validate::<Exchange, Kind>(instrument_map, &mut websocket)
.await?;
info!(%exchange, "subscribed to WebSocket");
Ok((websocket, map))
}
}