bpx_api_client/routes/
trades.rs

1use bpx_api_types::trade::Trade;
2
3use crate::BpxClient;
4use crate::error::Result;
5
6const API_TRADES: &str = "/api/v1/trades";
7const API_TRADES_HISTORY: &str = "/api/v1/trades/history";
8
9impl BpxClient {
10    /// Fetches the most recent trades for a given symbol, with an optional limit.
11    pub async fn get_recent_trades(&self, symbol: &str, limit: Option<i16>) -> Result<Vec<Trade>> {
12        let mut url = self.base_url.join(API_TRADES)?;
13        {
14            let mut query = url.query_pairs_mut();
15            query.append_pair("symbol", symbol);
16            if let Some(limit) = limit {
17                query.append_pair("limit", &limit.to_string());
18            }
19        }
20        let res = self.get(url).await?;
21        res.json().await.map_err(Into::into)
22    }
23
24    /// Fetches historical trades for a given symbol, with optional limit and offset.
25    pub async fn get_historical_trades(
26        &self,
27        symbol: &str,
28        limit: Option<i64>,
29        offset: Option<i64>,
30    ) -> Result<Vec<Trade>> {
31        let mut url = self.base_url.join(API_TRADES_HISTORY)?;
32        {
33            let mut query = url.query_pairs_mut();
34            query.append_pair("symbol", symbol);
35            if let Some(limit) = limit {
36                query.append_pair("limit", &limit.to_string());
37            }
38            if let Some(offset) = offset {
39                query.append_pair("offset", &offset.to_string());
40            }
41        }
42        let res = self.get(url).await?;
43        res.json().await.map_err(Into::into)
44    }
45}