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, queue_api, queue_selection_api};
use crate::gen::manager::models;
use crate::http::{collect_all, fetch_page, Page};
use crate::retry::{with_retry, RetryPolicy};
pub struct QueuesResource {
pub(crate) cfg: Arc<Configuration>,
pub(crate) retry: RetryPolicy,
pub selections: QueueSelectionsResource,
}
impl QueuesResource {
pub async fn list_all(&self) -> Result<Vec<models::Queue>, ManagerError> {
collect_all(&self.retry, map_manager_err, |page| async move {
let r = queue_api::list_queues(self.cfg.as_ref(), Some(page), None).await?;
Ok((r.items, r.pagination.pages, r.pagination.current))
})
.await
}
pub async fn list_page(
&self,
page: i32,
per_page: Option<i32>,
) -> Result<Page<models::Queue>, ManagerError> {
fetch_page(&self.retry, map_manager_err, || async move {
let r = queue_api::list_queues(self.cfg.as_ref(), Some(page), per_page).await?;
Ok((
r.items,
r.pagination.pages,
r.pagination.current,
Some(r.pagination.total),
))
})
.await
}
pub async fn create(
&self,
body: models::RestCreateQueue,
) -> Result<models::QueueItemResponse, ManagerError> {
with_retry(&self.retry, false, || {
queue_api::create_queue(self.cfg.as_ref(), body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn get(&self, id: &str) -> Result<models::QueueItemResponse, ManagerError> {
with_retry(&self.retry, true, || {
queue_api::get_queue(self.cfg.as_ref(), id)
})
.await
.map_err(map_manager_err)
}
pub async fn update(
&self,
id: &str,
body: models::RestUpdateQueue,
) -> Result<models::QueueItemResponse, ManagerError> {
with_retry(&self.retry, false, || {
queue_api::update_queue(self.cfg.as_ref(), id, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
queue_api::delete_queue(self.cfg.as_ref(), id)
})
.await
.map_err(map_manager_err)?;
Ok(())
}
pub async fn list_triggers(
&self,
queue_id: &str,
) -> Result<models::QueueTriggerListResponse, ManagerError> {
with_retry(&self.retry, true, || {
queue_api::list_queue_triggers(self.cfg.as_ref(), queue_id)
})
.await
.map_err(map_manager_err)
}
pub async fn bulk_update(
&self,
body: models::QueueBulkUpdateRequest,
) -> Result<models::QueueListResponse, ManagerError> {
with_retry(&self.retry, false, || {
queue_api::bulk_update_queues(self.cfg.as_ref(), body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn global_selections(
&self,
) -> Result<models::GlobalQueueSelectionListResponse, ManagerError> {
with_retry(&self.retry, true, || {
queue_api::list_global_queue_selections(self.cfg.as_ref(), None, None, None, None, None)
})
.await
.map_err(map_manager_err)
}
}
pub struct QueueSelectionsResource {
pub(crate) cfg: Arc<Configuration>,
pub(crate) retry: RetryPolicy,
}
impl QueueSelectionsResource {
pub async fn list_all(
&self,
queue_id: &str,
) -> Result<Vec<models::QueueSelection>, ManagerError> {
collect_all(&self.retry, map_manager_err, |page| async move {
let r = queue_selection_api::list_queue_selections(
self.cfg.as_ref(),
queue_id,
Some(page),
None,
)
.await?;
Ok((r.items, r.pagination.pages, r.pagination.current))
})
.await
}
pub async fn create(
&self,
queue_id: &str,
body: models::RestCreateQueueSelection,
) -> Result<models::QueueSelectionItemResponse, ManagerError> {
with_retry(&self.retry, false, || {
queue_selection_api::create_queue_selection(self.cfg.as_ref(), queue_id, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn get(
&self,
queue_id: &str,
id: &str,
) -> Result<models::QueueSelectionItemResponse, ManagerError> {
with_retry(&self.retry, true, || {
queue_selection_api::get_queue_selection(self.cfg.as_ref(), queue_id, id)
})
.await
.map_err(map_manager_err)
}
pub async fn update(
&self,
queue_id: &str,
id: &str,
body: models::RestUpdateQueueSelection,
) -> Result<models::QueueSelectionItemResponse, ManagerError> {
with_retry(&self.retry, false, || {
queue_selection_api::update_queue_selection(
self.cfg.as_ref(),
queue_id,
id,
body.clone(),
)
})
.await
.map_err(map_manager_err)
}
pub async fn delete(&self, queue_id: &str, id: &str) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
queue_selection_api::delete_queue_selection(self.cfg.as_ref(), queue_id, id)
})
.await
.map_err(map_manager_err)?;
Ok(())
}
pub async fn select_agents(
&self,
queue_id: &str,
) -> Result<models::QueueSelectionResponse, ManagerError> {
with_retry(&self.retry, true, || {
manager_api::get_agents_for_queue_selection(self.cfg.as_ref(), queue_id, None)
})
.await
.map_err(map_manager_err)
}
pub async fn add_agent(
&self,
queue_id: &str,
selection_id: &str,
agent_id: &str,
) -> Result<models::QueueSelectionModificationResponse, ManagerError> {
with_retry(&self.retry, false, || {
manager_api::add_agent_to_queue_selection(
self.cfg.as_ref(),
queue_id,
selection_id,
Some(addition(agent_id)),
)
})
.await
.map_err(map_manager_err)
}
pub async fn remove_agent(
&self,
queue_id: &str,
selection_id: &str,
id: &str,
) -> Result<models::QueueSelectionModificationResponse, ManagerError> {
with_retry(&self.retry, false, || {
manager_api::remove_agent_from_queue_selection(
self.cfg.as_ref(),
queue_id,
selection_id,
id,
)
})
.await
.map_err(map_manager_err)
}
pub async fn add_group(
&self,
queue_id: &str,
selection_id: &str,
group_id: &str,
) -> Result<models::QueueSelectionModificationResponse, ManagerError> {
with_retry(&self.retry, false, || {
manager_api::add_group_to_queue_selection(
self.cfg.as_ref(),
queue_id,
selection_id,
Some(addition(group_id)),
)
})
.await
.map_err(map_manager_err)
}
pub async fn remove_group(
&self,
queue_id: &str,
selection_id: &str,
id: &str,
) -> Result<models::QueueSelectionModificationResponse, ManagerError> {
with_retry(&self.retry, false, || {
manager_api::remove_group_from_queue_selection(
self.cfg.as_ref(),
queue_id,
selection_id,
id,
)
})
.await
.map_err(map_manager_err)
}
pub async fn add_tag(
&self,
queue_id: &str,
selection_id: &str,
tag_id: &str,
) -> Result<models::QueueSelectionModificationResponse, ManagerError> {
with_retry(&self.retry, false, || {
manager_api::add_tag_to_queue_selection(
self.cfg.as_ref(),
queue_id,
selection_id,
Some(addition(tag_id)),
)
})
.await
.map_err(map_manager_err)
}
pub async fn remove_tag(
&self,
queue_id: &str,
selection_id: &str,
id: &str,
) -> Result<models::QueueSelectionModificationResponse, ManagerError> {
with_retry(&self.retry, false, || {
manager_api::remove_tag_from_queue_selection(
self.cfg.as_ref(),
queue_id,
selection_id,
id,
)
})
.await
.map_err(map_manager_err)
}
pub async fn set_priority(
&self,
queue_id: &str,
items: Vec<models::QueueSelectionPriorityItem>,
) -> Result<models::QueueSelectionListResponse, ManagerError> {
with_retry(&self.retry, false, || {
queue_api::set_queue_selections_priority(self.cfg.as_ref(), queue_id, items.clone())
})
.await
.map_err(map_manager_err)
}
}
fn addition(id: &str) -> models::QueueSelectionAddition {
models::QueueSelectionAddition {
id: Some(id.to_string()),
}
}