renegade_sdk/renegade_wallet_client/actions/
admin_get_account_orders.rs

1//! Fetches all orders for a given account (admin)
2
3use renegade_external_api::http::admin::ADMIN_GET_ACCOUNT_ORDERS_ROUTE;
4use renegade_external_api::types::{ApiAdminOrder, GetOrdersAdminResponse};
5use uuid::Uuid;
6
7use crate::{
8    RenegadeClientError,
9    actions::{PAGE_TOKEN_PARAM, construct_http_path},
10    client::RenegadeClient,
11};
12
13// --- Public Actions --- //
14impl RenegadeClient {
15    /// Fetches all orders for the given account (admin).
16    ///
17    /// This method will paginate through all of the account's orders across
18    /// multiple requests, returning them all.
19    pub async fn admin_get_account_orders(
20        &self,
21        account_id: Uuid,
22    ) -> Result<Vec<ApiAdminOrder>, RenegadeClientError> {
23        let admin_relayer_client = self.get_admin_client()?;
24
25        let path = Self::build_admin_get_account_orders_request_path(account_id, None)?;
26
27        let GetOrdersAdminResponse { mut orders, mut next_page_token } =
28            admin_relayer_client.get(&path).await?;
29
30        while let Some(page_token) = next_page_token {
31            let path =
32                Self::build_admin_get_account_orders_request_path(account_id, Some(page_token))?;
33
34            let response: GetOrdersAdminResponse = admin_relayer_client.get(&path).await?;
35
36            orders.extend(response.orders);
37            next_page_token = response.next_page_token;
38        }
39
40        Ok(orders)
41    }
42}
43
44// --- Private Helpers --- //
45impl RenegadeClient {
46    /// Builds the request path for the admin get account orders endpoint
47    fn build_admin_get_account_orders_request_path(
48        account_id: Uuid,
49        page_token: Option<i64>,
50    ) -> Result<String, RenegadeClientError> {
51        let path = construct_http_path!(ADMIN_GET_ACCOUNT_ORDERS_ROUTE, "account_id" => account_id);
52
53        if let Some(token) = page_token {
54            let query_string =
55                serde_urlencoded::to_string(&[(PAGE_TOKEN_PARAM, token.to_string())])
56                    .map_err(RenegadeClientError::serde)?;
57            Ok(format!("{path}?{query_string}"))
58        } else {
59            Ok(path)
60        }
61    }
62}