use crate::error::{map_manager_err, ManagerError};
use crate::gen::manager::apis::configuration::Configuration;
use crate::gen::manager::apis::{conversation_api as api, conversations_api as sub};
use crate::gen::manager::models;
use crate::http::collect_all;
use crate::retry::{with_retry, RetryPolicy};
use crate::token::SharedCfg;
pub struct ConversationsResource {
pub(crate) cfg: SharedCfg<Configuration>,
pub(crate) retry: RetryPolicy,
}
impl ConversationsResource {
pub async fn list_all(&self) -> Result<Vec<models::Conversation>, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
collect_all(&self.retry, map_manager_err, |page| async move {
let r = api::list_conversations(cfg, Some(page), None, None, None).await?;
Ok((r.items, r.pagination.pages, r.pagination.current))
})
.await
}
pub async fn create(
&self,
body: models::RestCreateConversation,
) -> Result<models::ConversationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::create_conversation(cfg, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn get(&self, id: &str) -> Result<models::ConversationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || api::get_conversation(cfg, id))
.await
.map_err(map_manager_err)
}
pub async fn update(
&self,
id: &str,
body: models::RestUpdateConversation,
) -> Result<models::ConversationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::update_conversation(cfg, id, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || api::delete_conversation(cfg, id))
.await
.map_err(map_manager_err)?;
Ok(())
}
pub async fn events(
&self,
conversation_id: &str,
) -> Result<Vec<models::ConversationEvent>, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
let resp = with_retry(&self.retry, true, || {
sub::list_conversation_events(cfg, conversation_id)
})
.await
.map_err(map_manager_err)?;
Ok(resp.items)
}
pub async fn add_event(
&self,
conversation_id: &str,
body: models::ConversationEventRequest,
) -> Result<models::ConversationEventItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
sub::add_conversation_event(cfg, conversation_id, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn open(
&self,
conversation_id: &str,
) -> Result<models::ConversationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
sub::open_conversation(cfg, conversation_id)
})
.await
.map_err(map_manager_err)
}
pub async fn close(
&self,
conversation_id: &str,
) -> Result<models::ConversationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
sub::close_conversation(cfg, conversation_id)
})
.await
.map_err(map_manager_err)
}
pub async fn first_event(
&self,
conversation_id: &str,
) -> Result<models::ConversationEventItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || {
sub::get_first_conversation_event(cfg, conversation_id)
})
.await
.map_err(map_manager_err)
}
pub async fn latest_event(
&self,
conversation_id: &str,
) -> Result<models::ConversationEventItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || {
sub::get_latest_conversation_event(cfg, conversation_id)
})
.await
.map_err(map_manager_err)
}
pub async fn all_events(&self) -> Result<Vec<models::ConversationEvent>, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
collect_all(&self.retry, map_manager_err, |page| async move {
let r = sub::list_all_conversation_events(cfg, Some(page), None).await?;
Ok((r.items, r.pagination.pages, r.pagination.current))
})
.await
}
pub async fn get_session(
&self,
conversation_id: &str,
) -> Result<models::ConversationSessionVariablesItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || {
sub::get_conversation_session(cfg, conversation_id)
})
.await
.map_err(map_manager_err)
}
pub async fn update_session(
&self,
conversation_id: &str,
variables: std::collections::HashMap<String, serde_json::Value>,
) -> Result<models::ConversationSessionVariablesItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
sub::update_conversation_session(cfg, conversation_id, Some(variables.clone()))
})
.await
.map_err(map_manager_err)
}
}