Skip to main content

babelforce_manager_sdk/resources/
campaigns.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::manager_api as api;
6use crate::gen::manager::apis::outbound_api;
7use crate::gen::manager::models;
8use crate::retry::{with_retry, RetryPolicy};
9
10/// Outbound campaigns — `/api/v2/outbound/campaigns`.
11pub struct CampaignsResource {
12    pub(crate) cfg: Arc<Configuration>,
13    pub(crate) retry: RetryPolicy,
14}
15
16impl CampaignsResource {
17    /// List the outbound campaigns.
18    pub async fn list(&self) -> Result<Vec<models::OutboundCampaign>, ManagerError> {
19        let resp = with_retry(&self.retry, true, || api::list_campaigns(self.cfg.as_ref()))
20            .await
21            .map_err(map_manager_err)?;
22        Ok(resp.items)
23    }
24
25    /// Create a campaign.
26    pub async fn create(
27        &self,
28        body: models::CreateCampaignRequest,
29    ) -> Result<models::OutboundCampaignItemResponse, ManagerError> {
30        with_retry(&self.retry, false, || {
31            api::create_campaign(self.cfg.as_ref(), Some(body.clone()))
32        })
33        .await
34        .map_err(map_manager_err)
35    }
36
37    /// Get a campaign by id.
38    pub async fn get(
39        &self,
40        id: &str,
41    ) -> Result<models::OutboundCampaignItemResponse, ManagerError> {
42        with_retry(&self.retry, true, || {
43            api::get_campaign(self.cfg.as_ref(), id)
44        })
45        .await
46        .map_err(map_manager_err)
47    }
48
49    /// Update a campaign.
50    pub async fn update(
51        &self,
52        id: &str,
53        body: models::UpdateCampaignRequest,
54    ) -> Result<models::OutboundCampaignItemResponse, ManagerError> {
55        with_retry(&self.retry, false, || {
56            api::update_campaign(self.cfg.as_ref(), id, Some(body.clone()))
57        })
58        .await
59        .map_err(map_manager_err)
60    }
61
62    /// Delete a campaign.
63    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
64        with_retry(&self.retry, false, || {
65            api::delete_campaign(self.cfg.as_ref(), id)
66        })
67        .await
68        .map_err(map_manager_err)?;
69        Ok(())
70    }
71
72    /// List the dial attempts for a campaign, optionally filtered by phone `number`.
73    pub async fn attempts(
74        &self,
75        id: &str,
76        number: Option<&str>,
77    ) -> Result<models::PaginatedAttemptResponse, ManagerError> {
78        with_retry(&self.retry, true, || {
79            outbound_api::list_campaign_attempts(self.cfg.as_ref(), id, number)
80        })
81        .await
82        .map_err(map_manager_err)
83    }
84
85    /// Get the hopper (queued leads) of a campaign.
86    pub async fn hopper(&self, id: &str) -> Result<models::CampaignHopperResponse, ManagerError> {
87        with_retry(&self.retry, true, || {
88            outbound_api::get_campaign_hopper(self.cfg.as_ref(), id)
89        })
90        .await
91        .map_err(map_manager_err)
92    }
93
94    /// List the leads of a campaign.
95    pub async fn leads(&self, id: &str) -> Result<models::PaginatedLeadResponse, ManagerError> {
96        with_retry(&self.retry, true, || {
97            outbound_api::list_campaign_leads(self.cfg.as_ref(), id)
98        })
99        .await
100        .map_err(map_manager_err)
101    }
102
103    /// List the processed leads of a campaign.
104    pub async fn processed_leads(
105        &self,
106        id: &str,
107    ) -> Result<models::PaginatedLeadResponse, ManagerError> {
108        with_retry(&self.retry, true, || {
109            outbound_api::list_campaign_processed_leads(self.cfg.as_ref(), id)
110        })
111        .await
112        .map_err(map_manager_err)
113    }
114
115    /// Get the lead list assigned to a campaign.
116    pub async fn get_list(&self, id: &str) -> Result<models::LeadListItemResponse, ManagerError> {
117        with_retry(&self.retry, true, || {
118            outbound_api::get_campaign_list(self.cfg.as_ref(), id)
119        })
120        .await
121        .map_err(map_manager_err)
122    }
123
124    /// Set the lead list of a campaign.
125    pub async fn set_list(
126        &self,
127        id: &str,
128        body: models::SetCampaignListRequest,
129    ) -> Result<models::CampaignItemResponse, ManagerError> {
130        with_retry(&self.retry, false, || {
131            outbound_api::set_campaign_list(self.cfg.as_ref(), id, body.clone())
132        })
133        .await
134        .map_err(map_manager_err)
135    }
136
137    /// Unset (detach) the lead list of a campaign.
138    pub async fn unset_list(&self, id: &str) -> Result<models::CampaignItemResponse, ManagerError> {
139        with_retry(&self.retry, false, || {
140            outbound_api::unset_campaign_list(self.cfg.as_ref(), id)
141        })
142        .await
143        .map_err(map_manager_err)
144    }
145
146    /// Set the lead list of a campaign by list id.
147    pub async fn set_list_by_id(
148        &self,
149        id: &str,
150        list_id: &str,
151    ) -> Result<models::CampaignItemResponse, ManagerError> {
152        with_retry(&self.retry, false, || {
153            outbound_api::set_campaign_list_by_id(self.cfg.as_ref(), id, list_id)
154        })
155        .await
156        .map_err(map_manager_err)
157    }
158
159    /// Log out all agents currently assigned to a campaign.
160    pub async fn logout_all_agents(
161        &self,
162        id: &str,
163    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
164        with_retry(&self.retry, false, || {
165            outbound_api::logout_all_campaign_agents(self.cfg.as_ref(), id)
166        })
167        .await
168        .map_err(map_manager_err)
169    }
170
171    /// Get the statistics of a campaign, optionally bounded by a `from`/`to` time range.
172    pub async fn statistics(
173        &self,
174        id: &str,
175        from: Option<i32>,
176        to: Option<i32>,
177    ) -> Result<models::CampaignStatisticsResponse, ManagerError> {
178        with_retry(&self.retry, true, || {
179            outbound_api::get_campaign_statistics(self.cfg.as_ref(), id, from, to)
180        })
181        .await
182        .map_err(map_manager_err)
183    }
184
185    /// Get the realtime status of a campaign.
186    pub async fn status(
187        &self,
188        id: &str,
189    ) -> Result<models::CampaignRealtimeStatusResponse, ManagerError> {
190        with_retry(&self.retry, true, || {
191            outbound_api::get_campaign_status(self.cfg.as_ref(), id)
192        })
193        .await
194        .map_err(map_manager_err)
195    }
196}