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((
50                r.items,
51                r.pagination.pages.unwrap_or(1),
52                r.pagination.current.unwrap_or(1),
53            ))
54        })
55        .await
56    }
57}
58
59/// Dialer behaviours — `/api/v2/outbound/dialer-behaviours`.
60pub struct DialerBehavioursResource {
61    pub(crate) cfg: SharedCfg<Configuration>,
62    pub(crate) retry: RetryPolicy,
63}
64
65impl DialerBehavioursResource {
66    /// List all dialer behaviours (auto-paginated).
67    pub async fn list_all(&self) -> Result<Vec<models::DialerBehaviour>, ManagerError> {
68        let cfg = self.cfg.get().await?;
69        let cfg = cfg.as_ref();
70        collect_all(&self.retry, map_manager_err, |page| async move {
71            let r = outbound_api::list_dialer_behaviours(cfg, Some(page), None).await?;
72            Ok((
73                r.items,
74                r.pagination.pages.unwrap_or(1),
75                r.pagination.current.unwrap_or(1),
76            ))
77        })
78        .await
79    }
80
81    /// Create a dialer behaviour.
82    pub async fn create(
83        &self,
84        body: models::DialerBehaviourWriteBody,
85    ) -> Result<models::DialerBehaviourItemResponse, ManagerError> {
86        let cfg = self.cfg.get().await?;
87        let cfg = cfg.as_ref();
88        with_retry(&self.retry, false, || {
89            outbound_api::create_dialer_behaviour(cfg, body.clone())
90        })
91        .await
92        .map_err(map_manager_err)
93    }
94
95    /// Get a dialer behaviour by id.
96    pub async fn get(&self, id: &str) -> Result<models::DialerBehaviourItemResponse, ManagerError> {
97        let cfg = self.cfg.get().await?;
98        let cfg = cfg.as_ref();
99        with_retry(&self.retry, true, || {
100            outbound_api::get_dialer_behaviour(cfg, id)
101        })
102        .await
103        .map_err(map_manager_err)
104    }
105
106    /// Update a dialer behaviour.
107    pub async fn update(
108        &self,
109        id: &str,
110        body: models::DialerBehaviourWriteBody,
111    ) -> Result<models::DialerBehaviourItemResponse, ManagerError> {
112        let cfg = self.cfg.get().await?;
113        let cfg = cfg.as_ref();
114        with_retry(&self.retry, false, || {
115            outbound_api::update_dialer_behaviour(cfg, id, body.clone())
116        })
117        .await
118        .map_err(map_manager_err)
119    }
120
121    /// Delete a dialer behaviour.
122    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
123        let cfg = self.cfg.get().await?;
124        let cfg = cfg.as_ref();
125        with_retry(&self.retry, false, || {
126            outbound_api::delete_dialer_behaviour(cfg, id)
127        })
128        .await
129        .map_err(map_manager_err)?;
130        Ok(())
131    }
132}