Skip to main content

rust_okx/api/market/
requests.rs

1use std::borrow::Cow;
2
3use serde::Serialize;
4
5use crate::model::InstType;
6
7/// Request for [`get_ticker`](crate::api::market::Market::get_ticker) and
8/// [`get_block_ticker`](crate::api::market::Market::get_block_ticker).
9#[derive(Debug, Clone, Serialize)]
10pub struct InstIdRequest<'a> {
11    #[serde(rename = "instId")]
12    inst_id: Cow<'a, str>,
13}
14
15impl<'a> InstIdRequest<'a> {
16    /// Create a query for one instrument ID.
17    pub fn new(inst_id: impl Into<Cow<'a, str>>) -> Self {
18        Self {
19            inst_id: inst_id.into(),
20        }
21    }
22}
23
24/// Request for [`get_tickers`](crate::api::market::Market::get_tickers) and
25/// [`get_block_tickers`](crate::api::market::Market::get_block_tickers).
26#[derive(Debug, Clone, Serialize)]
27pub struct TickersRequest<'a> {
28    #[serde(rename = "instType")]
29    inst_type: InstType,
30    #[serde(rename = "instFamily", skip_serializing_if = "Option::is_none")]
31    inst_family: Option<Cow<'a, str>>,
32}
33
34impl<'a> TickersRequest<'a> {
35    /// Create a tickers query for an instrument type.
36    pub fn new(inst_type: InstType) -> Self {
37        Self {
38            inst_type,
39            inst_family: None,
40        }
41    }
42
43    /// Set the instrument family filter.
44    pub fn inst_family(mut self, inst_family: impl Into<Cow<'a, str>>) -> Self {
45        self.inst_family = Some(inst_family.into());
46        self
47    }
48}
49
50/// Request for [`get_index_tickers`](crate::api::market::Market::get_index_tickers).
51#[derive(Debug, Clone, Default, Serialize)]
52pub struct IndexTickersRequest<'a> {
53    #[serde(rename = "quoteCcy", skip_serializing_if = "Option::is_none")]
54    quote_ccy: Option<Cow<'a, str>>,
55    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
56    inst_id: Option<Cow<'a, str>>,
57}
58
59impl<'a> IndexTickersRequest<'a> {
60    /// Create an empty index-tickers query.
61    pub fn new() -> Self {
62        Self::default()
63    }
64
65    /// Set the quote currency filter.
66    pub fn quote_currency(mut self, quote_ccy: impl Into<Cow<'a, str>>) -> Self {
67        self.quote_ccy = Some(quote_ccy.into());
68        self
69    }
70
71    /// Set the index instrument ID filter.
72    pub fn inst_id(mut self, inst_id: impl Into<Cow<'a, str>>) -> Self {
73        self.inst_id = Some(inst_id.into());
74        self
75    }
76}
77
78/// Request for [`get_orderbook`](crate::api::market::Market::get_orderbook).
79#[derive(Debug, Clone, Serialize)]
80pub struct OrderBookRequest<'a> {
81    #[serde(rename = "instId")]
82    inst_id: Cow<'a, str>,
83    #[serde(skip_serializing_if = "Option::is_none")]
84    sz: Option<u32>,
85}
86
87impl<'a> OrderBookRequest<'a> {
88    /// Create an order-book query for an instrument.
89    pub fn new(inst_id: impl Into<Cow<'a, str>>) -> Self {
90        Self {
91            inst_id: inst_id.into(),
92            sz: None,
93        }
94    }
95
96    /// Set the depth, i.e. the number of price levels per side.
97    pub fn size(mut self, sz: u32) -> Self {
98        self.sz = Some(sz);
99        self
100    }
101}
102
103/// Request for [`get_candlesticks`](crate::api::market::Market::get_candlesticks).
104#[derive(Debug, Clone, Serialize)]
105pub struct CandlesRequest<'a> {
106    #[serde(rename = "instId")]
107    inst_id: Cow<'a, str>,
108    #[serde(skip_serializing_if = "Option::is_none")]
109    bar: Option<Cow<'a, str>>,
110    #[serde(skip_serializing_if = "Option::is_none")]
111    limit: Option<u32>,
112}
113
114impl<'a> CandlesRequest<'a> {
115    /// Create a candlestick query for an instrument.
116    pub fn new(inst_id: impl Into<Cow<'a, str>>) -> Self {
117        Self {
118            inst_id: inst_id.into(),
119            bar: None,
120            limit: None,
121        }
122    }
123
124    /// Set the bar size, e.g. `1m`, `1H`, or `1D`.
125    pub fn bar(mut self, bar: impl Into<Cow<'a, str>>) -> Self {
126        self.bar = Some(bar.into());
127        self
128    }
129
130    /// Set the maximum number of rows to return.
131    pub fn limit(mut self, limit: u32) -> Self {
132        self.limit = Some(limit);
133        self
134    }
135}
136
137/// Request for [`get_trades`](crate::api::market::Market::get_trades).
138#[derive(Debug, Clone, Serialize)]
139pub struct TradesRequest<'a> {
140    #[serde(rename = "instId")]
141    inst_id: Cow<'a, str>,
142    #[serde(skip_serializing_if = "Option::is_none")]
143    limit: Option<u32>,
144}
145
146impl<'a> TradesRequest<'a> {
147    /// Create a recent-trades query for an instrument.
148    pub fn new(inst_id: impl Into<Cow<'a, str>>) -> Self {
149        Self {
150            inst_id: inst_id.into(),
151            limit: None,
152        }
153    }
154
155    /// Set the maximum number of rows to return.
156    pub fn limit(mut self, limit: u32) -> Self {
157        self.limit = Some(limit);
158        self
159    }
160}
161
162/// Request for [`get_option_instrument_family_trades`](crate::api::market::Market::get_option_instrument_family_trades).
163#[derive(Debug, Clone, Serialize)]
164pub struct InstFamilyRequest<'a> {
165    #[serde(rename = "instFamily")]
166    inst_family: Cow<'a, str>,
167}
168
169impl<'a> InstFamilyRequest<'a> {
170    /// Create a query for an instrument family.
171    pub fn new(inst_family: impl Into<Cow<'a, str>>) -> Self {
172        Self {
173            inst_family: inst_family.into(),
174        }
175    }
176}
177
178/// Request for [`get_index_components`](crate::api::market::Market::get_index_components).
179#[derive(Debug, Clone, Serialize)]
180pub struct IndexRequest<'a> {
181    index: Cow<'a, str>,
182}
183
184impl<'a> IndexRequest<'a> {
185    /// Create a query for an index symbol.
186    pub fn new(index: impl Into<Cow<'a, str>>) -> Self {
187        Self {
188            index: index.into(),
189        }
190    }
191}
192
193/// Query parameters for historical/index/mark-price candlestick endpoints.
194#[derive(Debug, Clone, Serialize)]
195pub struct CandlesticksRequest<'a> {
196    #[serde(rename = "instId")]
197    inst_id: Cow<'a, str>,
198    #[serde(skip_serializing_if = "Option::is_none")]
199    after: Option<Cow<'a, str>>,
200    #[serde(skip_serializing_if = "Option::is_none")]
201    before: Option<Cow<'a, str>>,
202    #[serde(skip_serializing_if = "Option::is_none")]
203    bar: Option<Cow<'a, str>>,
204    #[serde(skip_serializing_if = "Option::is_none")]
205    limit: Option<u32>,
206}
207
208impl<'a> CandlesticksRequest<'a> {
209    /// Create a candlestick query for an instrument.
210    pub fn new(inst_id: impl Into<Cow<'a, str>>) -> Self {
211        Self {
212            inst_id: inst_id.into(),
213            after: None,
214            before: None,
215            bar: None,
216            limit: None,
217        }
218    }
219
220    /// Return records after this pagination cursor.
221    pub fn after(mut self, after: impl Into<Cow<'a, str>>) -> Self {
222        self.after = Some(after.into());
223        self
224    }
225
226    /// Return records before this pagination cursor.
227    pub fn before(mut self, before: impl Into<Cow<'a, str>>) -> Self {
228        self.before = Some(before.into());
229        self
230    }
231
232    /// Set the bar size, e.g. `1m`, `1H`, or `1D`.
233    pub fn bar(mut self, bar: impl Into<Cow<'a, str>>) -> Self {
234        self.bar = Some(bar.into());
235        self
236    }
237
238    /// Set the maximum number of rows to return.
239    pub fn limit(mut self, limit: u32) -> Self {
240        self.limit = Some(limit);
241        self
242    }
243}
244
245/// Query parameters for historical trades.
246#[derive(Debug, Clone, Serialize)]
247pub struct HistoryTradesRequest<'a> {
248    #[serde(rename = "instId")]
249    inst_id: Cow<'a, str>,
250    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
251    trade_type: Option<Cow<'a, str>>,
252    #[serde(skip_serializing_if = "Option::is_none")]
253    after: Option<Cow<'a, str>>,
254    #[serde(skip_serializing_if = "Option::is_none")]
255    before: Option<Cow<'a, str>>,
256    #[serde(skip_serializing_if = "Option::is_none")]
257    limit: Option<u32>,
258}
259
260impl<'a> HistoryTradesRequest<'a> {
261    /// Create a historical trades query for an instrument.
262    pub fn new(inst_id: impl Into<Cow<'a, str>>) -> Self {
263        Self {
264            inst_id: inst_id.into(),
265            trade_type: None,
266            after: None,
267            before: None,
268            limit: None,
269        }
270    }
271
272    /// Set the OKX trade type filter.
273    pub fn trade_type(mut self, trade_type: impl Into<Cow<'a, str>>) -> Self {
274        self.trade_type = Some(trade_type.into());
275        self
276    }
277
278    /// Return records after this pagination cursor.
279    pub fn after(mut self, after: impl Into<Cow<'a, str>>) -> Self {
280        self.after = Some(after.into());
281        self
282    }
283
284    /// Return records before this pagination cursor.
285    pub fn before(mut self, before: impl Into<Cow<'a, str>>) -> Self {
286        self.before = Some(before.into());
287        self
288    }
289
290    /// Set the maximum number of rows to return.
291    pub fn limit(mut self, limit: u32) -> Self {
292        self.limit = Some(limit);
293        self
294    }
295}