Skip to main content

gateio_rs/api/spot/
get_ticker.rs

1use crate::http::{Credentials, Method, request::Request};
2
3/// Request builder for retrieving ticker information.
4///
5/// Gets 24hr trading statistics for currency pairs including price, volume,
6/// and percentage changes. Can be used for a specific pair or all pairs.
7///
8/// # API Endpoint
9/// `GET /api/v4/spot/tickers`
10///
11/// # Examples
12///
13/// ```rust,no_run
14/// use gateio_rs::{api::spot::get_ticker, ureq::GateHttpClient};
15///
16/// let client = GateHttpClient::default();
17///
18/// // Get ticker for specific pair
19/// let request = get_ticker()
20///     .currency_pair("BTC_USDT")
21///     .timezone("utc8");
22/// let response = client.send(request)?;
23///
24/// // Get all tickers
25/// let request = get_ticker();
26/// let response = client.send(request)?;
27/// # Ok::<(), Box<dyn std::error::Error>>(()).expect("");
28/// ```
29///
30/// # Response Format
31/// Returns ticker data with fields like:
32/// - `currency_pair`: Trading pair name
33/// - `last`: Last traded price
34/// - `lowest_ask`: Lowest ask price
35/// - `highest_bid`: Highest bid price
36/// - `change_percentage`: 24hr price change percentage
37/// - `base_volume`: 24hr base currency volume
38/// - `quote_volume`: 24hr quote currency volume
39/// - `high_24h`: 24hr highest price
40/// - `low_24h`: 24hr lowest price
41pub struct GetTicker {
42    /// Optional currency pair to get ticker for (if not specified, returns all tickers)
43    pub currency_pair: Option<String>,
44    /// Timezone for the ticker data timestamps
45    pub timezone: Option<String>,
46    /// API credentials for authentication (optional for public data)
47    pub credentials: Option<Credentials>,
48}
49
50impl GetTicker {
51    /// Creates a new GetTicker request
52    pub fn new() -> Self {
53        Self {
54            currency_pair: None,
55            timezone: None,
56            credentials: None,
57        }
58    }
59
60    /// Sets the currency pair to get ticker for
61    pub fn currency_pair(mut self, s: &str) -> Self {
62        self.currency_pair = Some(s.into());
63        self
64    }
65
66    /// Sets the timezone for timestamp data
67    pub fn timezone(mut self, tz: &str) -> Self {
68        self.timezone = Some(tz.into());
69        self
70    }
71
72    /// Sets the API credentials for authentication
73    pub fn credentials(mut self, creds: Credentials) -> Self {
74        self.credentials = Some(creds);
75        self
76    }
77}
78
79impl From<GetTicker> for Request {
80    fn from(g: GetTicker) -> Request {
81        let mut params = Vec::new();
82        if let Some(s) = g.currency_pair {
83            params.push(("currency_pair".into(), s));
84        }
85        if let Some(tz) = g.timezone {
86            params.push(("timezone".into(), tz));
87        }
88
89        Request {
90            method: Method::Get,
91            path: "/api/v4/spot/tickers".into(),
92            params,
93            payload: "".to_string(),
94            x_gate_exp_time: None,
95            credentials: g.credentials,
96            sign: false,
97        }
98    }
99}