1use std::sync::Arc;
2
3use crate::error::{map_manager_err, ManagerError};
4use crate::gen::manager::apis::configuration::Configuration;
5use crate::gen::manager::apis::{agent_api, agent_group_api, manager_api};
6use crate::gen::manager::models;
7use crate::http::{collect_all, fetch_page, Page};
8use crate::retry::{with_retry, RetryPolicy};
9
10pub struct AgentsResource {
12 pub(crate) cfg: Arc<Configuration>,
13 pub(crate) retry: RetryPolicy,
14 pub groups: AgentGroupsResource,
16}
17
18impl AgentsResource {
19 pub async fn list_all(&self) -> Result<Vec<models::Agent>, ManagerError> {
21 collect_all(&self.retry, map_manager_err, |page| async move {
22 let r = agent_api::list_agents(
23 self.cfg.as_ref(),
24 Some(page),
25 None,
26 None,
27 None,
28 None,
29 None,
30 None,
31 None,
32 None,
33 None,
34 None,
35 None,
36 )
37 .await?;
38 Ok((r.items, r.pagination.pages, r.pagination.current))
39 })
40 .await
41 }
42
43 pub async fn list_page(
45 &self,
46 page: i32,
47 per_page: Option<i32>,
48 ) -> Result<Page<models::Agent>, ManagerError> {
49 fetch_page(&self.retry, map_manager_err, || async move {
50 let r = agent_api::list_agents(
51 self.cfg.as_ref(),
52 Some(page),
53 per_page,
54 None,
55 None,
56 None,
57 None,
58 None,
59 None,
60 None,
61 None,
62 None,
63 None,
64 )
65 .await?;
66 Ok((
67 r.items,
68 r.pagination.pages,
69 r.pagination.current,
70 Some(r.pagination.total),
71 ))
72 })
73 .await
74 }
75
76 pub async fn create(
78 &self,
79 body: models::RestCreateAgent,
80 ) -> Result<models::AgentItemResponse, ManagerError> {
81 with_retry(&self.retry, false, || {
82 agent_api::create_agent(self.cfg.as_ref(), body.clone())
83 })
84 .await
85 .map_err(map_manager_err)
86 }
87
88 pub async fn get(&self, id: &str) -> Result<models::AgentItemResponse, ManagerError> {
90 with_retry(&self.retry, true, || {
91 agent_api::get_agent(self.cfg.as_ref(), id)
92 })
93 .await
94 .map_err(map_manager_err)
95 }
96
97 pub async fn update(
99 &self,
100 id: &str,
101 body: models::RestUpdateAgent,
102 ) -> Result<models::AgentItemResponse, ManagerError> {
103 with_retry(&self.retry, false, || {
104 agent_api::update_agent(self.cfg.as_ref(), id, body.clone())
105 })
106 .await
107 .map_err(map_manager_err)
108 }
109
110 pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
112 with_retry(&self.retry, false, || {
113 agent_api::delete_agent(self.cfg.as_ref(), id)
114 })
115 .await
116 .map_err(map_manager_err)?;
117 Ok(())
118 }
119
120 pub async fn update_status(
122 &self,
123 id: &str,
124 status: models::UpdateAgentStatusRequest,
125 ) -> Result<models::AgentStatus, ManagerError> {
126 with_retry(&self.retry, false, || {
127 manager_api::update_agent_status(self.cfg.as_ref(), id, Some(status.clone()))
128 })
129 .await
130 .map_err(map_manager_err)
131 }
132
133 pub async fn presences(&self) -> Result<models::AgentPresenceListResponse, ManagerError> {
135 with_retry(&self.retry, true, || {
136 agent_api::list_agent_presences(self.cfg.as_ref())
137 })
138 .await
139 .map_err(map_manager_err)
140 }
141
142 pub async fn get_presence(
144 &self,
145 name: &str,
146 ) -> Result<models::AgentPresenceItemResponse, ManagerError> {
147 with_retry(&self.retry, true, || {
148 agent_api::get_agent_presence(self.cfg.as_ref(), name)
149 })
150 .await
151 .map_err(map_manager_err)
152 }
153
154 pub async fn create_presence(
156 &self,
157 body: models::AgentPresenceWriteBody,
158 ) -> Result<models::AgentPresenceItemResponse, ManagerError> {
159 with_retry(&self.retry, false, || {
160 agent_api::create_agent_presence(self.cfg.as_ref(), body.clone())
161 })
162 .await
163 .map_err(map_manager_err)
164 }
165
166 pub async fn update_presence(
168 &self,
169 name: &str,
170 body: models::AgentPresenceWriteBody,
171 ) -> Result<models::AgentPresenceItemResponse, ManagerError> {
172 with_retry(&self.retry, false, || {
173 agent_api::update_agent_presence(self.cfg.as_ref(), name, body.clone())
174 })
175 .await
176 .map_err(map_manager_err)
177 }
178
179 pub async fn delete_presence(
181 &self,
182 name: &str,
183 ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
184 with_retry(&self.retry, false, || {
185 agent_api::delete_agent_presence(self.cfg.as_ref(), name)
186 })
187 .await
188 .map_err(map_manager_err)
189 }
190
191 pub async fn get_status(
193 &self,
194 id: &str,
195 ) -> Result<models::AgentTotalStatusResponse, ManagerError> {
196 with_retry(&self.retry, true, || {
197 agent_api::get_agent_status(self.cfg.as_ref(), id)
198 })
199 .await
200 .map_err(map_manager_err)
201 }
202
203 pub async fn available_statuses(
205 &self,
206 ) -> Result<models::AgentAvailabilityListResponse, ManagerError> {
207 with_retry(&self.retry, true, || {
208 agent_api::list_available_agent_statuses(self.cfg.as_ref())
209 })
210 .await
211 .map_err(map_manager_err)
212 }
213
214 pub async fn enable(&self, id: &str) -> Result<models::DefaultV2MessageResponse, ManagerError> {
216 with_retry(&self.retry, false, || {
217 agent_api::enable_agent(self.cfg.as_ref(), id)
218 })
219 .await
220 .map_err(map_manager_err)
221 }
222
223 pub async fn disable(
225 &self,
226 id: &str,
227 ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
228 with_retry(&self.retry, false, || {
229 agent_api::disable_agent(self.cfg.as_ref(), id)
230 })
231 .await
232 .map_err(map_manager_err)
233 }
234
235 pub async fn hangup_call(
237 &self,
238 id: &str,
239 ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
240 with_retry(&self.retry, false, || {
241 agent_api::hangup_agent_call(self.cfg.as_ref(), id)
242 })
243 .await
244 .map_err(map_manager_err)
245 }
246
247 pub async fn bulk_action(
249 &self,
250 action: &str,
251 body: models::AgentBulkRequest,
252 ) -> Result<models::AgentBulkResponse, ManagerError> {
253 with_retry(&self.retry, false, || {
254 agent_api::bulk_agent_action(self.cfg.as_ref(), action, body.clone())
255 })
256 .await
257 .map_err(map_manager_err)
258 }
259
260 pub async fn export(&self, format: &str) -> Result<String, ManagerError> {
262 with_retry(&self.retry, true, || {
263 agent_api::export_agents(self.cfg.as_ref(), format)
264 })
265 .await
266 .map_err(map_manager_err)
267 }
268
269 #[allow(clippy::too_many_arguments)]
271 pub async fn import_agents(
272 &self,
273 format: &str,
274 file: std::path::PathBuf,
275 r#async: Option<bool>,
276 create_only: Option<bool>,
277 update_only: Option<bool>,
278 delete_unlisted: Option<bool>,
279 ) -> Result<models::AgentImportValidationResults, ManagerError> {
280 with_retry(&self.retry, false, || {
281 agent_api::import_agents(
282 self.cfg.as_ref(),
283 format,
284 file.clone(),
285 r#async,
286 create_only,
287 update_only,
288 delete_unlisted,
289 )
290 })
291 .await
292 .map_err(map_manager_err)
293 }
294
295 pub async fn validate_import(
297 &self,
298 file: std::path::PathBuf,
299 ) -> Result<models::AgentImportValidationResults, ManagerError> {
300 with_retry(&self.retry, false, || {
301 agent_api::validate_agent_import(self.cfg.as_ref(), file.clone())
302 })
303 .await
304 .map_err(map_manager_err)
305 }
306
307 pub async fn get_import_job(
309 &self,
310 id: &str,
311 ) -> Result<models::AgentImportJobItemResponse, ManagerError> {
312 with_retry(&self.retry, true, || {
313 agent_api::get_agent_import_job(self.cfg.as_ref(), id)
314 })
315 .await
316 .map_err(map_manager_err)
317 }
318
319 pub async fn logs(
321 &self,
322 id: &str,
323 from: Option<i32>,
324 to: Option<i32>,
325 ) -> Result<Vec<models::AgentLogEntry>, ManagerError> {
326 collect_all(&self.retry, map_manager_err, |page| async move {
327 let r = agent_api::list_agent_logs(self.cfg.as_ref(), id, Some(page), None, from, to)
328 .await?;
329 let (pages, current) = r
330 .pagination
331 .as_ref()
332 .map(|p| (p.pages, p.current))
333 .unwrap_or((1, 1));
334 Ok((r.items, pages, current))
335 })
336 .await
337 }
338
339 pub async fn all_logs(&self) -> Result<Vec<models::AgentLogEntry>, ManagerError> {
341 collect_all(&self.retry, map_manager_err, |page| async move {
342 let r = agent_api::list_all_agent_logs(self.cfg.as_ref(), Some(page), None).await?;
343 let (pages, current) = r
344 .pagination
345 .as_ref()
346 .map(|p| (p.pages, p.current))
347 .unwrap_or((1, 1));
348 Ok((r.items, pages, current))
349 })
350 .await
351 }
352
353 pub async fn push(
355 &self,
356 body: models::AgentPushRequest,
357 ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
358 with_retry(&self.retry, false, || {
359 agent_api::push_to_agent(self.cfg.as_ref(), body.clone())
360 })
361 .await
362 .map_err(map_manager_err)
363 }
364
365 pub async fn update_password(
367 &self,
368 id: &str,
369 body: models::AgentPasswordUpdateRequest,
370 ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
371 with_retry(&self.retry, false, || {
372 agent_api::update_agent_password(self.cfg.as_ref(), id, body.clone())
373 })
374 .await
375 .map_err(map_manager_err)
376 }
377}
378
379pub struct AgentGroupsResource {
381 pub(crate) cfg: Arc<Configuration>,
382 pub(crate) retry: RetryPolicy,
383}
384
385impl AgentGroupsResource {
386 pub async fn list_all(&self) -> Result<Vec<models::AgentGroup>, ManagerError> {
388 collect_all(&self.retry, map_manager_err, |page| async move {
389 let r = agent_group_api::list_agent_groups(self.cfg.as_ref(), Some(page), None).await?;
390 Ok((r.items, r.pagination.pages, r.pagination.current))
391 })
392 .await
393 }
394
395 pub async fn create(
397 &self,
398 body: models::RestCreateAgentGroup,
399 ) -> Result<models::AgentGroupItemResponse, ManagerError> {
400 with_retry(&self.retry, false, || {
401 agent_group_api::create_agent_group(self.cfg.as_ref(), body.clone())
402 })
403 .await
404 .map_err(map_manager_err)
405 }
406
407 pub async fn get(&self, id: &str) -> Result<models::AgentGroupItemResponse, ManagerError> {
409 with_retry(&self.retry, true, || {
410 agent_group_api::get_agent_group(self.cfg.as_ref(), id)
411 })
412 .await
413 .map_err(map_manager_err)
414 }
415
416 pub async fn update(
418 &self,
419 id: &str,
420 body: models::RestUpdateAgentGroup,
421 ) -> Result<models::AgentGroupItemResponse, ManagerError> {
422 with_retry(&self.retry, false, || {
423 agent_group_api::update_agent_group(self.cfg.as_ref(), id, body.clone())
424 })
425 .await
426 .map_err(map_manager_err)
427 }
428
429 pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
431 with_retry(&self.retry, false, || {
432 agent_group_api::delete_agent_group(self.cfg.as_ref(), id)
433 })
434 .await
435 .map_err(map_manager_err)?;
436 Ok(())
437 }
438
439 pub async fn add_agent(
441 &self,
442 group_id: &str,
443 agent_id: &str,
444 ) -> Result<models::AgentGroupAdditionResponse, ManagerError> {
445 let id = uuid::Uuid::parse_str(agent_id)
446 .map_err(|e| ManagerError::InvalidArgument(format!("agent_id: {e}")))?;
447 let body = models::AddAgentToGroupRequest { id };
448 with_retry(&self.retry, false, || {
449 manager_api::add_agent_to_group(self.cfg.as_ref(), group_id, Some(body.clone()))
450 })
451 .await
452 .map_err(map_manager_err)
453 }
454
455 pub async fn remove_agent(
457 &self,
458 group_id: &str,
459 agent_id: &str,
460 ) -> Result<models::AgentGroupItemResponse, ManagerError> {
461 with_retry(&self.retry, false, || {
462 agent_group_api::remove_agent_from_group(self.cfg.as_ref(), group_id, agent_id)
463 })
464 .await
465 .map_err(map_manager_err)
466 }
467
468 pub async fn list_agents(&self, group_id: &str) -> Result<Vec<models::Agent>, ManagerError> {
470 collect_all(&self.retry, map_manager_err, |page| async move {
471 let r = agent_group_api::list_agents_in_group(
472 self.cfg.as_ref(),
473 group_id,
474 Some(page),
475 None,
476 )
477 .await?;
478 Ok((r.items, r.pagination.pages, r.pagination.current))
479 })
480 .await
481 }
482
483 pub async fn bulk_delete(
485 &self,
486 ids: Vec<String>,
487 ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
488 let ids = ids
489 .iter()
490 .map(|id| {
491 uuid::Uuid::parse_str(id)
492 .map_err(|e| ManagerError::InvalidArgument(format!("ids: {e}")))
493 })
494 .collect::<Result<Vec<_>, _>>()?;
495 let body = models::BulkIdsRequest { ids };
496 with_retry(&self.retry, false, || {
497 agent_group_api::bulk_delete_agent_groups(self.cfg.as_ref(), body.clone())
498 })
499 .await
500 .map_err(map_manager_err)
501 }
502}