binance/convert/apis/
market_data_api.rs

1/*
2 * Binance Convert API
3 *
4 * OpenAPI specification for Binance exchange - Convert 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::convert::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17/// struct for passing parameters to the method [`convert_get_convert_asset_info_v1`]
18#[derive(Clone, Debug, Default)]
19pub struct ConvertGetConvertAssetInfoV1Params {
20    pub timestamp: i64,
21    /// The value cannot be greater than 60000
22    pub recv_window: Option<i64>
23}
24
25/// struct for passing parameters to the method [`convert_get_convert_exchange_info_v1`]
26#[derive(Clone, Debug, Default)]
27pub struct ConvertGetConvertExchangeInfoV1Params {
28    /// User spends coin
29    pub from_asset: Option<String>,
30    /// User receives coin
31    pub to_asset: Option<String>
32}
33
34
35/// struct for typed errors of method [`convert_get_convert_asset_info_v1`]
36#[derive(Debug, Clone, Serialize, Deserialize)]
37#[serde(untagged)]
38pub enum ConvertGetConvertAssetInfoV1Error {
39    Status4XX(models::ApiError),
40    Status5XX(models::ApiError),
41    UnknownValue(serde_json::Value),
42}
43
44/// struct for typed errors of method [`convert_get_convert_exchange_info_v1`]
45#[derive(Debug, Clone, Serialize, Deserialize)]
46#[serde(untagged)]
47pub enum ConvertGetConvertExchangeInfoV1Error {
48    Status4XX(models::ApiError),
49    Status5XX(models::ApiError),
50    UnknownValue(serde_json::Value),
51}
52
53
54/// Query for supported asset’s precision information
55pub async fn convert_get_convert_asset_info_v1(configuration: &configuration::Configuration, params: ConvertGetConvertAssetInfoV1Params) -> Result<Vec<models::ConvertGetConvertAssetInfoV1RespItem>, Error<ConvertGetConvertAssetInfoV1Error>> {
56
57    let uri_str = format!("{}/sapi/v1/convert/assetInfo", configuration.base_path);
58    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
59
60    // Create a mutable vector for query parameters
61    let mut query_params: Vec<(String, String)> = Vec::new();
62
63    if let Some(ref param_value) = params.recv_window {
64        query_params.push(("recvWindow".to_string(), param_value.to_string()));
65    }
66    query_params.push(("timestamp".to_string(), params.timestamp.to_string()));
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 `Vec&lt;models::ConvertGetConvertAssetInfoV1RespItem&gt;`"))),
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 `Vec&lt;models::ConvertGetConvertAssetInfoV1RespItem&gt;`")))),
123        }
124    } else {
125        let content = resp.text().await?;
126        let entity: Option<ConvertGetConvertAssetInfoV1Error> = serde_json::from_str(&content).ok();
127        Err(Error::ResponseError(ResponseContent { status, content, entity }))
128    }
129}
130
131/// Query for all convertible token pairs and the tokens’ respective upper/lower limits
132pub async fn convert_get_convert_exchange_info_v1(configuration: &configuration::Configuration, params: ConvertGetConvertExchangeInfoV1Params) -> Result<Vec<models::ConvertGetConvertExchangeInfoV1RespItem>, Error<ConvertGetConvertExchangeInfoV1Error>> {
133
134    let uri_str = format!("{}/sapi/v1/convert/exchangeInfo", configuration.base_path);
135    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
136
137    // Create a mutable vector for query parameters
138    let mut query_params: Vec<(String, String)> = Vec::new();
139
140    if let Some(ref param_value) = params.from_asset {
141        query_params.push(("fromAsset".to_string(), param_value.to_string()));
142    }
143    if let Some(ref param_value) = params.to_asset {
144        query_params.push(("toAsset".to_string(), param_value.to_string()));
145    }
146
147    // Create header parameters collection
148    let mut header_params = std::collections::HashMap::new();
149
150    // Handle Binance Auth first if configured
151    if let Some(ref binance_auth) = configuration.binance_auth {
152        // Add API key to headers
153        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
154        
155        // Generate request body for signing (if any)
156        let body_string: Option<Vec<u8>> = None;
157        
158        // Sign the request
159        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
160            Ok(sig) => sig,
161            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
162        };
163        
164        // Add signature to query params
165        query_params.push(("signature".to_string(), signature));
166    }
167
168    // Apply all query parameters
169    if !query_params.is_empty() {
170        req_builder = req_builder.query(&query_params);
171    }
172
173
174    // Add user agent if configured
175    if let Some(ref user_agent) = configuration.user_agent {
176        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
177    }
178
179    // Apply all header parameters
180    for (header_name, header_value) in header_params {
181        req_builder = req_builder.header(&header_name, &header_value);
182    }
183
184
185    let req = req_builder.build()?;
186    let resp = configuration.client.execute(req).await?;
187
188    let status = resp.status();
189    let content_type = resp
190        .headers()
191        .get("content-type")
192        .and_then(|v| v.to_str().ok())
193        .unwrap_or("application/octet-stream");
194    let content_type = super::ContentType::from(content_type);
195
196    if !status.is_client_error() && !status.is_server_error() {
197        let content = resp.text().await?;
198        match content_type {
199            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
200            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec&lt;models::ConvertGetConvertExchangeInfoV1RespItem&gt;`"))),
201            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec&lt;models::ConvertGetConvertExchangeInfoV1RespItem&gt;`")))),
202        }
203    } else {
204        let content = resp.text().await?;
205        let entity: Option<ConvertGetConvertExchangeInfoV1Error> = serde_json::from_str(&content).ok();
206        Err(Error::ResponseError(ResponseContent { status, content, entity }))
207    }
208}
209