Skip to main content

gateio_rs/api/spot/
get_candlesticks.rs

1use crate::http::{Credentials, Method, request::Request};
2
3/// Request for retrieving candlestick/kline data for a currency pair
4pub struct GetCandlesticks {
5    /// Currency pair to get candlestick data for
6    pub currency_pair: String,
7    /// Maximum number of candlesticks to return
8    pub limit: Option<i64>,
9    /// Start timestamp for candlestick data range
10    pub from: Option<i64>,
11    /// End timestamp for candlestick data range
12    pub to: Option<i64>,
13    /// Time interval for candlesticks (e.g., "1m", "5m", "1h")
14    pub interval: Option<String>,
15    /// API credentials for authentication (optional for public data)
16    pub credentials: Option<Credentials>,
17}
18
19impl GetCandlesticks {
20    /// Creates a new GetCandlesticks request for the specified currency pair
21    pub fn new(currency_pair: &str) -> Self {
22        Self {
23            currency_pair: currency_pair.to_owned(),
24            limit: None,
25            from: None,
26            to: None,
27            interval: None,
28            credentials: None,
29        }
30    }
31
32    /// Sets the maximum number of candlesticks to return
33    pub fn limit(mut self, limit: i64) -> Self {
34        self.limit = Some(limit.into());
35        self
36    }
37
38    /// Sets the start timestamp for the data range
39    pub fn from(mut self, from: i64) -> Self {
40        self.from = Some(from.into());
41        self
42    }
43
44    /// Sets the end timestamp for the data range
45    pub fn to(mut self, to: i64) -> Self {
46        self.to = Some(to.into());
47        self
48    }
49
50    /// Sets the time interval for candlesticks
51    pub fn interval(mut self, interval: &str) -> Self {
52        self.interval = Some(interval.into());
53        self
54    }
55
56    /// Sets the API credentials for authentication
57    pub fn credentials(mut self, creds: Credentials) -> Self {
58        self.credentials = Some(creds);
59        self
60    }
61}
62
63impl From<GetCandlesticks> for Request {
64    fn from(request: GetCandlesticks) -> Request {
65        let mut params = vec![("currency_pair".to_owned(), request.currency_pair)];
66
67        if let Some(limit) = request.limit {
68            params.push(("limit".into(), limit.to_string()));
69        }
70
71        if let Some(from) = request.from {
72            params.push(("from".into(), from.to_string()));
73        }
74
75        if let Some(to) = request.to {
76            params.push(("to".into(), to.to_string()));
77        }
78
79        if let Some(interval) = request.interval {
80            params.push(("interval".into(), interval.to_string()));
81        }
82
83        Request {
84            method: Method::Get,
85            path: "/api/v4/spot/candlesticks".into(),
86            params,
87            payload: "".to_string(),
88            x_gate_exp_time: None,
89            credentials: request.credentials,
90            sign: false,
91        }
92    }
93}