use std::time::Duration;
use crate::constants::{MAINNET, MAINNET_WS_PRIVATE, MAINNET_WS_PUBLIC_LINEAR};
#[derive(Debug, Clone)]
pub struct ClientConfig {
pub api_key: String,
pub api_secret: String,
pub base_url: String,
pub timeout: Duration,
pub recv_window: u64,
pub debug: bool,
}
impl ClientConfig {
pub fn builder(
api_key: impl Into<String>,
api_secret: impl Into<String>,
) -> ClientConfigBuilder {
ClientConfigBuilder::new(api_key, api_secret)
}
}
impl Default for ClientConfig {
fn default() -> Self {
Self {
api_key: String::new(),
api_secret: String::new(),
base_url: MAINNET.to_string(),
timeout: Duration::from_secs(30),
recv_window: 5000,
debug: false,
}
}
}
#[derive(Debug, Clone)]
pub struct ClientConfigBuilder {
config: ClientConfig,
}
impl ClientConfigBuilder {
pub fn new(api_key: impl Into<String>, api_secret: impl Into<String>) -> Self {
Self {
config: ClientConfig {
api_key: api_key.into(),
api_secret: api_secret.into(),
..Default::default()
},
}
}
pub fn base_url(mut self, url: impl Into<String>) -> Self {
self.config.base_url = url.into();
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.config.timeout = timeout;
self
}
pub fn recv_window(mut self, recv_window: u64) -> Self {
self.config.recv_window = recv_window;
self
}
pub fn debug(mut self, debug: bool) -> Self {
self.config.debug = debug;
self
}
pub fn build(self) -> ClientConfig {
self.config
}
}
#[derive(Debug, Clone)]
pub struct WsConfig {
pub api_key: Option<String>,
pub api_secret: Option<String>,
pub url: String,
pub ping_interval: u64,
pub max_reconnect_attempts: u32,
pub reconnect_delay: u64,
}
impl Default for WsConfig {
fn default() -> Self {
Self {
api_key: None,
api_secret: None,
url: MAINNET_WS_PUBLIC_LINEAR.to_string(),
ping_interval: 20,
max_reconnect_attempts: 10,
reconnect_delay: 5,
}
}
}
impl WsConfig {
pub fn public(url: impl Into<String>) -> Self {
Self {
url: url.into(),
..Default::default()
}
}
pub fn private(api_key: impl Into<String>, api_secret: impl Into<String>) -> Self {
Self {
api_key: Some(api_key.into()),
api_secret: Some(api_secret.into()),
url: MAINNET_WS_PRIVATE.to_string(),
..Default::default()
}
}
pub fn with_url(mut self, url: impl Into<String>) -> Self {
self.url = url.into();
self
}
pub fn with_ping_interval(mut self, interval: u64) -> Self {
self.ping_interval = interval;
self
}
pub fn with_max_reconnect_attempts(mut self, attempts: u32) -> Self {
self.max_reconnect_attempts = attempts;
self
}
}