Skip to main content

babelforce_manager_sdk/resources/
sessions.rs

1use std::sync::Arc;
2
3use crate::error::{map_manager_err, ManagerError};
4use crate::gen::manager::apis::configuration::Configuration;
5use crate::gen::manager::apis::manager_api as api;
6use crate::gen::manager::models;
7use crate::retry::{with_retry, RetryPolicy};
8
9/// Call/automation sessions — `/api/v2/sessions`.
10pub struct SessionsResource {
11    pub(crate) cfg: Arc<Configuration>,
12    pub(crate) retry: RetryPolicy,
13}
14
15impl SessionsResource {
16    /// Create a new session.
17    pub async fn create(&self) -> Result<models::SessionResponse, ManagerError> {
18        with_retry(&self.retry, false, || {
19            api::create_session(self.cfg.as_ref())
20        })
21        .await
22        .map_err(map_manager_err)
23    }
24
25    /// Get a session (its variables) by id.
26    pub async fn get(&self, id: &str) -> Result<models::SessionResponse, ManagerError> {
27        with_retry(&self.retry, true, || {
28            api::get_session_variables(self.cfg.as_ref(), id)
29        })
30        .await
31        .map_err(map_manager_err)
32    }
33
34    /// Update a session's variables.
35    pub async fn update_variables(
36        &self,
37        id: &str,
38        variables: std::collections::HashMap<String, serde_json::Value>,
39    ) -> Result<models::SessionResponse, ManagerError> {
40        with_retry(&self.retry, false, || {
41            api::update_session_variables(self.cfg.as_ref(), id, Some(variables.clone()))
42        })
43        .await
44        .map_err(map_manager_err)
45    }
46}