binance/spot/apis/
general_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_get_exchange_info_v3`]
18#[derive(Clone, Debug, Default)]
19pub struct SpotGetExchangeInfoV3Params {
20    /// Example: curl -X GET &#34;<a href=\"https://api.binance.com/api/v3/exchangeInfo?symbol=BNBBTC\" target=\"_blank\" rel=\"noopener noreferrer\">https://api.binance.com/api/v3/exchangeInfo?symbol=BNBBTC</a>&#34;
21    pub symbol: Option<String>,
22    /// Examples: curl -X GET &#34;<a href=\"https://api.binance.com/api/v3/exchangeInfo?symbols=%5B%22BNBBTC%22,%22BTCUSDT%22%5D\" target=\"_blank\" rel=\"noopener noreferrer\">https://api.binance.com/api/v3/exchangeInfo?symbols=%5B%22BNBBTC%22,%22BTCUSDT%22%5D</a>&#34; <br/> or <br/> curl -g -X  GET &#39;<a href=\"https://api.binance.com/api/v3/exchangeInfo?symbols=%5B%22BTCUSDT%22,%22BNBBTC\" target=\"_blank\" rel=\"noopener noreferrer\">https://api.binance.com/api/v3/exchangeInfo?symbols=[&#34;BTCUSDT&#34;,&#34;BNBBTC</a>&#34;]&#39;
23    pub symbols: Option<Vec<String>>,
24    /// Examples: curl -X GET &#34;<a href=\"https://api.binance.com/api/v3/exchangeInfo?permissions=SPOT\" target=\"_blank\" rel=\"noopener noreferrer\">https://api.binance.com/api/v3/exchangeInfo?permissions=SPOT</a>&#34; <br/> or <br/> curl -X GET &#34;<a href=\"https://api.binance.com/api/v3/exchangeInfo?permissions=%5B%22MARGIN%22%2C%22LEVERAGED%22%5D\" target=\"_blank\" rel=\"noopener noreferrer\">https://api.binance.com/api/v3/exchangeInfo?permissions=%5B%22MARGIN%22%2C%22LEVERAGED%22%5D</a>&#34; <br/> or <br/> curl -g -X GET &#39;<a href=\"https://api.binance.com/api/v3/exchangeInfo?permissions=%5B%22MARGIN%22,%22LEVERAGED\" target=\"_blank\" rel=\"noopener noreferrer\">https://api.binance.com/api/v3/exchangeInfo?permissions=[&#34;MARGIN&#34;,&#34;LEVERAGED</a>&#34;]&#39;
25    pub permissions: Option<String>,
26    /// Controls whether the content of the `permissionSets` field is populated or not. Defaults to `true`
27    pub show_permission_sets: Option<bool>,
28    /// Filters symbols that have this `tradingStatus`. Valid values: `TRADING`, `HALT`, `BREAK` <br/> Cannot be used in combination with `symbols` or `symbol`.
29    pub symbol_status: Option<String>
30}
31
32
33/// struct for typed errors of method [`spot_get_exchange_info_v3`]
34#[derive(Debug, Clone, Serialize, Deserialize)]
35#[serde(untagged)]
36pub enum SpotGetExchangeInfoV3Error {
37    Status4XX(models::ApiError),
38    Status5XX(models::ApiError),
39    UnknownValue(serde_json::Value),
40}
41
42/// struct for typed errors of method [`spot_get_ping_v3`]
43#[derive(Debug, Clone, Serialize, Deserialize)]
44#[serde(untagged)]
45pub enum SpotGetPingV3Error {
46    Status4XX(models::ApiError),
47    Status5XX(models::ApiError),
48    UnknownValue(serde_json::Value),
49}
50
51/// struct for typed errors of method [`spot_get_time_v3`]
52#[derive(Debug, Clone, Serialize, Deserialize)]
53#[serde(untagged)]
54pub enum SpotGetTimeV3Error {
55    Status4XX(models::ApiError),
56    Status5XX(models::ApiError),
57    UnknownValue(serde_json::Value),
58}
59
60
61/// Current exchange trading rules and symbol information
62pub async fn spot_get_exchange_info_v3(configuration: &configuration::Configuration, params: SpotGetExchangeInfoV3Params) -> Result<models::SpotGetExchangeInfoV3Resp, Error<SpotGetExchangeInfoV3Error>> {
63
64    let uri_str = format!("{}/api/v3/exchangeInfo", configuration.base_path);
65    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
66
67    // Create a mutable vector for query parameters
68    let mut query_params: Vec<(String, String)> = Vec::new();
69
70    if let Some(ref param_value) = params.symbol {
71        query_params.push(("symbol".to_string(), param_value.to_string()));
72    }
73    if let Some(ref param_value) = params.symbols {
74        match "multi" {
75            "multi" => {
76                for p in param_value {
77                    query_params.push(("symbols".to_string(), p.to_string()));
78                }
79            },
80            _ => {
81                let joined = param_value.iter()
82                    .map(|p| p.to_string())
83                    .collect::<Vec<String>>()
84                    .join(",");
85                query_params.push(("symbols".to_string(), joined));
86            }
87        };
88    }
89    if let Some(ref param_value) = params.permissions {
90        query_params.push(("permissions".to_string(), param_value.to_string()));
91    }
92    if let Some(ref param_value) = params.show_permission_sets {
93        query_params.push(("showPermissionSets".to_string(), param_value.to_string()));
94    }
95    if let Some(ref param_value) = params.symbol_status {
96        query_params.push(("symbolStatus".to_string(), param_value.to_string()));
97    }
98
99    // Create header parameters collection
100    let mut header_params = std::collections::HashMap::new();
101
102    // Handle Binance Auth first if configured
103    if let Some(ref binance_auth) = configuration.binance_auth {
104        // Add API key to headers
105        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
106        
107        // Generate request body for signing (if any)
108        let body_string: Option<Vec<u8>> = None;
109        
110        // Sign the request
111        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
112            Ok(sig) => sig,
113            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
114        };
115        
116        // Add signature to query params
117        query_params.push(("signature".to_string(), signature));
118    }
119
120    // Apply all query parameters
121    if !query_params.is_empty() {
122        req_builder = req_builder.query(&query_params);
123    }
124
125
126    // Add user agent if configured
127    if let Some(ref user_agent) = configuration.user_agent {
128        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
129    }
130
131    // Apply all header parameters
132    for (header_name, header_value) in header_params {
133        req_builder = req_builder.header(&header_name, &header_value);
134    }
135
136
137    let req = req_builder.build()?;
138    let resp = configuration.client.execute(req).await?;
139
140    let status = resp.status();
141    let content_type = resp
142        .headers()
143        .get("content-type")
144        .and_then(|v| v.to_str().ok())
145        .unwrap_or("application/octet-stream");
146    let content_type = super::ContentType::from(content_type);
147
148    if !status.is_client_error() && !status.is_server_error() {
149        let content = resp.text().await?;
150        match content_type {
151            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
152            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::SpotGetExchangeInfoV3Resp`"))),
153            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::SpotGetExchangeInfoV3Resp`")))),
154        }
155    } else {
156        let content = resp.text().await?;
157        let entity: Option<SpotGetExchangeInfoV3Error> = serde_json::from_str(&content).ok();
158        Err(Error::ResponseError(ResponseContent { status, content, entity }))
159    }
160}
161
162/// Test connectivity to the Rest API.
163pub async fn spot_get_ping_v3(configuration: &configuration::Configuration) -> Result<serde_json::Value, Error<SpotGetPingV3Error>> {
164
165    let uri_str = format!("{}/api/v3/ping", configuration.base_path);
166    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
167
168    // Create a mutable vector for query parameters
169    let mut query_params: Vec<(String, String)> = Vec::new();
170
171
172    // Create header parameters collection
173    let mut header_params = std::collections::HashMap::new();
174
175    // Handle Binance Auth first if configured
176    if let Some(ref binance_auth) = configuration.binance_auth {
177        // Add API key to headers
178        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
179        
180        // Generate request body for signing (if any)
181        let body_string: Option<Vec<u8>> = None;
182        
183        // Sign the request
184        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
185            Ok(sig) => sig,
186            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
187        };
188        
189        // Add signature to query params
190        query_params.push(("signature".to_string(), signature));
191    }
192
193    // Apply all query parameters
194    if !query_params.is_empty() {
195        req_builder = req_builder.query(&query_params);
196    }
197
198
199    // Add user agent if configured
200    if let Some(ref user_agent) = configuration.user_agent {
201        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
202    }
203
204    // Apply all header parameters
205    for (header_name, header_value) in header_params {
206        req_builder = req_builder.header(&header_name, &header_value);
207    }
208
209
210    let req = req_builder.build()?;
211    let resp = configuration.client.execute(req).await?;
212
213    let status = resp.status();
214    let content_type = resp
215        .headers()
216        .get("content-type")
217        .and_then(|v| v.to_str().ok())
218        .unwrap_or("application/octet-stream");
219    let content_type = super::ContentType::from(content_type);
220
221    if !status.is_client_error() && !status.is_server_error() {
222        let content = resp.text().await?;
223        match content_type {
224            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
225            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `serde_json::Value`"))),
226            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`")))),
227        }
228    } else {
229        let content = resp.text().await?;
230        let entity: Option<SpotGetPingV3Error> = serde_json::from_str(&content).ok();
231        Err(Error::ResponseError(ResponseContent { status, content, entity }))
232    }
233}
234
235/// Test connectivity to the Rest API and get the current server time.
236pub async fn spot_get_time_v3(configuration: &configuration::Configuration) -> Result<models::SpotGetTimeV3Resp, Error<SpotGetTimeV3Error>> {
237
238    let uri_str = format!("{}/api/v3/time", configuration.base_path);
239    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
240
241    // Create a mutable vector for query parameters
242    let mut query_params: Vec<(String, String)> = Vec::new();
243
244
245    // Create header parameters collection
246    let mut header_params = std::collections::HashMap::new();
247
248    // Handle Binance Auth first if configured
249    if let Some(ref binance_auth) = configuration.binance_auth {
250        // Add API key to headers
251        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
252        
253        // Generate request body for signing (if any)
254        let body_string: Option<Vec<u8>> = None;
255        
256        // Sign the request
257        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
258            Ok(sig) => sig,
259            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
260        };
261        
262        // Add signature to query params
263        query_params.push(("signature".to_string(), signature));
264    }
265
266    // Apply all query parameters
267    if !query_params.is_empty() {
268        req_builder = req_builder.query(&query_params);
269    }
270
271
272    // Add user agent if configured
273    if let Some(ref user_agent) = configuration.user_agent {
274        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
275    }
276
277    // Apply all header parameters
278    for (header_name, header_value) in header_params {
279        req_builder = req_builder.header(&header_name, &header_value);
280    }
281
282
283    let req = req_builder.build()?;
284    let resp = configuration.client.execute(req).await?;
285
286    let status = resp.status();
287    let content_type = resp
288        .headers()
289        .get("content-type")
290        .and_then(|v| v.to_str().ok())
291        .unwrap_or("application/octet-stream");
292    let content_type = super::ContentType::from(content_type);
293
294    if !status.is_client_error() && !status.is_server_error() {
295        let content = resp.text().await?;
296        match content_type {
297            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
298            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::SpotGetTimeV3Resp`"))),
299            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::SpotGetTimeV3Resp`")))),
300        }
301    } else {
302        let content = resp.text().await?;
303        let entity: Option<SpotGetTimeV3Error> = serde_json::from_str(&content).ok();
304        Err(Error::ResponseError(ResponseContent { status, content, entity }))
305    }
306}
307