Skip to main content

babelforce_manager_sdk/resources/
me.rs

1use crate::error::{map_user_err, ManagerError};
2use crate::gen::user::apis::configuration::Configuration;
3use crate::gen::user::apis::user_api as api;
4use crate::gen::user::models;
5use crate::retry::{with_retry, RetryPolicy};
6use crate::token::SharedCfg;
7
8/// The authenticated principal — the current user, the accounts they can access, and self-service
9/// password reset. Backed by `/api/v2/user` (the user spec).
10pub struct MeResource {
11    pub(crate) cfg: SharedCfg<Configuration>,
12    pub(crate) retry: RetryPolicy,
13}
14
15impl MeResource {
16    /// Get the current user.
17    pub async fn get(&self) -> Result<models::UserItemResponse, ManagerError> {
18        let cfg = self.cfg.get().await?;
19        let cfg = cfg.as_ref();
20        with_retry(&self.retry, true, || api::get_user(cfg))
21            .await
22            .map_err(map_user_err)
23    }
24
25    /// Get the current user together with their account (customer) information.
26    pub async fn customer(&self) -> Result<models::UserCustomerItemResponse, ManagerError> {
27        let cfg = self.cfg.get().await?;
28        let cfg = cfg.as_ref();
29        with_retry(&self.retry, true, || api::get_user_customer(cfg))
30            .await
31            .map_err(map_user_err)
32    }
33
34    /// List the accounts the current user can access.
35    pub async fn accounts(&self) -> Result<Vec<models::Account>, ManagerError> {
36        let cfg = self.cfg.get().await?;
37        let cfg = cfg.as_ref();
38        let resp = with_retry(&self.retry, true, || api::list_accounts(cfg))
39            .await
40            .map_err(map_user_err)?;
41        Ok(resp.items)
42    }
43
44    /// Request a password-reset email for the current user.
45    pub async fn reset_password(&self) -> Result<(), ManagerError> {
46        let cfg = self.cfg.get().await?;
47        let cfg = cfg.as_ref();
48        with_retry(&self.retry, false, || api::reset_password(cfg))
49            .await
50            .map_err(map_user_err)?;
51        Ok(())
52    }
53}