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