Skip to main content

babelforce_manager_sdk/resources/
calendars.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::{calendar_api as api, manager_api};
6use crate::gen::manager::models;
7use crate::http::collect_all;
8use crate::retry::{with_retry, RetryPolicy};
9
10/// Calendars — `/api/v2/calendars`.
11pub struct CalendarsResource {
12    pub(crate) cfg: Arc<Configuration>,
13    pub(crate) retry: RetryPolicy,
14}
15
16impl CalendarsResource {
17    /// List all calendars (auto-paginated).
18    pub async fn list_all(&self) -> Result<Vec<models::Calendar>, ManagerError> {
19        collect_all(&self.retry, map_manager_err, |page| async move {
20            let r = api::list_calendars(self.cfg.as_ref(), Some(page), None).await?;
21            Ok((r.items, r.pagination.pages, r.pagination.current))
22        })
23        .await
24    }
25
26    /// Create a calendar.
27    pub async fn create(
28        &self,
29        body: models::RestCreateCalendar,
30    ) -> Result<models::CalendarItemResponse, ManagerError> {
31        with_retry(&self.retry, false, || {
32            api::create_calendar(self.cfg.as_ref(), body.clone())
33        })
34        .await
35        .map_err(map_manager_err)
36    }
37
38    /// Get a calendar by id.
39    pub async fn get(&self, id: &str) -> Result<models::CalendarItemResponse, ManagerError> {
40        with_retry(&self.retry, true, || {
41            api::get_calendar(self.cfg.as_ref(), id)
42        })
43        .await
44        .map_err(map_manager_err)
45    }
46
47    /// Update a calendar.
48    pub async fn update(
49        &self,
50        id: &str,
51        body: models::RestUpdateCalendar,
52    ) -> Result<models::CalendarItemResponse, ManagerError> {
53        with_retry(&self.retry, false, || {
54            api::update_calendar(self.cfg.as_ref(), id, body.clone())
55        })
56        .await
57        .map_err(map_manager_err)
58    }
59
60    /// Delete a calendar.
61    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
62        with_retry(&self.retry, false, || {
63            api::delete_calendar(self.cfg.as_ref(), id)
64        })
65        .await
66        .map_err(map_manager_err)?;
67        Ok(())
68    }
69
70    /// Get the dates configured on a calendar.
71    pub async fn get_dates(
72        &self,
73        id: &str,
74    ) -> Result<models::CalendarDateListResponse, ManagerError> {
75        with_retry(&self.retry, true, || {
76            manager_api::get_calender_dates(self.cfg.as_ref(), id)
77        })
78        .await
79        .map_err(map_manager_err)
80    }
81
82    /// Add a date to a calendar.
83    pub async fn add_date(
84        &self,
85        id: &str,
86        body: models::CalendarDateBody,
87    ) -> Result<models::CalendarDateItemResponse, ManagerError> {
88        with_retry(&self.retry, false, || {
89            manager_api::add_calendar_date(self.cfg.as_ref(), id, body.clone())
90        })
91        .await
92        .map_err(map_manager_err)
93    }
94
95    /// Get a single date from a calendar.
96    pub async fn get_date(
97        &self,
98        id: &str,
99        date_id: &str,
100    ) -> Result<models::CalendarDateItemResponse, ManagerError> {
101        with_retry(&self.retry, true, || {
102            api::get_calendar_date(self.cfg.as_ref(), id, date_id)
103        })
104        .await
105        .map_err(map_manager_err)
106    }
107
108    /// Update a date on a calendar.
109    pub async fn update_date(
110        &self,
111        id: &str,
112        date_id: &str,
113        body: models::CalendarDateBody,
114    ) -> Result<models::CalendarDateItemResponse, ManagerError> {
115        with_retry(&self.retry, false, || {
116            api::update_calendar_date(self.cfg.as_ref(), id, date_id, body.clone())
117        })
118        .await
119        .map_err(map_manager_err)
120    }
121
122    /// Remove a date from a calendar.
123    pub async fn remove_date(
124        &self,
125        id: &str,
126        date_id: &str,
127    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
128        with_retry(&self.retry, false, || {
129            api::remove_calendar_date(self.cfg.as_ref(), id, date_id)
130        })
131        .await
132        .map_err(map_manager_err)
133    }
134
135    /// Test whether a date is a holiday across the account's calendars.
136    pub async fn test_date(
137        &self,
138        date: Option<&str>,
139    ) -> Result<models::GenericItemResponse, ManagerError> {
140        with_retry(&self.retry, true, || {
141            api::test_calendar_date(self.cfg.as_ref(), date)
142        })
143        .await
144        .map_err(map_manager_err)
145    }
146
147    /// Bulk-delete calendars by id.
148    pub async fn bulk_delete(
149        &self,
150        ids: Vec<String>,
151    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
152        let ids = ids
153            .iter()
154            .map(|id| {
155                uuid::Uuid::parse_str(id)
156                    .map_err(|e| ManagerError::InvalidArgument(format!("ids: {e}")))
157            })
158            .collect::<Result<Vec<_>, _>>()?;
159        let body = models::BulkIdsRequest { ids };
160        with_retry(&self.retry, false, || {
161            api::bulk_delete_calendars(self.cfg.as_ref(), body.clone())
162        })
163        .await
164        .map_err(map_manager_err)
165    }
166
167    /// Bulk-update calendars.
168    pub async fn bulk_update(
169        &self,
170        body: models::CalendarBulkUpdateRequest,
171    ) -> Result<models::CalendarListResponse, ManagerError> {
172        with_retry(&self.retry, false, || {
173            api::bulk_update_calendars(self.cfg.as_ref(), body.clone())
174        })
175        .await
176        .map_err(map_manager_err)
177    }
178}