Skip to main content

babelforce_manager_sdk/resources/
routing.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::routing_api as api;
6use crate::gen::manager::models;
7use crate::http::collect_all;
8use crate::retry::{with_retry, RetryPolicy};
9
10/// Routing rules — `/api/v2/routings`.
11pub struct RoutingResource {
12    pub(crate) cfg: Arc<Configuration>,
13    pub(crate) retry: RetryPolicy,
14}
15
16impl RoutingResource {
17    /// List all routings (auto-paginated).
18    pub async fn list_all(&self) -> Result<Vec<models::Routing>, ManagerError> {
19        collect_all(&self.retry, map_manager_err, |page| async move {
20            let r = api::list_routings(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 routing.
27    pub async fn create(
28        &self,
29        body: models::RestCreateRouting,
30    ) -> Result<models::RoutingItemResponse, ManagerError> {
31        with_retry(&self.retry, false, || {
32            api::create_routing(self.cfg.as_ref(), body.clone())
33        })
34        .await
35        .map_err(map_manager_err)
36    }
37
38    /// Get a routing by id.
39    pub async fn get(&self, id: &str) -> Result<models::RoutingItemResponse, ManagerError> {
40        with_retry(&self.retry, true, || {
41            api::get_routing(self.cfg.as_ref(), id)
42        })
43        .await
44        .map_err(map_manager_err)
45    }
46
47    /// Update a routing.
48    pub async fn update(
49        &self,
50        id: &str,
51        body: models::RestUpdateRouting,
52    ) -> Result<models::RoutingItemResponse, ManagerError> {
53        with_retry(&self.retry, false, || {
54            api::update_routing(self.cfg.as_ref(), id, body.clone())
55        })
56        .await
57        .map_err(map_manager_err)
58    }
59
60    /// Delete a routing.
61    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
62        with_retry(&self.retry, false, || {
63            api::delete_routing(self.cfg.as_ref(), id)
64        })
65        .await
66        .map_err(map_manager_err)?;
67        Ok(())
68    }
69}