Skip to main content

babelforce_manager_sdk/resources/
dashboards.rs

1use std::sync::Arc;
2
3use crate::error::{map_manager_err, ManagerError};
4use crate::gen::manager::apis::configuration::Configuration;
5use crate::gen::manager::apis::dashboard_api;
6use crate::gen::manager::models;
7use crate::http::{collect_all, fetch_page, Page};
8use crate::retry::{with_retry, RetryPolicy};
9
10/// Reporting dashboards — `/api/v2/dashboards`, with per-dashboard user management.
11pub struct DashboardsResource {
12    pub(crate) cfg: Arc<Configuration>,
13    pub(crate) retry: RetryPolicy,
14}
15
16impl DashboardsResource {
17    /// List all dashboards (auto-paginated).
18    pub async fn list_all(&self) -> Result<Vec<models::Dashboard>, ManagerError> {
19        collect_all(&self.retry, map_manager_err, |page| async move {
20            let r = dashboard_api::list_dashboards(
21                self.cfg.as_ref(),
22                Some(page),
23                None,
24                None,
25                None,
26                None,
27                None,
28            )
29            .await?;
30            Ok((r.items, r.pagination.pages, r.pagination.current))
31        })
32        .await
33    }
34
35    /// List one page of dashboards.
36    pub async fn list_page(
37        &self,
38        page: i32,
39        per_page: Option<i32>,
40    ) -> Result<Page<models::Dashboard>, ManagerError> {
41        fetch_page(&self.retry, map_manager_err, || async move {
42            let r = dashboard_api::list_dashboards(
43                self.cfg.as_ref(),
44                Some(page),
45                per_page,
46                None,
47                None,
48                None,
49                None,
50            )
51            .await?;
52            Ok((
53                r.items,
54                r.pagination.pages,
55                r.pagination.current,
56                Some(r.pagination.total),
57            ))
58        })
59        .await
60    }
61
62    /// Create a dashboard.
63    pub async fn create(
64        &self,
65        body: models::DashboardCreateBody,
66    ) -> Result<models::DashboardItemResponse, ManagerError> {
67        with_retry(&self.retry, false, || {
68            dashboard_api::create_dashboard(self.cfg.as_ref(), body.clone())
69        })
70        .await
71        .map_err(map_manager_err)
72    }
73
74    /// Get a dashboard by id.
75    pub async fn get(&self, id: &str) -> Result<models::DashboardItemResponse, ManagerError> {
76        with_retry(&self.retry, true, || {
77            dashboard_api::get_dashboard(self.cfg.as_ref(), id)
78        })
79        .await
80        .map_err(map_manager_err)
81    }
82
83    /// Update a dashboard.
84    pub async fn update(
85        &self,
86        id: &str,
87        body: models::DashboardUpdateBody,
88    ) -> Result<models::DashboardItemResponse, ManagerError> {
89        with_retry(&self.retry, false, || {
90            dashboard_api::update_dashboard(self.cfg.as_ref(), id, body.clone())
91        })
92        .await
93        .map_err(map_manager_err)
94    }
95
96    /// Delete a dashboard.
97    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
98        with_retry(&self.retry, false, || {
99            dashboard_api::delete_dashboard(self.cfg.as_ref(), id)
100        })
101        .await
102        .map_err(map_manager_err)?;
103        Ok(())
104    }
105
106    /// List the users allowed to access a dashboard.
107    pub async fn list_users(
108        &self,
109        id: &str,
110    ) -> Result<models::DashboardUsersResponse, ManagerError> {
111        with_retry(&self.retry, true, || {
112            dashboard_api::list_dashboard_users(self.cfg.as_ref(), id)
113        })
114        .await
115        .map_err(map_manager_err)
116    }
117
118    /// Add an existing reporter user (by email) to a dashboard's allowed users.
119    pub async fn add_user(
120        &self,
121        id: &str,
122        email: &str,
123    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
124        let body = models::DashboardUserAddRequest {
125            email: email.to_string(),
126        };
127        with_retry(&self.retry, false, || {
128            dashboard_api::add_dashboard_user(self.cfg.as_ref(), id, body.clone())
129        })
130        .await
131        .map_err(map_manager_err)
132    }
133
134    /// Remove a user from a dashboard's allowed users.
135    pub async fn remove_user(
136        &self,
137        id: &str,
138        user_id: &str,
139    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
140        with_retry(&self.retry, false, || {
141            dashboard_api::remove_dashboard_user(self.cfg.as_ref(), id, user_id)
142        })
143        .await
144        .map_err(map_manager_err)
145    }
146}