bpx_api_client/routes/
order.rs

1use bpx_api_types::order::{
2    CancelOpenOrdersPayload, CancelOrderPayload, ExecuteOrderPayload, Order,
3};
4
5use crate::BpxClient;
6use crate::error::{Error, Result};
7
8#[doc(hidden)]
9pub const API_ORDER: &str = "/api/v1/order";
10#[doc(hidden)]
11pub const API_ORDERS: &str = "/api/v1/orders";
12
13impl BpxClient {
14    /// Fetches a specific open order by symbol and either order ID or client ID.
15    pub async fn get_open_order(
16        &self,
17        symbol: &str,
18        order_id: Option<&str>,
19        client_id: Option<u32>,
20    ) -> Result<Order> {
21        let mut url = format!("{}{}?symbol={}", self.base_url, API_ORDER, symbol);
22        if let Some(order_id) = order_id {
23            url.push_str(&format!("&orderId={order_id}"));
24        } else {
25            url.push_str(&format!(
26                "&clientId={}",
27                client_id.ok_or_else(|| Error::InvalidRequest(
28                    "either order_id or client_id is required".into()
29                ))?
30            ));
31        }
32        let res = self.get(url).await?;
33        res.json().await.map_err(Into::into)
34    }
35
36    /// Executes a new order with the given payload.
37    pub async fn execute_order(&self, payload: ExecuteOrderPayload) -> Result<Order> {
38        let endpoint = format!("{}{}", self.base_url, API_ORDER);
39        let res = self.post(endpoint, payload).await?;
40        res.json().await.map_err(Into::into)
41    }
42
43    /// Cancels a specific order by symbol and either order ID or client ID.
44    pub async fn cancel_order(
45        &self,
46        symbol: &str,
47        order_id: Option<&str>,
48        client_id: Option<u32>,
49    ) -> Result<Order> {
50        let url = format!("{}{}", self.base_url, API_ORDER);
51        let payload = CancelOrderPayload {
52            symbol: symbol.to_string(),
53            order_id: order_id.map(|s| s.to_string()),
54            client_id,
55        };
56
57        let res = self.delete(url, payload).await?;
58        res.json().await.map_err(Into::into)
59    }
60
61    /// Retrieves all open orders, optionally filtered by symbol.
62    pub async fn get_open_orders(&self, symbol: Option<&str>) -> Result<Vec<Order>> {
63        let mut url = format!("{}{}", self.base_url, API_ORDERS);
64        if let Some(s) = symbol {
65            url.push_str(&format!("?symbol={s}"));
66        }
67        let res = self.get(url).await?;
68        res.json().await.map_err(Into::into)
69    }
70
71    /// Cancels all open orders matching the specified payload.
72    pub async fn cancel_open_orders(&self, payload: CancelOpenOrdersPayload) -> Result<Vec<Order>> {
73        let url = format!("{}{}", self.base_url, API_ORDERS);
74        let res = self.delete(url, payload).await?;
75        res.json().await.map_err(Into::into)
76    }
77}