binance/margin/apis/
risk_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_update_margin_listen_key_v1`]
18#[derive(Clone, Debug, Default)]
19pub struct MarginUpdateMarginListenKeyV1Params {
20    pub listen_key: String
21}
22
23
24/// struct for typed errors of method [`margin_create_margin_listen_key_v1`]
25#[derive(Debug, Clone, Serialize, Deserialize)]
26#[serde(untagged)]
27pub enum MarginCreateMarginListenKeyV1Error {
28    Status4XX(models::ApiError),
29    Status5XX(models::ApiError),
30    UnknownValue(serde_json::Value),
31}
32
33/// struct for typed errors of method [`margin_delete_margin_listen_key_v1`]
34#[derive(Debug, Clone, Serialize, Deserialize)]
35#[serde(untagged)]
36pub enum MarginDeleteMarginListenKeyV1Error {
37    Status4XX(models::ApiError),
38    Status5XX(models::ApiError),
39    UnknownValue(serde_json::Value),
40}
41
42/// struct for typed errors of method [`margin_update_margin_listen_key_v1`]
43#[derive(Debug, Clone, Serialize, Deserialize)]
44#[serde(untagged)]
45pub enum MarginUpdateMarginListenKeyV1Error {
46    Status4XX(models::ApiError),
47    Status5XX(models::ApiError),
48    UnknownValue(serde_json::Value),
49}
50
51
52/// Start a new user data stream.
53pub async fn margin_create_margin_listen_key_v1(configuration: &configuration::Configuration) -> Result<models::MarginCreateMarginListenKeyV1Resp, Error<MarginCreateMarginListenKeyV1Error>> {
54
55    let uri_str = format!("{}/sapi/v1/margin/listen-key", configuration.base_path);
56    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
57
58    // Create a mutable vector for query parameters
59    let mut query_params: Vec<(String, String)> = Vec::new();
60
61
62    // Create header parameters collection
63    let mut header_params = std::collections::HashMap::new();
64
65    // Handle Binance Auth first if configured
66    if let Some(ref binance_auth) = configuration.binance_auth {
67        // Add API key to headers
68        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
69        
70        // Generate request body for signing (if any)
71        let body_string: Option<Vec<u8>> = None;
72        
73        // Sign the request
74        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
75            Ok(sig) => sig,
76            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
77        };
78        
79        // Add signature to query params
80        query_params.push(("signature".to_string(), signature));
81    }
82
83    // Apply all query parameters
84    if !query_params.is_empty() {
85        req_builder = req_builder.query(&query_params);
86    }
87
88
89    // Add user agent if configured
90    if let Some(ref user_agent) = configuration.user_agent {
91        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
92    }
93
94    // Apply all header parameters
95    for (header_name, header_value) in header_params {
96        req_builder = req_builder.header(&header_name, &header_value);
97    }
98
99
100    let req = req_builder.build()?;
101    let resp = configuration.client.execute(req).await?;
102
103    let status = resp.status();
104    let content_type = resp
105        .headers()
106        .get("content-type")
107        .and_then(|v| v.to_str().ok())
108        .unwrap_or("application/octet-stream");
109    let content_type = super::ContentType::from(content_type);
110
111    if !status.is_client_error() && !status.is_server_error() {
112        let content = resp.text().await?;
113        match content_type {
114            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
115            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::MarginCreateMarginListenKeyV1Resp`"))),
116            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::MarginCreateMarginListenKeyV1Resp`")))),
117        }
118    } else {
119        let content = resp.text().await?;
120        let entity: Option<MarginCreateMarginListenKeyV1Error> = serde_json::from_str(&content).ok();
121        Err(Error::ResponseError(ResponseContent { status, content, entity }))
122    }
123}
124
125/// Close out a user data stream.
126pub async fn margin_delete_margin_listen_key_v1(configuration: &configuration::Configuration) -> Result<serde_json::Value, Error<MarginDeleteMarginListenKeyV1Error>> {
127
128    let uri_str = format!("{}/sapi/v1/margin/listen-key", configuration.base_path);
129    let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);
130
131    // Create a mutable vector for query parameters
132    let mut query_params: Vec<(String, String)> = Vec::new();
133
134
135    // Create header parameters collection
136    let mut header_params = std::collections::HashMap::new();
137
138    // Handle Binance Auth first if configured
139    if let Some(ref binance_auth) = configuration.binance_auth {
140        // Add API key to headers
141        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
142        
143        // Generate request body for signing (if any)
144        let body_string: Option<Vec<u8>> = None;
145        
146        // Sign the request
147        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
148            Ok(sig) => sig,
149            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
150        };
151        
152        // Add signature to query params
153        query_params.push(("signature".to_string(), signature));
154    }
155
156    // Apply all query parameters
157    if !query_params.is_empty() {
158        req_builder = req_builder.query(&query_params);
159    }
160
161
162    // Add user agent if configured
163    if let Some(ref user_agent) = configuration.user_agent {
164        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
165    }
166
167    // Apply all header parameters
168    for (header_name, header_value) in header_params {
169        req_builder = req_builder.header(&header_name, &header_value);
170    }
171
172
173    let req = req_builder.build()?;
174    let resp = configuration.client.execute(req).await?;
175
176    let status = resp.status();
177    let content_type = resp
178        .headers()
179        .get("content-type")
180        .and_then(|v| v.to_str().ok())
181        .unwrap_or("application/octet-stream");
182    let content_type = super::ContentType::from(content_type);
183
184    if !status.is_client_error() && !status.is_server_error() {
185        let content = resp.text().await?;
186        match content_type {
187            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
188            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `serde_json::Value`"))),
189            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`")))),
190        }
191    } else {
192        let content = resp.text().await?;
193        let entity: Option<MarginDeleteMarginListenKeyV1Error> = serde_json::from_str(&content).ok();
194        Err(Error::ResponseError(ResponseContent { status, content, entity }))
195    }
196}
197
198/// Keepalive a user data stream to prevent a time out.
199pub async fn margin_update_margin_listen_key_v1(configuration: &configuration::Configuration, params: MarginUpdateMarginListenKeyV1Params) -> Result<serde_json::Value, Error<MarginUpdateMarginListenKeyV1Error>> {
200
201    let uri_str = format!("{}/sapi/v1/margin/listen-key", configuration.base_path);
202    let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
203
204    // Create a mutable vector for query parameters
205    let mut query_params: Vec<(String, String)> = Vec::new();
206
207
208    // Create header parameters collection
209    let mut header_params = std::collections::HashMap::new();
210
211    // Handle Binance Auth first if configured
212    if let Some(ref binance_auth) = configuration.binance_auth {
213        // Add API key to headers
214        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
215        
216        // Generate request body for signing (if any)
217        let body_string: Option<Vec<u8>> = None;
218        
219        // Sign the request
220        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
221            Ok(sig) => sig,
222            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
223        };
224        
225        // Add signature to query params
226        query_params.push(("signature".to_string(), signature));
227    }
228
229    // Apply all query parameters
230    if !query_params.is_empty() {
231        req_builder = req_builder.query(&query_params);
232    }
233
234
235    // Add user agent if configured
236    if let Some(ref user_agent) = configuration.user_agent {
237        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
238    }
239
240    // Apply all header parameters
241    for (header_name, header_value) in header_params {
242        req_builder = req_builder.header(&header_name, &header_value);
243    }
244
245    let mut multipart_form_params = std::collections::HashMap::new();
246    multipart_form_params.insert("listenKey", params.listen_key.to_string());
247    req_builder = req_builder.form(&multipart_form_params);
248
249    let req = req_builder.build()?;
250    let resp = configuration.client.execute(req).await?;
251
252    let status = resp.status();
253    let content_type = resp
254        .headers()
255        .get("content-type")
256        .and_then(|v| v.to_str().ok())
257        .unwrap_or("application/octet-stream");
258    let content_type = super::ContentType::from(content_type);
259
260    if !status.is_client_error() && !status.is_server_error() {
261        let content = resp.text().await?;
262        match content_type {
263            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
264            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `serde_json::Value`"))),
265            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`")))),
266        }
267    } else {
268        let content = resp.text().await?;
269        let entity: Option<MarginUpdateMarginListenKeyV1Error> = serde_json::from_str(&content).ok();
270        Err(Error::ResponseError(ResponseContent { status, content, entity }))
271    }
272}
273