binance/margin/apis/
trade_data_stream_api.rs

1/*
2 * Binance Margin Trading API
3 *
4 * OpenAPI specification for Binance exchange - Margin API
5 *
6 * The version of the OpenAPI document: 0.1.0
7 * 
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::margin::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17/// struct for passing parameters to the method [`margin_create_user_data_stream_isolated_v1`]
18#[derive(Clone, Debug, Default)]
19pub struct MarginCreateUserDataStreamIsolatedV1Params {
20    pub symbol: String
21}
22
23/// struct for passing parameters to the method [`margin_delete_user_data_stream_isolated_v1`]
24#[derive(Clone, Debug, Default)]
25pub struct MarginDeleteUserDataStreamIsolatedV1Params {
26    pub symbol: String,
27    pub listenkey: String
28}
29
30/// struct for passing parameters to the method [`margin_delete_user_data_stream_v1`]
31#[derive(Clone, Debug, Default)]
32pub struct MarginDeleteUserDataStreamV1Params {
33    pub listenkey: String
34}
35
36/// struct for passing parameters to the method [`margin_update_user_data_stream_isolated_v1`]
37#[derive(Clone, Debug, Default)]
38pub struct MarginUpdateUserDataStreamIsolatedV1Params {
39    pub listen_key: String,
40    pub symbol: String
41}
42
43/// struct for passing parameters to the method [`margin_update_user_data_stream_v1`]
44#[derive(Clone, Debug, Default)]
45pub struct MarginUpdateUserDataStreamV1Params {
46    pub listen_key: String
47}
48
49
50/// struct for typed errors of method [`margin_create_user_data_stream_isolated_v1`]
51#[derive(Debug, Clone, Serialize, Deserialize)]
52#[serde(untagged)]
53pub enum MarginCreateUserDataStreamIsolatedV1Error {
54    Status4XX(models::ApiError),
55    Status5XX(models::ApiError),
56    UnknownValue(serde_json::Value),
57}
58
59/// struct for typed errors of method [`margin_create_user_data_stream_v1`]
60#[derive(Debug, Clone, Serialize, Deserialize)]
61#[serde(untagged)]
62pub enum MarginCreateUserDataStreamV1Error {
63    Status4XX(models::ApiError),
64    Status5XX(models::ApiError),
65    UnknownValue(serde_json::Value),
66}
67
68/// struct for typed errors of method [`margin_delete_user_data_stream_isolated_v1`]
69#[derive(Debug, Clone, Serialize, Deserialize)]
70#[serde(untagged)]
71pub enum MarginDeleteUserDataStreamIsolatedV1Error {
72    Status4XX(models::ApiError),
73    Status5XX(models::ApiError),
74    UnknownValue(serde_json::Value),
75}
76
77/// struct for typed errors of method [`margin_delete_user_data_stream_v1`]
78#[derive(Debug, Clone, Serialize, Deserialize)]
79#[serde(untagged)]
80pub enum MarginDeleteUserDataStreamV1Error {
81    Status4XX(models::ApiError),
82    Status5XX(models::ApiError),
83    UnknownValue(serde_json::Value),
84}
85
86/// struct for typed errors of method [`margin_update_user_data_stream_isolated_v1`]
87#[derive(Debug, Clone, Serialize, Deserialize)]
88#[serde(untagged)]
89pub enum MarginUpdateUserDataStreamIsolatedV1Error {
90    Status4XX(models::ApiError),
91    Status5XX(models::ApiError),
92    UnknownValue(serde_json::Value),
93}
94
95/// struct for typed errors of method [`margin_update_user_data_stream_v1`]
96#[derive(Debug, Clone, Serialize, Deserialize)]
97#[serde(untagged)]
98pub enum MarginUpdateUserDataStreamV1Error {
99    Status4XX(models::ApiError),
100    Status5XX(models::ApiError),
101    UnknownValue(serde_json::Value),
102}
103
104
105/// Start a new isolated margin user data stream. The stream will close after 60 minutes unless a keepalive is sent. If the account has an active listenKey, that listenKey will be returned and its validity will be extended for 60 minutes.
106pub async fn margin_create_user_data_stream_isolated_v1(configuration: &configuration::Configuration, params: MarginCreateUserDataStreamIsolatedV1Params) -> Result<models::MarginCreateUserDataStreamIsolatedV1Resp, Error<MarginCreateUserDataStreamIsolatedV1Error>> {
107
108    let uri_str = format!("{}/sapi/v1/userDataStream/isolated", configuration.base_path);
109    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
110
111    // Create a mutable vector for query parameters
112    let mut query_params: Vec<(String, String)> = Vec::new();
113
114
115    // Create header parameters collection
116    let mut header_params = std::collections::HashMap::new();
117
118    // Handle Binance Auth first if configured
119    if let Some(ref binance_auth) = configuration.binance_auth {
120        // Add API key to headers
121        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
122        
123        // Generate request body for signing (if any)
124        let body_string: Option<Vec<u8>> = None;
125        
126        // Sign the request
127        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
128            Ok(sig) => sig,
129            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
130        };
131        
132        // Add signature to query params
133        query_params.push(("signature".to_string(), signature));
134    }
135
136    // Apply all query parameters
137    if !query_params.is_empty() {
138        req_builder = req_builder.query(&query_params);
139    }
140
141
142    // Add user agent if configured
143    if let Some(ref user_agent) = configuration.user_agent {
144        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
145    }
146
147    // Apply all header parameters
148    for (header_name, header_value) in header_params {
149        req_builder = req_builder.header(&header_name, &header_value);
150    }
151
152    let mut multipart_form_params = std::collections::HashMap::new();
153    multipart_form_params.insert("symbol", params.symbol.to_string());
154    req_builder = req_builder.form(&multipart_form_params);
155
156    let req = req_builder.build()?;
157    let resp = configuration.client.execute(req).await?;
158
159    let status = resp.status();
160    let content_type = resp
161        .headers()
162        .get("content-type")
163        .and_then(|v| v.to_str().ok())
164        .unwrap_or("application/octet-stream");
165    let content_type = super::ContentType::from(content_type);
166
167    if !status.is_client_error() && !status.is_server_error() {
168        let content = resp.text().await?;
169        match content_type {
170            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
171            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::MarginCreateUserDataStreamIsolatedV1Resp`"))),
172            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::MarginCreateUserDataStreamIsolatedV1Resp`")))),
173        }
174    } else {
175        let content = resp.text().await?;
176        let entity: Option<MarginCreateUserDataStreamIsolatedV1Error> = serde_json::from_str(&content).ok();
177        Err(Error::ResponseError(ResponseContent { status, content, entity }))
178    }
179}
180
181/// Start a new margin user data stream. The stream will close after 60 minutes unless a keepalive is sent. If the account has an active listenKey, that listenKey will be returned and its validity will be extended for 60 minutes.
182pub async fn margin_create_user_data_stream_v1(configuration: &configuration::Configuration) -> Result<models::MarginCreateUserDataStreamV1Resp, Error<MarginCreateUserDataStreamV1Error>> {
183
184    let uri_str = format!("{}/sapi/v1/userDataStream", configuration.base_path);
185    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
186
187    // Create a mutable vector for query parameters
188    let mut query_params: Vec<(String, String)> = Vec::new();
189
190
191    // Create header parameters collection
192    let mut header_params = std::collections::HashMap::new();
193
194    // Handle Binance Auth first if configured
195    if let Some(ref binance_auth) = configuration.binance_auth {
196        // Add API key to headers
197        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
198        
199        // Generate request body for signing (if any)
200        let body_string: Option<Vec<u8>> = None;
201        
202        // Sign the request
203        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
204            Ok(sig) => sig,
205            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
206        };
207        
208        // Add signature to query params
209        query_params.push(("signature".to_string(), signature));
210    }
211
212    // Apply all query parameters
213    if !query_params.is_empty() {
214        req_builder = req_builder.query(&query_params);
215    }
216
217
218    // Add user agent if configured
219    if let Some(ref user_agent) = configuration.user_agent {
220        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
221    }
222
223    // Apply all header parameters
224    for (header_name, header_value) in header_params {
225        req_builder = req_builder.header(&header_name, &header_value);
226    }
227
228
229    let req = req_builder.build()?;
230    let resp = configuration.client.execute(req).await?;
231
232    let status = resp.status();
233    let content_type = resp
234        .headers()
235        .get("content-type")
236        .and_then(|v| v.to_str().ok())
237        .unwrap_or("application/octet-stream");
238    let content_type = super::ContentType::from(content_type);
239
240    if !status.is_client_error() && !status.is_server_error() {
241        let content = resp.text().await?;
242        match content_type {
243            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
244            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::MarginCreateUserDataStreamV1Resp`"))),
245            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::MarginCreateUserDataStreamV1Resp`")))),
246        }
247    } else {
248        let content = resp.text().await?;
249        let entity: Option<MarginCreateUserDataStreamV1Error> = serde_json::from_str(&content).ok();
250        Err(Error::ResponseError(ResponseContent { status, content, entity }))
251    }
252}
253
254/// Close out a isolated margin user data stream.
255pub async fn margin_delete_user_data_stream_isolated_v1(configuration: &configuration::Configuration, params: MarginDeleteUserDataStreamIsolatedV1Params) -> Result<serde_json::Value, Error<MarginDeleteUserDataStreamIsolatedV1Error>> {
256
257    let uri_str = format!("{}/sapi/v1/userDataStream/isolated", configuration.base_path);
258    let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);
259
260    // Create a mutable vector for query parameters
261    let mut query_params: Vec<(String, String)> = Vec::new();
262
263    query_params.push(("symbol".to_string(), params.symbol.to_string()));
264    query_params.push(("listenkey".to_string(), params.listenkey.to_string()));
265
266    // Create header parameters collection
267    let mut header_params = std::collections::HashMap::new();
268
269    // Handle Binance Auth first if configured
270    if let Some(ref binance_auth) = configuration.binance_auth {
271        // Add API key to headers
272        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
273        
274        // Generate request body for signing (if any)
275        let body_string: Option<Vec<u8>> = None;
276        
277        // Sign the request
278        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
279            Ok(sig) => sig,
280            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
281        };
282        
283        // Add signature to query params
284        query_params.push(("signature".to_string(), signature));
285    }
286
287    // Apply all query parameters
288    if !query_params.is_empty() {
289        req_builder = req_builder.query(&query_params);
290    }
291
292
293    // Add user agent if configured
294    if let Some(ref user_agent) = configuration.user_agent {
295        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
296    }
297
298    // Apply all header parameters
299    for (header_name, header_value) in header_params {
300        req_builder = req_builder.header(&header_name, &header_value);
301    }
302
303
304    let req = req_builder.build()?;
305    let resp = configuration.client.execute(req).await?;
306
307    let status = resp.status();
308    let content_type = resp
309        .headers()
310        .get("content-type")
311        .and_then(|v| v.to_str().ok())
312        .unwrap_or("application/octet-stream");
313    let content_type = super::ContentType::from(content_type);
314
315    if !status.is_client_error() && !status.is_server_error() {
316        let content = resp.text().await?;
317        match content_type {
318            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
319            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `serde_json::Value`"))),
320            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `serde_json::Value`")))),
321        }
322    } else {
323        let content = resp.text().await?;
324        let entity: Option<MarginDeleteUserDataStreamIsolatedV1Error> = serde_json::from_str(&content).ok();
325        Err(Error::ResponseError(ResponseContent { status, content, entity }))
326    }
327}
328
329/// Close out a Margin user data stream.
330pub async fn margin_delete_user_data_stream_v1(configuration: &configuration::Configuration, params: MarginDeleteUserDataStreamV1Params) -> Result<serde_json::Value, Error<MarginDeleteUserDataStreamV1Error>> {
331
332    let uri_str = format!("{}/sapi/v1/userDataStream", configuration.base_path);
333    let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);
334
335    // Create a mutable vector for query parameters
336    let mut query_params: Vec<(String, String)> = Vec::new();
337
338    query_params.push(("listenkey".to_string(), params.listenkey.to_string()));
339
340    // Create header parameters collection
341    let mut header_params = std::collections::HashMap::new();
342
343    // Handle Binance Auth first if configured
344    if let Some(ref binance_auth) = configuration.binance_auth {
345        // Add API key to headers
346        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
347        
348        // Generate request body for signing (if any)
349        let body_string: Option<Vec<u8>> = None;
350        
351        // Sign the request
352        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
353            Ok(sig) => sig,
354            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
355        };
356        
357        // Add signature to query params
358        query_params.push(("signature".to_string(), signature));
359    }
360
361    // Apply all query parameters
362    if !query_params.is_empty() {
363        req_builder = req_builder.query(&query_params);
364    }
365
366
367    // Add user agent if configured
368    if let Some(ref user_agent) = configuration.user_agent {
369        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
370    }
371
372    // Apply all header parameters
373    for (header_name, header_value) in header_params {
374        req_builder = req_builder.header(&header_name, &header_value);
375    }
376
377
378    let req = req_builder.build()?;
379    let resp = configuration.client.execute(req).await?;
380
381    let status = resp.status();
382    let content_type = resp
383        .headers()
384        .get("content-type")
385        .and_then(|v| v.to_str().ok())
386        .unwrap_or("application/octet-stream");
387    let content_type = super::ContentType::from(content_type);
388
389    if !status.is_client_error() && !status.is_server_error() {
390        let content = resp.text().await?;
391        match content_type {
392            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
393            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `serde_json::Value`"))),
394            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `serde_json::Value`")))),
395        }
396    } else {
397        let content = resp.text().await?;
398        let entity: Option<MarginDeleteUserDataStreamV1Error> = serde_json::from_str(&content).ok();
399        Err(Error::ResponseError(ResponseContent { status, content, entity }))
400    }
401}
402
403/// Keepalive an isolated margin user data stream to prevent a time out.
404pub async fn margin_update_user_data_stream_isolated_v1(configuration: &configuration::Configuration, params: MarginUpdateUserDataStreamIsolatedV1Params) -> Result<serde_json::Value, Error<MarginUpdateUserDataStreamIsolatedV1Error>> {
405
406    let uri_str = format!("{}/sapi/v1/userDataStream/isolated", configuration.base_path);
407    let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
408
409    // Create a mutable vector for query parameters
410    let mut query_params: Vec<(String, String)> = Vec::new();
411
412
413    // Create header parameters collection
414    let mut header_params = std::collections::HashMap::new();
415
416    // Handle Binance Auth first if configured
417    if let Some(ref binance_auth) = configuration.binance_auth {
418        // Add API key to headers
419        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
420        
421        // Generate request body for signing (if any)
422        let body_string: Option<Vec<u8>> = None;
423        
424        // Sign the request
425        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
426            Ok(sig) => sig,
427            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
428        };
429        
430        // Add signature to query params
431        query_params.push(("signature".to_string(), signature));
432    }
433
434    // Apply all query parameters
435    if !query_params.is_empty() {
436        req_builder = req_builder.query(&query_params);
437    }
438
439
440    // Add user agent if configured
441    if let Some(ref user_agent) = configuration.user_agent {
442        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
443    }
444
445    // Apply all header parameters
446    for (header_name, header_value) in header_params {
447        req_builder = req_builder.header(&header_name, &header_value);
448    }
449
450    let mut multipart_form_params = std::collections::HashMap::new();
451    multipart_form_params.insert("listenKey", params.listen_key.to_string());
452    multipart_form_params.insert("symbol", params.symbol.to_string());
453    req_builder = req_builder.form(&multipart_form_params);
454
455    let req = req_builder.build()?;
456    let resp = configuration.client.execute(req).await?;
457
458    let status = resp.status();
459    let content_type = resp
460        .headers()
461        .get("content-type")
462        .and_then(|v| v.to_str().ok())
463        .unwrap_or("application/octet-stream");
464    let content_type = super::ContentType::from(content_type);
465
466    if !status.is_client_error() && !status.is_server_error() {
467        let content = resp.text().await?;
468        match content_type {
469            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
470            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `serde_json::Value`"))),
471            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `serde_json::Value`")))),
472        }
473    } else {
474        let content = resp.text().await?;
475        let entity: Option<MarginUpdateUserDataStreamIsolatedV1Error> = serde_json::from_str(&content).ok();
476        Err(Error::ResponseError(ResponseContent { status, content, entity }))
477    }
478}
479
480/// Keepalive a margin user data stream to prevent a time out.
481pub async fn margin_update_user_data_stream_v1(configuration: &configuration::Configuration, params: MarginUpdateUserDataStreamV1Params) -> Result<serde_json::Value, Error<MarginUpdateUserDataStreamV1Error>> {
482
483    let uri_str = format!("{}/sapi/v1/userDataStream", configuration.base_path);
484    let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
485
486    // Create a mutable vector for query parameters
487    let mut query_params: Vec<(String, String)> = Vec::new();
488
489
490    // Create header parameters collection
491    let mut header_params = std::collections::HashMap::new();
492
493    // Handle Binance Auth first if configured
494    if let Some(ref binance_auth) = configuration.binance_auth {
495        // Add API key to headers
496        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
497        
498        // Generate request body for signing (if any)
499        let body_string: Option<Vec<u8>> = None;
500        
501        // Sign the request
502        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
503            Ok(sig) => sig,
504            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
505        };
506        
507        // Add signature to query params
508        query_params.push(("signature".to_string(), signature));
509    }
510
511    // Apply all query parameters
512    if !query_params.is_empty() {
513        req_builder = req_builder.query(&query_params);
514    }
515
516
517    // Add user agent if configured
518    if let Some(ref user_agent) = configuration.user_agent {
519        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
520    }
521
522    // Apply all header parameters
523    for (header_name, header_value) in header_params {
524        req_builder = req_builder.header(&header_name, &header_value);
525    }
526
527    let mut multipart_form_params = std::collections::HashMap::new();
528    multipart_form_params.insert("listenKey", params.listen_key.to_string());
529    req_builder = req_builder.form(&multipart_form_params);
530
531    let req = req_builder.build()?;
532    let resp = configuration.client.execute(req).await?;
533
534    let status = resp.status();
535    let content_type = resp
536        .headers()
537        .get("content-type")
538        .and_then(|v| v.to_str().ok())
539        .unwrap_or("application/octet-stream");
540    let content_type = super::ContentType::from(content_type);
541
542    if !status.is_client_error() && !status.is_server_error() {
543        let content = resp.text().await?;
544        match content_type {
545            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
546            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `serde_json::Value`"))),
547            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `serde_json::Value`")))),
548        }
549    } else {
550        let content = resp.text().await?;
551        let entity: Option<MarginUpdateUserDataStreamV1Error> = serde_json::from_str(&content).ok();
552        Err(Error::ResponseError(ResponseContent { status, content, entity }))
553    }
554}
555