Skip to main content

datamaxi/
cex.rs

1use crate::api::{Client, Config, Datamaxi, Result};
2pub use crate::models::{CandleOptions, SymbolsOptions};
3use crate::models::{CandleResponse, SymbolsResponse};
4use std::collections::HashMap;
5
6/// Provides methods for retrieving CEX candle data and related information.
7#[derive(Clone)]
8pub struct Candle {
9    pub client: Client,
10}
11
12impl Candle {
13    /// Retrieves candle data for a specified exchange and symbol. Additional parameters can be
14    /// provided to filter and sort the results. The response will contain an array of candle data
15    /// objects, each representing a single candle with open, high, low, close, and volume values.
16    pub fn get<E, S>(
17        &self,
18        exchange: E,
19        symbol: S,
20        options: CandleOptions,
21    ) -> Result<CandleResponse>
22    where
23        E: Into<String>,
24        S: Into<String>,
25    {
26        let mut parameters = HashMap::new();
27
28        // required
29        parameters.insert("exchange".to_string(), exchange.into());
30        parameters.insert("symbol".to_string(), symbol.into());
31
32        // optional
33        parameters.extend(
34            [
35                options
36                    .market
37                    .map(|market| ("market".to_string(), market.to_string())),
38                options
39                    .interval
40                    .map(|interval| ("interval".to_string(), interval.to_string())),
41                options
42                    .page
43                    .map(|page| ("page".to_string(), page.to_string())),
44                options
45                    .limit
46                    .map(|limit| ("limit".to_string(), limit.to_string())),
47                options
48                    .from
49                    .map(|from| ("from".to_string(), from.to_string())),
50                options.to.map(|to| ("to".to_string(), to.to_string())),
51                options
52                    .sort
53                    .map(|sort| ("sort".to_string(), sort.to_string())),
54            ]
55            .into_iter()
56            .flatten(),
57        );
58
59        self.client.get("/cex/candle", Some(parameters))
60    }
61
62    /// Retrieves a list of supported exchanges for candle data. The market parameter can be used
63    /// to filter the results by market.
64    pub fn exchanges<M>(&self, market: M) -> Result<Vec<String>>
65    where
66        M: Into<String>,
67    {
68        let mut parameters = HashMap::new();
69
70        // required
71        parameters.insert("market".to_string(), market.into());
72
73        self.client.get("/cex/candle/exchanges", Some(parameters))
74    }
75
76    /// Retrieves a list of supported symbols for candle data. The exchange parameter is required,
77    /// and the market parameter can be used to filter the results by market.
78    pub fn symbols<E>(&self, exchange: E, options: SymbolsOptions) -> Result<Vec<SymbolsResponse>>
79    where
80        E: Into<String>,
81    {
82        let mut parameters = HashMap::new();
83
84        // required
85        parameters.insert("exchange".to_string(), exchange.into());
86
87        // optional
88        if let Some(market) = options.market {
89            parameters.insert("market".to_string(), market);
90        }
91
92        self.client.get("/cex/candle/symbols", Some(parameters))
93    }
94
95    /// Retrieves a list of supported candle intervals.
96    pub fn intervals(&self) -> Result<Vec<String>> {
97        self.client.get("/cex/candle/intervals", None)
98    }
99}
100
101/// Implements the `Datamaxi` trait for `Candle`, providing methods
102/// to create new instances of `Candle` with or without a custom base URL.
103impl Datamaxi for Candle {
104    /// Creates a new `Candle` instance with the default base URL.
105    ///
106    /// # Parameters
107    /// - `api_key`: A `String` representing the API key used for authentication in API requests.
108    ///
109    /// # Returns
110    /// A new `Candle` instance configured with the default base URL and the provided `api_key`.
111    ///
112    /// # Example
113    /// ```rust
114    /// use crate::datamaxi::api::Datamaxi;
115    /// let candle = datamaxi::cex::Candle::new("my_api_key".to_string());
116    /// ```
117    fn new(api_key: String) -> Candle {
118        let config = Config {
119            base_url: None, // Default base URL will be used
120            api_key,        // Provided API key
121        };
122        Candle {
123            client: Client::new(config), // Create a new client with the given config
124        }
125    }
126
127    /// Creates a new `Candle` instance with a custom base URL.
128    ///
129    /// # Parameters
130    /// - `api_key`: A `String` representing the API key used for authentication in API requests.
131    /// - `base_url`: A `String` representing the custom base URL for API requests.
132    ///
133    /// # Returns
134    /// A new `Candle` instance configured with the specified `base_url` and `api_key`.
135    ///
136    /// # Example
137    /// ```rust
138    /// use crate::datamaxi::api::Datamaxi;
139    /// let candle = datamaxi::cex::Candle::new_with_base_url("my_api_key".to_string(), "https://custom-api.example.com".to_string());
140    /// ```
141    fn new_with_base_url(api_key: String, base_url: String) -> Candle {
142        let config = Config {
143            base_url: Some(base_url), // Use the provided custom base URL
144            api_key,                  // Provided API key
145        };
146        Candle {
147            client: Client::new(config), // Create a new client with the given config
148        }
149    }
150}