Skip to main content

babelforce_manager_sdk/resources/
dashboards.rs

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