binance/spot/apis/
user_data_stream_api.rs1use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::spot::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17#[derive(Clone, Debug, Default)]
19pub struct SpotDeleteUserDataStreamV3Params {
20 pub listen_key: String
21}
22
23#[derive(Clone, Debug, Default)]
25pub struct SpotUpdateUserDataStreamV3Params {
26 pub listen_key: String
27}
28
29
30#[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#[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#[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
58pub 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 let mut query_params: Vec<(String, String)> = Vec::new();
66
67
68 let mut header_params = std::collections::HashMap::new();
70
71 if let Some(ref binance_auth) = configuration.binance_auth {
73 header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
75
76 let body_string: Option<Vec<u8>> = None;
78
79 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 query_params.push(("signature".to_string(), signature));
87 }
88
89 if !query_params.is_empty() {
91 req_builder = req_builder.query(&query_params);
92 }
93
94
95 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 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
131pub 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 let mut query_params: Vec<(String, String)> = Vec::new();
139
140 query_params.push(("listenKey".to_string(), params.listen_key.to_string()));
141
142 let mut header_params = std::collections::HashMap::new();
144
145 if let Some(ref binance_auth) = configuration.binance_auth {
147 header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
149
150 let body_string: Option<Vec<u8>> = None;
152
153 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 query_params.push(("signature".to_string(), signature));
161 }
162
163 if !query_params.is_empty() {
165 req_builder = req_builder.query(&query_params);
166 }
167
168
169 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 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
205pub 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 let mut query_params: Vec<(String, String)> = Vec::new();
213
214
215 let mut header_params = std::collections::HashMap::new();
217
218 if let Some(ref binance_auth) = configuration.binance_auth {
220 header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
222
223 let body_string: Option<Vec<u8>> = None;
225
226 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 query_params.push(("signature".to_string(), signature));
234 }
235
236 if !query_params.is_empty() {
238 req_builder = req_builder.query(&query_params);
239 }
240
241
242 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 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