Skip to main content

babelforce_manager_sdk/resources/
sessions.rs

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