babelforce_manager_sdk/resources/
conversations.rs1use std::sync::Arc;
2
3use crate::error::{map_manager_err, ManagerError};
4use crate::gen::manager::apis::configuration::Configuration;
5use crate::gen::manager::apis::{conversation_api as api, conversations_api as sub};
6use crate::gen::manager::models;
7use crate::http::collect_all;
8use crate::retry::{with_retry, RetryPolicy};
9
10pub struct ConversationsResource {
12 pub(crate) cfg: Arc<Configuration>,
13 pub(crate) retry: RetryPolicy,
14}
15
16impl ConversationsResource {
17 pub async fn list_all(&self) -> Result<Vec<models::Conversation>, ManagerError> {
19 collect_all(&self.retry, map_manager_err, |page| async move {
20 let r =
21 api::list_conversations(self.cfg.as_ref(), Some(page), None, None, None).await?;
22 Ok((r.items, r.pagination.pages, r.pagination.current))
23 })
24 .await
25 }
26
27 pub async fn create(
29 &self,
30 body: models::RestCreateConversation,
31 ) -> Result<models::ConversationItemResponse, ManagerError> {
32 with_retry(&self.retry, false, || {
33 api::create_conversation(self.cfg.as_ref(), body.clone())
34 })
35 .await
36 .map_err(map_manager_err)
37 }
38
39 pub async fn get(&self, id: &str) -> Result<models::ConversationItemResponse, ManagerError> {
41 with_retry(&self.retry, true, || {
42 api::get_conversation(self.cfg.as_ref(), id)
43 })
44 .await
45 .map_err(map_manager_err)
46 }
47
48 pub async fn update(
50 &self,
51 id: &str,
52 body: models::RestUpdateConversation,
53 ) -> Result<models::ConversationItemResponse, ManagerError> {
54 with_retry(&self.retry, false, || {
55 api::update_conversation(self.cfg.as_ref(), id, body.clone())
56 })
57 .await
58 .map_err(map_manager_err)
59 }
60
61 pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
63 with_retry(&self.retry, false, || {
64 api::delete_conversation(self.cfg.as_ref(), id)
65 })
66 .await
67 .map_err(map_manager_err)?;
68 Ok(())
69 }
70
71 pub async fn events(
73 &self,
74 conversation_id: &str,
75 ) -> Result<Vec<models::ConversationEvent>, ManagerError> {
76 let resp = with_retry(&self.retry, true, || {
77 sub::list_conversation_events(self.cfg.as_ref(), conversation_id)
78 })
79 .await
80 .map_err(map_manager_err)?;
81 Ok(resp.items)
82 }
83
84 pub async fn add_event(
86 &self,
87 conversation_id: &str,
88 body: models::ConversationEventRequest,
89 ) -> Result<models::ConversationEventItemResponse, ManagerError> {
90 with_retry(&self.retry, false, || {
91 sub::add_conversation_event(self.cfg.as_ref(), conversation_id, body.clone())
92 })
93 .await
94 .map_err(map_manager_err)
95 }
96
97 pub async fn open(
99 &self,
100 conversation_id: &str,
101 ) -> Result<models::ConversationItemResponse, ManagerError> {
102 with_retry(&self.retry, false, || {
103 sub::open_conversation(self.cfg.as_ref(), conversation_id)
104 })
105 .await
106 .map_err(map_manager_err)
107 }
108
109 pub async fn close(
111 &self,
112 conversation_id: &str,
113 ) -> Result<models::ConversationItemResponse, ManagerError> {
114 with_retry(&self.retry, false, || {
115 sub::close_conversation(self.cfg.as_ref(), conversation_id)
116 })
117 .await
118 .map_err(map_manager_err)
119 }
120
121 pub async fn first_event(
123 &self,
124 conversation_id: &str,
125 ) -> Result<models::ConversationEventItemResponse, ManagerError> {
126 with_retry(&self.retry, true, || {
127 sub::get_first_conversation_event(self.cfg.as_ref(), conversation_id)
128 })
129 .await
130 .map_err(map_manager_err)
131 }
132
133 pub async fn latest_event(
135 &self,
136 conversation_id: &str,
137 ) -> Result<models::ConversationEventItemResponse, ManagerError> {
138 with_retry(&self.retry, true, || {
139 sub::get_latest_conversation_event(self.cfg.as_ref(), conversation_id)
140 })
141 .await
142 .map_err(map_manager_err)
143 }
144
145 pub async fn all_events(&self) -> Result<Vec<models::ConversationEvent>, ManagerError> {
147 collect_all(&self.retry, map_manager_err, |page| async move {
148 let r = sub::list_all_conversation_events(self.cfg.as_ref(), Some(page), None).await?;
149 Ok((r.items, r.pagination.pages, r.pagination.current))
150 })
151 .await
152 }
153
154 pub async fn get_session(
156 &self,
157 conversation_id: &str,
158 ) -> Result<models::ConversationSessionVariablesItemResponse, ManagerError> {
159 with_retry(&self.retry, true, || {
160 sub::get_conversation_session(self.cfg.as_ref(), conversation_id)
161 })
162 .await
163 .map_err(map_manager_err)
164 }
165
166 pub async fn update_session(
168 &self,
169 conversation_id: &str,
170 variables: std::collections::HashMap<String, serde_json::Value>,
171 ) -> Result<models::ConversationSessionVariablesItemResponse, ManagerError> {
172 with_retry(&self.retry, false, || {
173 sub::update_conversation_session(
174 self.cfg.as_ref(),
175 conversation_id,
176 Some(variables.clone()),
177 )
178 })
179 .await
180 .map_err(map_manager_err)
181 }
182}