diff --git a/bothan-binance/examples/dummy_example.rs b/bothan-binance/examples/dummy_example.rs index 7cb308c1..cca233b1 100644 --- a/bothan-binance/examples/dummy_example.rs +++ b/bothan-binance/examples/dummy_example.rs @@ -1,13 +1,13 @@ use tracing_subscriber::fmt::init; -use bothan_binance::service::BinanceServiceBuilder; +use bothan_binance::BinanceServiceBuilder; use bothan_core::service::Service; #[tokio::main] async fn main() { init(); - let service = BinanceServiceBuilder::new().build().await; + let service = BinanceServiceBuilder::default().build().await; if let Ok(mut service) = service { loop { let data = service.get_price_data(&["btcusdt", "ethusdt"]).await; diff --git a/bothan-binance/src/builder.rs b/bothan-binance/src/builder.rs index 8b137891..2d10fa14 100644 --- a/bothan-binance/src/builder.rs +++ b/bothan-binance/src/builder.rs @@ -1 +1,49 @@ +use std::sync::Arc; +use tokio::sync::Mutex; + +use crate::api::types::DEFAULT_URL; +use crate::error::Error; +use crate::types::DEFAULT_CHANNEL_SIZE; +use crate::{BinanceService, BinanceWebSocketConnector}; + +pub struct BinanceServiceBuilder { + url: String, + cmd_ch_size: usize, + rem_id_ch_size: usize, +} + +impl BinanceServiceBuilder { + pub fn with_cmd_ch_size(mut self, size: usize) -> Self { + self.cmd_ch_size = size; + self + } + + pub fn with_rem_id_ch_size(mut self, size: usize) -> Self { + self.rem_id_ch_size = size; + self + } + + pub async fn build(self) -> Result { + let connector = BinanceWebSocketConnector::new(self.url); + let connection = connector.connect().await?; + let service = BinanceService::new( + Arc::new(connector), + Arc::new(Mutex::new(connection)), + self.cmd_ch_size, + self.rem_id_ch_size, + ) + .await; + Ok(service) + } +} + +impl Default for BinanceServiceBuilder { + fn default() -> Self { + Self { + url: DEFAULT_URL.to_string(), + cmd_ch_size: DEFAULT_CHANNEL_SIZE, + rem_id_ch_size: DEFAULT_CHANNEL_SIZE, + } + } +} diff --git a/bothan-binance/src/lib.rs b/bothan-binance/src/lib.rs index 0bb40d51..e50b83d0 100644 --- a/bothan-binance/src/lib.rs +++ b/bothan-binance/src/lib.rs @@ -1,4 +1,5 @@ pub use api::websocket::{BinanceWebSocketConnection, BinanceWebSocketConnector}; +pub use builder::BinanceServiceBuilder; pub use service::BinanceService; pub mod api; diff --git a/bothan-binance/src/service.rs b/bothan-binance/src/service.rs index dcac2bb4..a5d96ebf 100644 --- a/bothan-binance/src/service.rs +++ b/bothan-binance/src/service.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use tokio::select; use tokio::sync::mpsc::{channel, Receiver, Sender}; -use tokio::sync::{Mutex, MutexGuard}; +use tokio::sync::Mutex; use tokio::time::timeout; use tracing::{debug, error, info, warn}; @@ -11,49 +11,9 @@ use bothan_core::service::{Error as ServiceError, Service, ServiceResult}; use bothan_core::types::PriceData; use crate::api::error::Error as BinanceError; -use crate::api::types::{BinanceResponse, Data, DEFAULT_URL}; +use crate::api::types::{BinanceResponse, Data}; use crate::api::websocket::{BinanceWebSocketConnection, BinanceWebSocketConnector}; -use crate::error::Error; -use crate::types::{Command, DEFAULT_CHANNEL_SIZE, DEFAULT_TIMEOUT}; - -pub struct BinanceServiceBuilder { - url: String, - cmd_ch_size: usize, - rem_id_ch_size: usize, -} - -impl BinanceServiceBuilder { - pub fn new() -> Self { - Self { - url: DEFAULT_URL.to_string(), - cmd_ch_size: DEFAULT_CHANNEL_SIZE, - rem_id_ch_size: DEFAULT_CHANNEL_SIZE, - } - } - - pub fn with_cmd_ch_size(mut self, size: usize) -> Self { - self.cmd_ch_size = size; - self - } - - pub fn with_rem_id_ch_size(mut self, size: usize) -> Self { - self.rem_id_ch_size = size; - self - } - - pub async fn build(self) -> Result { - let connector = BinanceWebSocketConnector::new(self.url); - let connection = connector.connect().await?; - let service = BinanceService::new( - Arc::new(connector), - Arc::new(Mutex::new(connection)), - self.cmd_ch_size, - self.rem_id_ch_size, - ) - .await; - Ok(service) - } -} +use crate::types::{Command, DEFAULT_TIMEOUT}; pub struct BinanceService { cache: Arc>, @@ -61,7 +21,7 @@ pub struct BinanceService { } impl BinanceService { - async fn new( + pub(crate) async fn new( connector: Arc, connection: Arc>, cmd_ch_size: usize,