rain_sdk/api/
balances.rs

1//! Balances API
2//!
3//! This module provides functionality to retrieve balances for tenants, companies, and users.
4
5use crate::client::RainClient;
6use crate::error::Result;
7use crate::models::balances::BalanceResponse;
8use uuid::Uuid;
9
10impl RainClient {
11    /// Get a tenant's credit balances
12    ///
13    /// # Returns
14    ///
15    /// Returns a [`BalanceResponse`] containing the tenant's balance information.
16    ///
17    /// # Examples
18    ///
19    /// ```no_run
20    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
21    ///
22    /// # #[cfg(feature = "async")]
23    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
24    /// let config = Config::new(Environment::Dev);
25    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
26    /// let client = RainClient::new(config, auth)?;
27    ///
28    /// let balance = client.get_balances().await?;
29    /// println!("Credit limit: {}", balance.credit_limit);
30    /// println!("Spending power: {}", balance.spending_power);
31    /// # Ok(())
32    /// # }
33    /// ```
34    #[cfg(feature = "async")]
35    pub async fn get_balances(&self) -> Result<BalanceResponse> {
36        let path = "/issuing/balances";
37        self.get(path).await
38    }
39
40    /// Get a company's credit balances
41    ///
42    /// # Arguments
43    ///
44    /// * `company_id` - The unique identifier of the company
45    ///
46    /// # Returns
47    ///
48    /// Returns a [`BalanceResponse`] containing the company's balance information.
49    ///
50    /// # Examples
51    ///
52    /// ```no_run
53    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
54    /// use uuid::Uuid;
55    ///
56    /// # #[cfg(feature = "async")]
57    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
58    /// let config = Config::new(Environment::Dev);
59    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
60    /// let client = RainClient::new(config, auth)?;
61    ///
62    /// let company_id = Uuid::new_v4();
63    /// let balance = client.get_company_balances(&company_id).await?;
64    /// println!("Credit limit: {}", balance.credit_limit);
65    /// # Ok(())
66    /// # }
67    /// ```
68    #[cfg(feature = "async")]
69    pub async fn get_company_balances(&self, company_id: &Uuid) -> Result<BalanceResponse> {
70        let path = format!("/issuing/companies/{company_id}/balances");
71        self.get(&path).await
72    }
73
74    /// Get a user's credit balances
75    ///
76    /// # Arguments
77    ///
78    /// * `user_id` - The unique identifier of the user
79    ///
80    /// # Returns
81    ///
82    /// Returns a [`BalanceResponse`] containing the user's balance information.
83    ///
84    /// # Examples
85    ///
86    /// ```no_run
87    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
88    /// use uuid::Uuid;
89    ///
90    /// # #[cfg(feature = "async")]
91    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
92    /// let config = Config::new(Environment::Dev);
93    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
94    /// let client = RainClient::new(config, auth)?;
95    ///
96    /// let user_id = Uuid::new_v4();
97    /// let balance = client.get_user_balances(&user_id).await?;
98    /// println!("Credit limit: {}", balance.credit_limit);
99    /// # Ok(())
100    /// # }
101    /// ```
102    #[cfg(feature = "async")]
103    pub async fn get_user_balances(&self, user_id: &Uuid) -> Result<BalanceResponse> {
104        let path = format!("/issuing/users/{user_id}/balances");
105        self.get(&path).await
106    }
107
108    // ============================================================================
109    // Blocking Methods
110    // ============================================================================
111
112    /// Get a tenant's credit balances (blocking)
113    #[cfg(feature = "sync")]
114    pub fn get_balances_blocking(&self) -> Result<BalanceResponse> {
115        let path = "/issuing/balances";
116        self.get_blocking(path)
117    }
118
119    /// Get a company's credit balances (blocking)
120    #[cfg(feature = "sync")]
121    pub fn get_company_balances_blocking(&self, company_id: &Uuid) -> Result<BalanceResponse> {
122        let path = format!("/issuing/companies/{company_id}/balances");
123        self.get_blocking(&path)
124    }
125
126    /// Get a user's credit balances (blocking)
127    #[cfg(feature = "sync")]
128    pub fn get_user_balances_blocking(&self, user_id: &Uuid) -> Result<BalanceResponse> {
129        let path = format!("/issuing/users/{user_id}/balances");
130        self.get_blocking(&path)
131    }
132}