binance_client/http_exchange_api_v2/
mod.rs1pub mod data;
6pub mod response;
7
8use reqwest::Method;
9use reqwest::Url;
10
11use crate::error::Error;
12
13use self::data::product_by_symbol::get::request::Query as ProductBySymbolGetQuery;
14use self::data::product_by_symbol::get::response::Response as ProductBySymbolGetResponse;
15use self::data::products::get::request::Query as ProductsGetQuery;
16use self::data::products::get::response::Response as ProductsGetResponse;
17use self::response::Response;
18
19#[derive(Debug, Clone)]
23pub struct Client {
24 inner: reqwest::Client,
26}
27
28impl Default for Client {
29 fn default() -> Self {
30 Self::new()
31 }
32}
33
34type Result<T> = ::std::result::Result<Response<T>, Error>;
35
36impl Client {
37 const BASE_URL: &'static str = "https://binance.com";
39
40 pub fn new() -> Self {
44 Self {
45 inner: reqwest::Client::new(),
46 }
47 }
48
49 pub fn get_products(&self, request: ProductsGetQuery) -> Result<ProductsGetResponse> {
53 self.execute::<ProductsGetResponse>(
54 Method::GET,
55 format!(
56 "/bapi/asset/v2/public/asset-service/product/get-products?{}",
57 request.to_string()
58 ),
59 )
60 }
61
62 pub fn get_product_by_symbol(
66 &self,
67 request: ProductBySymbolGetQuery,
68 ) -> Result<ProductBySymbolGetResponse> {
69 self.execute::<ProductBySymbolGetResponse>(
70 Method::GET,
71 format!(
72 "/bapi/asset/v2/public/asset-service/product/get-product-by-symbol?{}",
73 request.to_string()
74 ),
75 )
76 }
77
78 fn execute<T>(&self, method: Method, url: String) -> Result<T>
82 where
83 T: serde::de::DeserializeOwned,
84 {
85 let url = Self::BASE_URL.to_owned() + url.as_str();
86
87 let response = self
88 .inner
89 .execute(
90 self.inner
91 .request(
92 method,
93 Url::parse(&url).map_err(|error| Error::UrlParsing(error, url))?,
94 )
95 .build()
96 .map_err(Error::RequestBuilding)?,
97 )
98 .map_err(Error::RequestExecution)?
99 .text()
100 .map_err(Error::ResponseReading)?;
101 let response: Response<T> = serde_json::from_str(response.as_str())
102 .map_err(|error| Error::ResponseParsing(error, response))?;
103
104 Ok(response)
105 }
106}