binance/
api.rs

1use crate::account::*;
2use crate::client::*;
3use crate::config::Config;
4use crate::general::*;
5use crate::market::*;
6use crate::userstream::*;
7
8pub trait Binance: Sized {
9    fn new(api_key: Option<String>, secret_key: Option<String>) -> Self {
10        Self::new_with_config(api_key, secret_key, &Config::default())
11    }
12
13    /// Create a binance API using environment variables for credentials
14    /// BINANCE_API_KEY=$YOUR_API_KEY
15    /// BINANCE_API_SECRET_KEY=$YOUR_SECRET_KEY
16    fn new_with_env(config: &Config) -> Self {
17        let api_key = std::env::var("BINANCE_API_KEY").ok();
18        let secret = std::env::var("BINANCE_API_SECRET_KEY").ok();
19        Self::new_with_config(api_key, secret, config)
20    }
21
22    fn new_with_config(api_key: Option<String>, secret_key: Option<String>, config: &Config) -> Self;
23}
24
25impl Binance for General {
26    fn new_with_config(api_key: Option<String>, secret_key: Option<String>, config: &Config) -> General {
27        General {
28            client: Client::new(api_key, secret_key, config.rest_api_endpoint.clone(), config.timeout),
29        }
30    }
31}
32
33impl Binance for Account {
34    fn new_with_config(api_key: Option<String>, secret_key: Option<String>, config: &Config) -> Account {
35        Account {
36            client: Client::new(api_key, secret_key, config.rest_api_endpoint.clone(), config.timeout),
37            recv_window: config.recv_window,
38        }
39    }
40}
41
42#[cfg(feature = "savings_api")]
43impl Binance for crate::savings::Savings {
44    fn new_with_config(api_key: Option<String>, secret_key: Option<String>, config: &Config) -> Self {
45        Self {
46            client: Client::new(api_key, secret_key, config.rest_api_endpoint.clone(), config.timeout),
47            recv_window: config.recv_window,
48        }
49    }
50}
51
52impl Binance for Market {
53    fn new_with_config(api_key: Option<String>, secret_key: Option<String>, config: &Config) -> Market {
54        Market {
55            client: Client::new(api_key, secret_key, config.rest_api_endpoint.clone(), config.timeout),
56            recv_window: config.recv_window,
57        }
58    }
59}
60
61impl Binance for UserStream {
62    fn new_with_config(api_key: Option<String>, secret_key: Option<String>, config: &Config) -> UserStream {
63        UserStream {
64            client: Client::new(api_key, secret_key, config.rest_api_endpoint.clone(), config.timeout),
65            recv_window: config.recv_window,
66        }
67    }
68}
69
70#[cfg(feature = "futures_api")]
71impl Binance for crate::futures::general::FuturesGeneral {
72    fn new_with_config(api_key: Option<String>, secret_key: Option<String>, config: &Config) -> Self {
73        Self {
74            client: Client::new(
75                api_key,
76                secret_key,
77                config.futures_rest_api_endpoint.clone(),
78                config.timeout,
79            ),
80        }
81    }
82}
83
84#[cfg(feature = "futures_api")]
85impl Binance for crate::futures::market::FuturesMarket {
86    fn new_with_config(api_key: Option<String>, secret_key: Option<String>, config: &Config) -> Self {
87        Self {
88            client: Client::new(
89                api_key,
90                secret_key,
91                config.futures_rest_api_endpoint.clone(),
92                config.timeout,
93            ),
94            recv_window: config.recv_window,
95        }
96    }
97}
98
99#[cfg(feature = "futures_api")]
100impl Binance for crate::futures::account::FuturesAccount {
101    fn new_with_config(api_key: Option<String>, secret_key: Option<String>, config: &Config) -> Self {
102        Self {
103            client: Client::new(
104                api_key,
105                secret_key,
106                config.futures_rest_api_endpoint.clone(),
107                config.timeout,
108            ),
109            recv_window: config.recv_window,
110        }
111    }
112}
113
114#[cfg(feature = "futures_api")]
115impl Binance for crate::futures::userstream::UserStream {
116    fn new_with_config(api_key: Option<String>, secret_key: Option<String>, config: &Config) -> Self {
117        Self {
118            client: Client::new(
119                api_key,
120                secret_key,
121                config.futures_rest_api_endpoint.clone(),
122                config.timeout,
123            ),
124            recv_window: config.recv_window,
125        }
126    }
127}
128
129#[cfg(feature = "margin_api")]
130impl Binance for crate::margin::Margin {
131    fn new_with_config(api_key: Option<String>, secret_key: Option<String>, config: &Config) -> Self {
132        Self {
133            client: Client::new(api_key, secret_key, config.rest_api_endpoint.clone(), config.timeout),
134            recv_window: config.recv_window,
135        }
136    }
137}
138
139#[cfg(feature = "wallet_api")]
140impl Binance for crate::wallet::Wallet {
141    fn new_with_config(api_key: Option<String>, secret_key: Option<String>, config: &Config) -> Self {
142        Self {
143            client: Client::new(api_key, secret_key, config.rest_api_endpoint.clone(), config.timeout),
144            recv_window: config.recv_window,
145            binance_us_api: config.binance_us_api,
146        }
147    }
148}