tokio_binance/client/
general.rs

1use reqwest::{Url, Client};
2use crate::param::{
3    Parameters, 
4};
5use crate::builder::ParamBuilder;
6use crate::types::*;
7
8/// Client for dealing with general exchange information
9#[derive(Clone)]
10pub struct GeneralClient {
11    pub(super) url: Url,
12    pub(super) client: Client,
13}
14
15impl GeneralClient {
16    /// Creates new client instance
17    /// # Example
18    ///
19    /// ```no_run
20    /// use tokio_binance::{GeneralClient, BINANCE_US_URL};
21    /// 
22    /// #[tokio::main]
23    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
24    ///     let client = GeneralClient::connect(BINANCE_US_URL)?;
25    ///     Ok(())
26    /// }
27    /// ```
28    pub fn connect<U: Into<String>>(url: U) -> crate::error::Result<Self> {
29        Ok(Self {
30            url: url.into().parse::<Url>()?,
31            client: Client::new()
32        })
33    }
34    /// Test connectivity to the Rest API.
35    /// # Example
36    ///
37    /// ```no_run
38    /// # use tokio_binance::{GeneralClient, BINANCE_US_URL};
39    /// use serde_json::Value;
40    /// 
41    /// # #[tokio::main]
42    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
43    /// # let client = GeneralClient::connect(BINANCE_US_URL)?;
44    /// let response = client
45    ///     .ping()
46    ///     .json::<Value>()
47    ///     .await?;
48    /// # Ok(())
49    /// # }
50    /// ```
51    pub fn ping(&self) -> ParamBuilder<'_, '_, PingParams>{
52        let Self { url, client } = self;
53        let url = url.join("/api/v3/ping").unwrap();
54
55        ParamBuilder::new(
56            Parameters::default(),
57            client.get(url),
58            None,
59            None
60        )
61    }
62    /// Test connectivity to the Rest API and get the current server time.
63    /// # Example
64    ///
65    /// ```no_run
66    /// # use tokio_binance::{GeneralClient, BINANCE_US_URL};
67    /// use serde_json::Value;
68    /// 
69    /// # #[tokio::main]
70    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
71    /// # let client = GeneralClient::connect(BINANCE_US_URL)?;
72    /// let response = client
73    ///     .get_server_time()
74    ///     .json::<Value>()
75    ///     .await?;
76    /// # Ok(())
77    /// # }
78    /// ```
79    pub fn get_server_time(&self) -> ParamBuilder<'_, '_, TimeParams>{
80        let Self { url, client } = self;
81        let url = url.join("/api/v3/time").unwrap();
82
83        ParamBuilder::new(
84            Parameters::default(),
85            client.get(url),
86            None,
87            None
88        )
89    }
90    /// Current exchange trading rules and symbol information.
91    /// # Example
92    ///
93    /// ```no_run
94    /// # use tokio_binance::{GeneralClient, BINANCE_US_URL};
95    /// use serde_json::Value;
96    /// 
97    /// # #[tokio::main]
98    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
99    /// # let client = GeneralClient::connect(BINANCE_US_URL)?;
100    /// let response = client
101    ///     .get_exchange_info()
102    ///     .json::<Value>()
103    ///     .await?;
104    /// # Ok(())
105    /// # }
106    /// ```
107    pub fn get_exchange_info(&self) -> ParamBuilder<'_, '_, ExchangeInfoParams>{
108        let Self { url, client } = self;
109        let url = url.join("/api/v3/exchangeInfo").unwrap();
110
111        ParamBuilder::new(
112            Parameters::default(),
113            client.get(url),
114            None,
115            None
116        )
117    }
118}