Skip to main content

babelforce_manager_sdk/resources/
expressions.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/// Expressions — `/api/v2/expressions`. The catalog of available expressions plus an evaluator.
9pub struct ExpressionsResource {
10    pub(crate) cfg: SharedCfg<Configuration>,
11    pub(crate) retry: RetryPolicy,
12}
13
14impl ExpressionsResource {
15    /// List the available expressions.
16    pub async fn list(&self) -> Result<Vec<models::AvailableExpression>, ManagerError> {
17        let cfg = self.cfg.get().await?;
18        let cfg = cfg.as_ref();
19        let resp = with_retry(&self.retry, true, || api::list_expressions(cfg))
20            .await
21            .map_err(map_manager_err)?;
22        Ok(resp.items)
23    }
24
25    /// Evaluate an expression. `is_async` runs the evaluation asynchronously.
26    pub async fn evaluate(
27        &self,
28        body: models::EvaluateExpression,
29        is_async: bool,
30    ) -> Result<models::EvaluateExpressionResponse, ManagerError> {
31        let cfg = self.cfg.get().await?;
32        let cfg = cfg.as_ref();
33        with_retry(&self.retry, false, || {
34            api::evaluate_expression(cfg, is_async, Some(body.clone()))
35        })
36        .await
37        .map_err(map_manager_err)
38    }
39}