Skip to main content

babelforce_manager_sdk/resources/
me.rs

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