binance_sdk/wallet/rest_api/apis/
others_api.rs1#![allow(unused_imports)]
15use async_trait::async_trait;
16use derive_builder::Builder;
17use reqwest;
18use rust_decimal::prelude::*;
19use serde::{Deserialize, Serialize};
20use serde_json::{Value, json};
21use std::collections::BTreeMap;
22
23use crate::common::{
24 config::ConfigurationRestApi,
25 models::{ParamBuildError, RestApiResponse},
26 utils::send_request,
27};
28use crate::wallet::rest_api::models;
29
30const HAS_TIME_UNIT: bool = false;
31
32#[async_trait]
33pub trait OthersApi: Send + Sync {
34 async fn get_symbols_delist_schedule_for_spot(
35 &self,
36 params: GetSymbolsDelistScheduleForSpotParams,
37 ) -> anyhow::Result<RestApiResponse<Vec<models::GetSymbolsDelistScheduleForSpotResponseInner>>>;
38 async fn system_status(&self) -> anyhow::Result<RestApiResponse<models::SystemStatusResponse>>;
39}
40
41#[derive(Debug, Clone)]
42pub struct OthersApiClient {
43 configuration: ConfigurationRestApi,
44}
45
46impl OthersApiClient {
47 pub fn new(configuration: ConfigurationRestApi) -> Self {
48 Self { configuration }
49 }
50}
51
52#[derive(Clone, Debug, Builder, Deserialize, Default)]
57#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
58pub struct GetSymbolsDelistScheduleForSpotParams {
59 #[builder(setter(into), default)]
64 #[serde(rename = "recvWindow", default)]
65 pub recv_window: Option<i64>,
66}
67
68impl GetSymbolsDelistScheduleForSpotParams {
69 #[must_use]
72 pub fn builder() -> GetSymbolsDelistScheduleForSpotParamsBuilder {
73 GetSymbolsDelistScheduleForSpotParamsBuilder::default()
74 }
75}
76
77#[async_trait]
78impl OthersApi for OthersApiClient {
79 async fn get_symbols_delist_schedule_for_spot(
80 &self,
81 params: GetSymbolsDelistScheduleForSpotParams,
82 ) -> anyhow::Result<RestApiResponse<Vec<models::GetSymbolsDelistScheduleForSpotResponseInner>>>
83 {
84 let GetSymbolsDelistScheduleForSpotParams { recv_window } = params;
85
86 let mut query_params = BTreeMap::new();
87 let body_params = BTreeMap::new();
88
89 if let Some(rw) = recv_window {
90 query_params.insert("recvWindow".to_string(), json!(rw));
91 }
92
93 send_request::<Vec<models::GetSymbolsDelistScheduleForSpotResponseInner>>(
94 &self.configuration,
95 "/sapi/v1/spot/delist-schedule",
96 reqwest::Method::GET,
97 query_params,
98 body_params,
99 if HAS_TIME_UNIT {
100 self.configuration.time_unit
101 } else {
102 None
103 },
104 false,
105 )
106 .await
107 }
108
109 async fn system_status(&self) -> anyhow::Result<RestApiResponse<models::SystemStatusResponse>> {
110 let query_params = BTreeMap::new();
111 let body_params = BTreeMap::new();
112
113 send_request::<models::SystemStatusResponse>(
114 &self.configuration,
115 "/sapi/v1/system/status",
116 reqwest::Method::GET,
117 query_params,
118 body_params,
119 if HAS_TIME_UNIT {
120 self.configuration.time_unit
121 } else {
122 None
123 },
124 false,
125 )
126 .await
127 }
128}
129
130#[cfg(all(test, feature = "wallet"))]
131mod tests {
132 use super::*;
133 use crate::TOKIO_SHARED_RT;
134 use crate::{errors::ConnectorError, models::DataFuture, models::RestApiRateLimit};
135 use async_trait::async_trait;
136 use std::collections::HashMap;
137
138 struct DummyRestApiResponse<T> {
139 inner: Box<dyn FnOnce() -> DataFuture<Result<T, ConnectorError>> + Send + Sync>,
140 status: u16,
141 headers: HashMap<String, String>,
142 rate_limits: Option<Vec<RestApiRateLimit>>,
143 }
144
145 impl<T> From<DummyRestApiResponse<T>> for RestApiResponse<T> {
146 fn from(dummy: DummyRestApiResponse<T>) -> Self {
147 Self {
148 data_fn: dummy.inner,
149 status: dummy.status,
150 headers: dummy.headers,
151 rate_limits: dummy.rate_limits,
152 }
153 }
154 }
155
156 struct MockOthersApiClient {
157 force_error: bool,
158 }
159
160 #[async_trait]
161 impl OthersApi for MockOthersApiClient {
162 async fn get_symbols_delist_schedule_for_spot(
163 &self,
164 _params: GetSymbolsDelistScheduleForSpotParams,
165 ) -> anyhow::Result<
166 RestApiResponse<Vec<models::GetSymbolsDelistScheduleForSpotResponseInner>>,
167 > {
168 if self.force_error {
169 return Err(ConnectorError::ConnectorClientError {
170 msg: "ResponseError".to_string(),
171 code: None,
172 }
173 .into());
174 }
175
176 let resp_json: Value =
177 serde_json::from_str(r#"[{"delistTime":1686161202000,"symbols":["ADAUSDT"]}]"#)
178 .unwrap_or_else(|_| serde_json::json!({}));
179 let dummy_response: Vec<models::GetSymbolsDelistScheduleForSpotResponseInner> =
180 serde_json::from_value(resp_json.clone()).expect(
181 "should parse into Vec<models::GetSymbolsDelistScheduleForSpotResponseInner>",
182 );
183
184 let dummy = DummyRestApiResponse {
185 inner: Box::new(move || Box::pin(async move { Ok(dummy_response) })),
186 status: 200,
187 headers: HashMap::new(),
188 rate_limits: None,
189 };
190
191 Ok(dummy.into())
192 }
193
194 async fn system_status(
195 &self,
196 ) -> anyhow::Result<RestApiResponse<models::SystemStatusResponse>> {
197 if self.force_error {
198 return Err(ConnectorError::ConnectorClientError {
199 msg: "ResponseError".to_string(),
200 code: None,
201 }
202 .into());
203 }
204
205 let resp_json: Value = serde_json::from_str(r#"{"status":0,"msg":"normal"}"#)
206 .unwrap_or_else(|_| serde_json::json!({}));
207 let dummy_response: models::SystemStatusResponse =
208 serde_json::from_value(resp_json.clone())
209 .expect("should parse into models::SystemStatusResponse");
210
211 let dummy = DummyRestApiResponse {
212 inner: Box::new(move || Box::pin(async move { Ok(dummy_response) })),
213 status: 200,
214 headers: HashMap::new(),
215 rate_limits: None,
216 };
217
218 Ok(dummy.into())
219 }
220 }
221
222 #[test]
223 fn get_symbols_delist_schedule_for_spot_required_params_success() {
224 TOKIO_SHARED_RT.block_on(async {
225 let client = MockOthersApiClient { force_error: false };
226
227 let params = GetSymbolsDelistScheduleForSpotParams::builder()
228 .build()
229 .unwrap();
230
231 let resp_json: Value =
232 serde_json::from_str(r#"[{"delistTime":1686161202000,"symbols":["ADAUSDT"]}]"#)
233 .unwrap_or_else(|_| serde_json::json!({}));
234 let expected_response: Vec<models::GetSymbolsDelistScheduleForSpotResponseInner> =
235 serde_json::from_value(resp_json.clone()).expect(
236 "should parse into Vec<models::GetSymbolsDelistScheduleForSpotResponseInner>",
237 );
238
239 let resp = client
240 .get_symbols_delist_schedule_for_spot(params)
241 .await
242 .expect("Expected a response");
243 let data_future = resp.data();
244 let actual_response = data_future.await.unwrap();
245 assert_eq!(actual_response, expected_response);
246 });
247 }
248
249 #[test]
250 fn get_symbols_delist_schedule_for_spot_optional_params_success() {
251 TOKIO_SHARED_RT.block_on(async {
252 let client = MockOthersApiClient { force_error: false };
253
254 let params = GetSymbolsDelistScheduleForSpotParams::builder()
255 .recv_window(5000)
256 .build()
257 .unwrap();
258
259 let resp_json: Value =
260 serde_json::from_str(r#"[{"delistTime":1686161202000,"symbols":["ADAUSDT"]}]"#)
261 .unwrap_or_else(|_| serde_json::json!({}));
262 let expected_response: Vec<models::GetSymbolsDelistScheduleForSpotResponseInner> =
263 serde_json::from_value(resp_json.clone()).expect(
264 "should parse into Vec<models::GetSymbolsDelistScheduleForSpotResponseInner>",
265 );
266
267 let resp = client
268 .get_symbols_delist_schedule_for_spot(params)
269 .await
270 .expect("Expected a response");
271 let data_future = resp.data();
272 let actual_response = data_future.await.unwrap();
273 assert_eq!(actual_response, expected_response);
274 });
275 }
276
277 #[test]
278 fn get_symbols_delist_schedule_for_spot_response_error() {
279 TOKIO_SHARED_RT.block_on(async {
280 let client = MockOthersApiClient { force_error: true };
281
282 let params = GetSymbolsDelistScheduleForSpotParams::builder()
283 .build()
284 .unwrap();
285
286 match client.get_symbols_delist_schedule_for_spot(params).await {
287 Ok(_) => panic!("Expected an error"),
288 Err(err) => {
289 assert_eq!(err.to_string(), "Connector client error: ResponseError");
290 }
291 }
292 });
293 }
294
295 #[test]
296 fn system_status_required_params_success() {
297 TOKIO_SHARED_RT.block_on(async {
298 let client = MockOthersApiClient { force_error: false };
299
300 let resp_json: Value = serde_json::from_str(r#"{"status":0,"msg":"normal"}"#)
301 .unwrap_or_else(|_| serde_json::json!({}));
302 let expected_response: models::SystemStatusResponse =
303 serde_json::from_value(resp_json.clone())
304 .expect("should parse into models::SystemStatusResponse");
305
306 let resp = client.system_status().await.expect("Expected a response");
307 let data_future = resp.data();
308 let actual_response = data_future.await.unwrap();
309 assert_eq!(actual_response, expected_response);
310 });
311 }
312
313 #[test]
314 fn system_status_optional_params_success() {
315 TOKIO_SHARED_RT.block_on(async {
316 let client = MockOthersApiClient { force_error: false };
317
318 let resp_json: Value = serde_json::from_str(r#"{"status":0,"msg":"normal"}"#)
319 .unwrap_or_else(|_| serde_json::json!({}));
320 let expected_response: models::SystemStatusResponse =
321 serde_json::from_value(resp_json.clone())
322 .expect("should parse into models::SystemStatusResponse");
323
324 let resp = client.system_status().await.expect("Expected a response");
325 let data_future = resp.data();
326 let actual_response = data_future.await.unwrap();
327 assert_eq!(actual_response, expected_response);
328 });
329 }
330
331 #[test]
332 fn system_status_response_error() {
333 TOKIO_SHARED_RT.block_on(async {
334 let client = MockOthersApiClient { force_error: true };
335
336 match client.system_status().await {
337 Ok(_) => panic!("Expected an error"),
338 Err(err) => {
339 assert_eq!(err.to_string(), "Connector client error: ResponseError");
340 }
341 }
342 });
343 }
344}