use std::sync::Arc;
use crate::error::{map_manager_err, ManagerError};
use crate::gen::manager::apis::configuration::Configuration;
use crate::gen::manager::apis::manager_api as api;
use crate::gen::manager::apis::outbound_api;
use crate::gen::manager::models;
use crate::retry::{with_retry, RetryPolicy};
pub struct CampaignsResource {
pub(crate) cfg: Arc<Configuration>,
pub(crate) retry: RetryPolicy,
}
impl CampaignsResource {
pub async fn list(&self) -> Result<Vec<models::OutboundCampaign>, ManagerError> {
let resp = with_retry(&self.retry, true, || api::list_campaigns(self.cfg.as_ref()))
.await
.map_err(map_manager_err)?;
Ok(resp.items)
}
pub async fn create(
&self,
body: models::CreateCampaignRequest,
) -> Result<models::OutboundCampaignItemResponse, ManagerError> {
with_retry(&self.retry, false, || {
api::create_campaign(self.cfg.as_ref(), Some(body.clone()))
})
.await
.map_err(map_manager_err)
}
pub async fn get(
&self,
id: &str,
) -> Result<models::OutboundCampaignItemResponse, ManagerError> {
with_retry(&self.retry, true, || {
api::get_campaign(self.cfg.as_ref(), id)
})
.await
.map_err(map_manager_err)
}
pub async fn update(
&self,
id: &str,
body: models::UpdateCampaignRequest,
) -> Result<models::OutboundCampaignItemResponse, ManagerError> {
with_retry(&self.retry, false, || {
api::update_campaign(self.cfg.as_ref(), id, Some(body.clone()))
})
.await
.map_err(map_manager_err)
}
pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
api::delete_campaign(self.cfg.as_ref(), id)
})
.await
.map_err(map_manager_err)?;
Ok(())
}
pub async fn attempts(
&self,
id: &str,
number: Option<&str>,
) -> Result<models::PaginatedAttemptResponse, ManagerError> {
with_retry(&self.retry, true, || {
outbound_api::list_campaign_attempts(self.cfg.as_ref(), id, number)
})
.await
.map_err(map_manager_err)
}
pub async fn hopper(&self, id: &str) -> Result<models::CampaignHopperResponse, ManagerError> {
with_retry(&self.retry, true, || {
outbound_api::get_campaign_hopper(self.cfg.as_ref(), id)
})
.await
.map_err(map_manager_err)
}
pub async fn leads(&self, id: &str) -> Result<models::PaginatedLeadResponse, ManagerError> {
with_retry(&self.retry, true, || {
outbound_api::list_campaign_leads(self.cfg.as_ref(), id)
})
.await
.map_err(map_manager_err)
}
pub async fn processed_leads(
&self,
id: &str,
) -> Result<models::PaginatedLeadResponse, ManagerError> {
with_retry(&self.retry, true, || {
outbound_api::list_campaign_processed_leads(self.cfg.as_ref(), id)
})
.await
.map_err(map_manager_err)
}
pub async fn get_list(&self, id: &str) -> Result<models::LeadListItemResponse, ManagerError> {
with_retry(&self.retry, true, || {
outbound_api::get_campaign_list(self.cfg.as_ref(), id)
})
.await
.map_err(map_manager_err)
}
pub async fn set_list(
&self,
id: &str,
body: models::SetCampaignListRequest,
) -> Result<models::CampaignItemResponse, ManagerError> {
with_retry(&self.retry, false, || {
outbound_api::set_campaign_list(self.cfg.as_ref(), id, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn unset_list(&self, id: &str) -> Result<models::CampaignItemResponse, ManagerError> {
with_retry(&self.retry, false, || {
outbound_api::unset_campaign_list(self.cfg.as_ref(), id)
})
.await
.map_err(map_manager_err)
}
pub async fn set_list_by_id(
&self,
id: &str,
list_id: &str,
) -> Result<models::CampaignItemResponse, ManagerError> {
with_retry(&self.retry, false, || {
outbound_api::set_campaign_list_by_id(self.cfg.as_ref(), id, list_id)
})
.await
.map_err(map_manager_err)
}
pub async fn logout_all_agents(
&self,
id: &str,
) -> Result<models::DefaultV2MessageResponse, ManagerError> {
with_retry(&self.retry, false, || {
outbound_api::logout_all_campaign_agents(self.cfg.as_ref(), id)
})
.await
.map_err(map_manager_err)
}
pub async fn statistics(
&self,
id: &str,
from: Option<i32>,
to: Option<i32>,
) -> Result<models::CampaignStatisticsResponse, ManagerError> {
with_retry(&self.retry, true, || {
outbound_api::get_campaign_statistics(self.cfg.as_ref(), id, from, to)
})
.await
.map_err(map_manager_err)
}
pub async fn status(
&self,
id: &str,
) -> Result<models::CampaignRealtimeStatusResponse, ManagerError> {
with_retry(&self.retry, true, || {
outbound_api::get_campaign_status(self.cfg.as_ref(), id)
})
.await
.map_err(map_manager_err)
}
}