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