binance/margin/apis/
risk_data_stream_api.rs1use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::margin::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17#[derive(Clone, Debug, Default)]
19pub struct MarginUpdateMarginListenKeyV1Params {
20 pub listen_key: String
21}
22
23
24#[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#[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#[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
52pub 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 let mut query_params: Vec<(String, String)> = Vec::new();
60
61
62 let mut header_params = std::collections::HashMap::new();
64
65 if let Some(ref binance_auth) = configuration.binance_auth {
67 header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
69
70 let body_string: Option<Vec<u8>> = None;
72
73 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 query_params.push(("signature".to_string(), signature));
81 }
82
83 if !query_params.is_empty() {
85 req_builder = req_builder.query(&query_params);
86 }
87
88
89 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 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
125pub 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 let mut query_params: Vec<(String, String)> = Vec::new();
133
134
135 let mut header_params = std::collections::HashMap::new();
137
138 if let Some(ref binance_auth) = configuration.binance_auth {
140 header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
142
143 let body_string: Option<Vec<u8>> = None;
145
146 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 query_params.push(("signature".to_string(), signature));
154 }
155
156 if !query_params.is_empty() {
158 req_builder = req_builder.query(&query_params);
159 }
160
161
162 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 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
198pub 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 let mut query_params: Vec<(String, String)> = Vec::new();
206
207
208 let mut header_params = std::collections::HashMap::new();
210
211 if let Some(ref binance_auth) = configuration.binance_auth {
213 header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
215
216 let body_string: Option<Vec<u8>> = None;
218
219 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 query_params.push(("signature".to_string(), signature));
227 }
228
229 if !query_params.is_empty() {
231 req_builder = req_builder.query(&query_params);
232 }
233
234
235 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 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