binance/spot/apis/
mod.rs

1use std::error;
2use std::fmt;
3
4#[derive(Debug, Clone)]
5pub struct ResponseContent<T> {
6    pub status: reqwest::StatusCode,
7    pub content: String,
8    pub entity: Option<T>,
9}
10
11#[derive(Debug)]
12pub enum Error<T> {
13    Reqwest(reqwest::Error),
14    Serde(serde_json::Error),
15    Io(std::io::Error),
16    ResponseError(ResponseContent<T>),
17    BinanceAuth(BinanceAuthError),
18    Generic(String),
19}
20
21impl <T> fmt::Display for Error<T> {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        let (module, e) = match self {
24            Error::Reqwest(e) => ("reqwest", e.to_string()),
25            Error::Serde(e) => ("serde", e.to_string()),
26            Error::Io(e) => ("IO", e.to_string()),
27            Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
28            Error::BinanceAuth(e) => ("binance auth", e.to_string()),
29            Error::Generic(e) => ("generic", e.to_string()),
30        };
31        write!(f, "error in {}: {}", module, e)
32    }
33}
34
35impl <T: fmt::Debug> error::Error for Error<T> {
36    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
37        Some(match self {
38            Error::Reqwest(e) => e,
39            Error::Serde(e) => e,
40            Error::Io(e) => e,
41            Error::ResponseError(_) => return None,
42            Error::BinanceAuth(e) => e,
43            Error::Generic(_) => return None,
44        })
45    }
46}
47
48impl <T> From<reqwest::Error> for Error<T> {
49    fn from(e: reqwest::Error) -> Self {
50        Error::Reqwest(e)
51    }
52}
53
54impl <T> From<serde_json::Error> for Error<T> {
55    fn from(e: serde_json::Error) -> Self {
56        Error::Serde(e)
57    }
58}
59
60impl <T> From<std::io::Error> for Error<T> {
61    fn from(e: std::io::Error) -> Self {
62        Error::Io(e)
63    }
64}
65
66impl <T> From<BinanceAuthError> for Error<T> {
67    fn from(e: BinanceAuthError) -> Self {
68        Error::BinanceAuth(e)
69    }
70}
71
72pub fn urlencode<T: AsRef<str>>(s: T) -> String {
73    ::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
74}
75
76pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String, String)> {
77    if let serde_json::Value::Object(object) = value {
78        let mut params = vec![];
79
80        for (key, value) in object {
81            match value {
82                serde_json::Value::Object(_) => params.append(&mut parse_deep_object(
83                    &format!("{}[{}]", prefix, key),
84                    value,
85                )),
86                serde_json::Value::Array(array) => {
87                    for (i, value) in array.iter().enumerate() {
88                        params.append(&mut parse_deep_object(
89                            &format!("{}[{}][{}]", prefix, key, i),
90                            value,
91                        ));
92                    }
93                },
94                serde_json::Value::String(s) => params.push((format!("{}[{}]", prefix, key), s.clone())),
95                _ => params.push((format!("{}[{}]", prefix, key), value.to_string())),
96            }
97        }
98
99        return params;
100    }
101
102    unimplemented!("Only objects are supported with style=deepObject")
103}
104
105/// Internal use only
106/// A content type supported by this client.
107#[allow(dead_code)]
108enum ContentType {
109    Json,
110    Text,
111    Unsupported(String)
112}
113
114impl From<&str> for ContentType {
115    fn from(content_type: &str) -> Self {
116        if content_type.starts_with("application") && content_type.contains("json") {
117            return Self::Json;
118        } else if content_type.starts_with("text/plain") {
119            return Self::Text;
120        } else {
121            return Self::Unsupported(content_type.to_string());
122        }
123    }
124}
125
126pub mod algo_trading_api;
127pub mod binance_link_api;
128pub mod convert_api;
129pub mod copy_trading_api;
130pub mod futures_data_api;
131pub mod margin_trading_api;
132pub mod portfolio_margin_pro_api;
133pub mod spot_trading_api;
134pub mod sub_account_api;
135pub mod wallet_api;
136
137pub mod signing;
138pub mod configuration;
139pub use configuration::*;