renegade_sdk/renegade_wallet_client/actions/
get_tasks.rs

1//! Gets the wallet's task history from the historical state engine
2
3use renegade_external_api::{
4    http::task::{GET_TASKS_ROUTE, GetTasksResponse},
5    types::ApiTask,
6};
7
8use crate::{
9    RenegadeClientError,
10    actions::{INCLUDE_HISTORIC_TASKS_PARAM, PAGE_TOKEN_PARAM, construct_http_path},
11    client::RenegadeClient,
12};
13
14// --- Public Actions --- //
15impl RenegadeClient {
16    /// Fetches all tasks in the account, optionally including historic tasks.
17    ///
18    /// This method will paginate through all of the account's tasks across
19    /// multiple requests, returning them all.
20    pub async fn get_tasks(
21        &self,
22        include_historic_tasks: bool,
23    ) -> Result<Vec<ApiTask>, RenegadeClientError> {
24        let path = self.build_get_tasks_request_path(include_historic_tasks, None)?;
25
26        let GetTasksResponse { mut tasks, mut next_page_token } =
27            self.relayer_client.get(&path).await?;
28
29        while let Some(page_token) = next_page_token {
30            let path =
31                self.build_get_tasks_request_path(include_historic_tasks, Some(page_token))?;
32
33            let response: GetTasksResponse = self.relayer_client.get(&path).await?;
34
35            tasks.extend(response.tasks);
36            next_page_token = response.next_page_token;
37        }
38
39        Ok(tasks)
40    }
41}
42
43// --- Private Helpers --- //
44impl RenegadeClient {
45    /// Builds the request path for the get tasks endpoint
46    fn build_get_tasks_request_path(
47        &self,
48        include_historic_tasks: bool,
49        page_token: Option<i64>,
50    ) -> Result<String, RenegadeClientError> {
51        let path = construct_http_path!(GET_TASKS_ROUTE, "account_id" => self.get_account_id());
52
53        let mut params = vec![(INCLUDE_HISTORIC_TASKS_PARAM, include_historic_tasks.to_string())];
54        if let Some(token) = page_token {
55            params.push((PAGE_TOKEN_PARAM, token.to_string()));
56        }
57
58        let query_string =
59            serde_urlencoded::to_string(&params).map_err(RenegadeClientError::serde)?;
60
61        Ok(format!("{path}?{query_string}"))
62    }
63}