Skip to main content

gateio_rs/api/spot/
get_orderbook.rs

1use crate::http::{Credentials, Method, request::Request};
2
3/// Request for retrieving order book data for a currency pair
4pub struct GetOrderbook {
5    /// Currency pair to get order book for
6    pub currency_pair: String,
7    /// Price interval aggregation ("0" for no aggregation)
8    pub interval: Option<String>,
9    /// Maximum depth of order book entries to return
10    pub limit: Option<i64>,
11    /// Whether to return order IDs with the book data
12    pub with_id: Option<bool>,
13    /// API credentials for authentication (optional for public data)
14    pub credentials: Option<Credentials>,
15}
16
17impl GetOrderbook {
18    /// Creates a new GetOrderbook request for the specified currency pair
19    pub fn new(currency_pair: &str) -> Self {
20        Self {
21            currency_pair: currency_pair.to_owned(),
22            interval: None,
23            limit: None,
24            with_id: None,
25            credentials: None,
26        }
27    }
28
29    /// Sets the price interval for aggregation
30    pub fn interval(mut self, interval: &str) -> Self {
31        self.interval = Some(interval.into());
32        self
33    }
34
35    /// Sets the maximum depth of order book entries
36    pub fn limit(mut self, limit: i64) -> Self {
37        self.limit = Some(limit.into());
38        self
39    }
40
41    /// Sets whether to include order IDs in the response
42    pub fn with_id(mut self, with_id: bool) -> Self {
43        self.with_id = Some(with_id);
44        self
45    }
46
47    /// Sets the API credentials for authentication
48    pub fn credentials(mut self, creds: Credentials) -> Self {
49        self.credentials = Some(creds);
50        self
51    }
52}
53
54impl From<GetOrderbook> for Request {
55    fn from(request: GetOrderbook) -> Request {
56        let mut params = vec![("currency_pair".to_owned(), request.currency_pair)];
57
58        if let Some(interval) = request.interval {
59            params.push(("interval".into(), interval.to_string()));
60        }
61
62        if let Some(limit) = request.limit {
63            params.push(("limit".into(), limit.to_string()));
64        }
65
66        if let Some(with_id) = request.with_id {
67            params.push(("with_id".into(), with_id.to_string()));
68        }
69
70        Request {
71            method: Method::Get,
72            path: "/api/v4/spot/order_book".into(),
73            params,
74            payload: "".to_string(),
75            x_gate_exp_time: None,
76            credentials: request.credentials,
77            sign: false,
78        }
79    }
80}