Skip to main content

babelforce_manager_sdk/resources/
dialer.rs

1use crate::error::{map_manager_err, ManagerError};
2use crate::gen::manager::apis::configuration::Configuration;
3use crate::gen::manager::apis::{dialer_api, outbound_api, reporting_call_api};
4use crate::gen::manager::models;
5use crate::http::collect_all;
6use crate::retry::{with_retry, RetryPolicy};
7use crate::token::SharedCfg;
8
9/// Dialer runtime & reporting — `/api/v2/dialer`, with a nested `behaviours` sub-resource.
10pub struct DialerResource {
11    pub(crate) cfg: SharedCfg<Configuration>,
12    pub(crate) retry: RetryPolicy,
13    /// Dialer behaviours — `/api/v2/outbound/dialer-behaviours`.
14    pub behaviours: DialerBehavioursResource,
15}
16
17impl DialerResource {
18    /// Get inbound dialer runtime information — `/api/v2/dialer`.
19    pub async fn info(&self) -> Result<models::GenericItemResponse, ManagerError> {
20        let cfg = self.cfg.get().await?;
21        let cfg = cfg.as_ref();
22        with_retry(&self.retry, true, || dialer_api::get_dialer_info(cfg))
23            .await
24            .map_err(map_manager_err)
25    }
26
27    /// Flush dialer tasks — by `id`, or `all`.
28    pub async fn flush(
29        &self,
30        id: Option<&str>,
31        all: Option<bool>,
32    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
33        let cfg = self.cfg.get().await?;
34        let cfg = cfg.as_ref();
35        with_retry(&self.retry, false, || {
36            dialer_api::flush_dialer(cfg, id, all)
37        })
38        .await
39        .map_err(map_manager_err)
40    }
41
42    /// List all dialer simple reporting calls (auto-paginated).
43    pub async fn simple_reporting(&self) -> Result<Vec<models::ReportingCall>, ManagerError> {
44        let cfg = self.cfg.get().await?;
45        let cfg = cfg.as_ref();
46        collect_all(&self.retry, map_manager_err, |page| async move {
47            let r = reporting_call_api::list_dialer_simple_reporting_calls(cfg, Some(page), None)
48                .await?;
49            Ok((r.items, r.pagination.pages, r.pagination.current))
50        })
51        .await
52    }
53}
54
55/// Dialer behaviours — `/api/v2/outbound/dialer-behaviours`.
56pub struct DialerBehavioursResource {
57    pub(crate) cfg: SharedCfg<Configuration>,
58    pub(crate) retry: RetryPolicy,
59}
60
61impl DialerBehavioursResource {
62    /// List all dialer behaviours (auto-paginated).
63    pub async fn list_all(&self) -> Result<Vec<models::DialerBehaviour>, ManagerError> {
64        let cfg = self.cfg.get().await?;
65        let cfg = cfg.as_ref();
66        collect_all(&self.retry, map_manager_err, |page| async move {
67            let r = outbound_api::list_dialer_behaviours(cfg, Some(page), None).await?;
68            Ok((r.items, r.pagination.pages, r.pagination.current))
69        })
70        .await
71    }
72
73    /// Create a dialer behaviour.
74    pub async fn create(
75        &self,
76        body: models::DialerBehaviourWriteBody,
77    ) -> Result<models::DialerBehaviourItemResponse, ManagerError> {
78        let cfg = self.cfg.get().await?;
79        let cfg = cfg.as_ref();
80        with_retry(&self.retry, false, || {
81            outbound_api::create_dialer_behaviour(cfg, body.clone())
82        })
83        .await
84        .map_err(map_manager_err)
85    }
86
87    /// Get a dialer behaviour by id.
88    pub async fn get(&self, id: &str) -> Result<models::DialerBehaviourItemResponse, ManagerError> {
89        let cfg = self.cfg.get().await?;
90        let cfg = cfg.as_ref();
91        with_retry(&self.retry, true, || {
92            outbound_api::get_dialer_behaviour(cfg, id)
93        })
94        .await
95        .map_err(map_manager_err)
96    }
97
98    /// Update a dialer behaviour.
99    pub async fn update(
100        &self,
101        id: &str,
102        body: models::DialerBehaviourWriteBody,
103    ) -> Result<models::DialerBehaviourItemResponse, ManagerError> {
104        let cfg = self.cfg.get().await?;
105        let cfg = cfg.as_ref();
106        with_retry(&self.retry, false, || {
107            outbound_api::update_dialer_behaviour(cfg, id, body.clone())
108        })
109        .await
110        .map_err(map_manager_err)
111    }
112
113    /// Delete a dialer behaviour.
114    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
115        let cfg = self.cfg.get().await?;
116        let cfg = cfg.as_ref();
117        with_retry(&self.retry, false, || {
118            outbound_api::delete_dialer_behaviour(cfg, id)
119        })
120        .await
121        .map_err(map_manager_err)?;
122        Ok(())
123    }
124}