Skip to content

Commit cf29ed1

Browse files
committed
feat: binance account/balance
1 parent e9cef4d commit cf29ed1

File tree

8 files changed

+403
-78
lines changed

8 files changed

+403
-78
lines changed

Cargo.lock

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v_exchanges/src/main.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ async fn main() {
99
v_utils::utils::init_subscriber(v_utils::utils::LogDestination::xdg("v_exchanges"));
1010

1111
let mut client = Client::new();
12+
1213
//client.update_default_option(BinanceOption::HttpUrl(BinanceHttpUrl::FuturesUsdM));
13-
let klines = client.klines(("BTC", "USDT").into(), "1m".into(), None, None, None).await.unwrap();
14-
//let price = client.price(("BTC", "USDT").into()).await.unwrap();
15-
dbg!(&klines);
14+
//let klines = client.futures_klines(("BTC", "USDT").into(), "1m".into(), None, None, None).await.unwrap();
15+
//let price = client.futures_price(("BTC", "USDT").into()).await.unwrap();
16+
//dbg!(&klines);
1617

17-
//let key = env::var("BINANCE_TIGER_READ_KEY").unwrap();
18-
//let secret = env::var("BINANCE_TIGER_READ_SECRET").unwrap();
19-
//client.update_default_option(BinanceOption::Key(key));
20-
//client.update_default_option(BinanceOption::Secret(secret));
21-
//let balance = client.futures_balance("USDT").await.unwrap();
22-
//
23-
//dbg!(&balance);
18+
let key = env::var("BINANCE_TIGER_READ_KEY").unwrap();
19+
let secret = env::var("BINANCE_TIGER_READ_SECRET").unwrap();
20+
client.update_default_option(BinanceOption::Key(key));
21+
client.update_default_option(BinanceOption::Secret(secret));
22+
let balance = client.futures_asset_balance("USDT".into()).await.unwrap();
23+
dbg!(&balance);
2424
}

v_exchanges_adapters/src/lib.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ impl Client {
7171
where
7272
O: HttpOption<'a, R, B>,
7373
O::RequestHandler: RequestHandler<B>,
74-
B: std::fmt::Debug,
7574
Self: GetOptions<O::Options>,
7675
Q: Serialize + ?Sized, {
7776
self.client.request(method, url, query, body, &O::request_handler(self.merged_options(options))).await
@@ -88,40 +87,77 @@ impl Client {
8887
self.client.get(url, query, &O::request_handler(self.merged_options(options))).await
8988
}
9089

90+
/// see [http::Client::get_no_query()]
91+
#[inline(always)]
92+
pub async fn get_no_query<'a, R, O>(&self, url: &str, options: impl IntoIterator<Item = O>) -> request_return_type!('a, R, O, ())
93+
where
94+
O: HttpOption<'a, R, ()>,
95+
O::RequestHandler: RequestHandler<()>,
96+
Self: GetOptions<O::Options>, {
97+
self.client.get_no_query(url, &O::request_handler(self.merged_options(options))).await
98+
}
99+
91100
/// see [http::Client::post()]
92101
#[inline(always)]
93102
pub async fn post<'a, R, O, B>(&self, url: &str, body: Option<B>, options: impl IntoIterator<Item = O>) -> request_return_type!('a, R, O, B)
94103
where
95104
O: HttpOption<'a, R, B>,
96105
O::RequestHandler: RequestHandler<B>,
97-
B: std::fmt::Debug,
98106
Self: GetOptions<O::Options>, {
99107
self.client.post(url, body, &O::request_handler(self.merged_options(options))).await
100108
}
101109

110+
/// see [http::Client::post_no_body()]
111+
#[inline(always)]
112+
pub async fn post_no_body<'a, R, O>(&self, url: &str, options: impl IntoIterator<Item = O>) -> request_return_type!('a, R, O, ())
113+
where
114+
O: HttpOption<'a, R, ()>,
115+
O::RequestHandler: RequestHandler<()>,
116+
Self: GetOptions<O::Options>, {
117+
self.client.post_no_body(url, &O::request_handler(self.merged_options(options))).await
118+
}
119+
102120
/// see [http::Client::put()]
103121
#[inline(always)]
104122
pub async fn put<'a, R, O, B>(&self, url: &str, body: Option<B>, options: impl IntoIterator<Item = O>) -> request_return_type!('a, R, O, B)
105123
where
106124
O: HttpOption<'a, R, B>,
107125
O::RequestHandler: RequestHandler<B>,
108-
B: std::fmt::Debug,
109126
Self: GetOptions<O::Options>, {
110127
self.client.put(url, body, &O::request_handler(self.merged_options(options))).await
111128
}
112129

130+
/// see [http::Client::put_no_body()]
131+
#[inline(always)]
132+
pub async fn put_no_body<'a, R, O>(&self, url: &str, options: impl IntoIterator<Item = O>) -> request_return_type!('a, R, O, ())
133+
where
134+
O: HttpOption<'a, R, ()>,
135+
O::RequestHandler: RequestHandler<()>,
136+
Self: GetOptions<O::Options>, {
137+
self.client.put_no_body(url, &O::request_handler(self.merged_options(options))).await
138+
}
139+
113140
/// see [http::Client::delete()]
114141
#[inline(always)]
115142
pub async fn delete<'a, R, O, Q>(&self, url: &str, query: Option<&Q>, options: impl IntoIterator<Item = O>) -> request_return_type!('a, R, O, ())
116143
where
117144
O: HttpOption<'a, R, ()>,
118-
R: std::fmt::Debug,
119145
O::RequestHandler: RequestHandler<()>,
120146
Self: GetOptions<O::Options>,
121147
Q: Serialize + ?Sized, {
122148
self.client.delete(url, query, &O::request_handler(self.merged_options(options))).await
123149
}
124150

151+
/// see [http::Client::delete_no_query()]
152+
#[inline(always)]
153+
pub async fn delete_no_query<'a, R, O>(&self, url: &str, options: impl IntoIterator<Item = O>) -> request_return_type!('a, R, O, ())
154+
where
155+
O: HttpOption<'a, R, ()>,
156+
O::RequestHandler: RequestHandler<()>,
157+
Self: GetOptions<O::Options>, {
158+
self.client.delete_no_query(url, &O::request_handler(self.merged_options(options))).await
159+
}
160+
125161
#[inline(always)]
126162
pub async fn websocket<O, H>(&self, url: &str, handler: H, options: impl IntoIterator<Item = O>) -> Result<WebSocketConnection<O::WebSocketHandler>, TungsteniteError>
127163
where

v_exchanges_api_generics/src/http.rs

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub static USER_AGENT: &str = concat!("v_exchanges_api_generics/", env!("CARGO_P
1616
///
1717
/// When making a HTTP request or starting a websocket connection with this client,
1818
/// a handler that implements [RequestHandler] is required.
19-
#[derive(Debug, Clone)]
19+
#[derive(Debug, Clone, Default)]
2020
pub struct Client {
2121
client: reqwest::Client,
2222
}
@@ -88,6 +88,19 @@ impl Client {
8888
self.request::<Q, (), H>(Method::GET, url, query, None, handler).await
8989
}
9090

91+
/// Makes an GET request with the given [RequestHandler], without queries.
92+
///
93+
/// This method just calls [request()][Self::request()]. It requires less typing for type parameters and parameters.
94+
/// This method requires that `handler` can handle a request with a body of type `()`. The actual body passed will be `None`.
95+
///
96+
/// For more information, see [request()][Self::request()].
97+
#[inline(always)]
98+
pub async fn get_no_query<H>(&self, url: &str, handler: &H) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>
99+
where
100+
H: RequestHandler<()>, {
101+
self.request::<&[(&str, &str)], (), H>(Method::GET, url, None, None, handler).await
102+
}
103+
91104
/// Makes an POST request with the given [RequestHandler].
92105
///
93106
/// This method just calls [request()][Self::request()]. It requires less typing for type parameters and parameters.
@@ -100,6 +113,19 @@ impl Client {
100113
self.request::<(), B, H>(Method::POST, url, None, body, handler).await
101114
}
102115

116+
/// Makes an POST request with the given [RequestHandler], without a body.
117+
///
118+
/// This method just calls [request()][Self::request()]. It requires less typing for type parameters and parameters.
119+
/// This method requires that `handler` can handle a request with a body of type `()`. The actual body passed will be `None`.
120+
///
121+
/// For more information, see [request()][Self::request()].
122+
#[inline(always)]
123+
pub async fn post_no_body<H>(&self, url: &str, handler: &H) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>
124+
where
125+
H: RequestHandler<()>, {
126+
self.request::<(), (), H>(Method::POST, url, None, None, handler).await
127+
}
128+
103129
/// Makes an PUT request with the given [RequestHandler].
104130
///
105131
/// This method just calls [request()][Self::request()]. It requires less typing for type parameters and parameters.
@@ -112,6 +138,19 @@ impl Client {
112138
self.request::<(), B, H>(Method::PUT, url, None, body, handler).await
113139
}
114140

141+
/// Makes an PUT request with the given [RequestHandler], without a body.
142+
///
143+
/// This method just calls [request()][Self::request()]. It requires less typing for type parameters and parameters.
144+
/// This method requires that `handler` can handle a request with a body of type `()`. The actual body passed will be `None`.
145+
///
146+
/// For more information, see [request()][Self::request()].
147+
#[inline(always)]
148+
pub async fn put_no_body<H>(&self, url: &str, handler: &H) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>
149+
where
150+
H: RequestHandler<()>, {
151+
self.request::<(), (), H>(Method::PUT, url, None, None, handler).await
152+
}
153+
115154
/// Makes an DELETE request with the given [RequestHandler].
116155
///
117156
/// This method just calls [request()][Self::request()]. It requires less typing for type parameters and parameters.
@@ -125,12 +164,18 @@ impl Client {
125164
H: RequestHandler<()>, {
126165
self.request::<Q, (), H>(Method::DELETE, url, query, None, handler).await
127166
}
128-
}
129167

130-
impl Default for Client {
131-
fn default() -> Self {
132-
let client = reqwest::ClientBuilder::new().user_agent(USER_AGENT).build().unwrap(); // user agent should be valid
133-
Self { client }
168+
/// Makes an DELETE request with the given [RequestHandler], without queries.
169+
///
170+
/// This method just calls [request()][Self::request()]. It requires less typing for type parameters and parameters.
171+
/// This method requires that `handler` can handle a request with a body of type `()`. The actual body passed will be `None`.
172+
///
173+
/// For more information, see [request()][Self::request()].
174+
#[inline(always)]
175+
pub async fn delete_no_query<H>(&self, url: &str, handler: &H) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>
176+
where
177+
H: RequestHandler<()>, {
178+
self.request::<&[(&str, &str)], (), H>(Method::DELETE, url, None, None, handler).await
134179
}
135180
}
136181

0 commit comments

Comments
 (0)