binance/spot/apis/
user_data_stream_api.rs

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