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    /// # Errors
18    ///
19    /// This method can return the following errors:
20    /// - `401` - Invalid authorization
21    /// - `500` - Internal server error
22    ///
23    /// # Examples
24    ///
25    /// ```no_run
26    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
27    ///
28    /// # #[cfg(feature = "async")]
29    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
30    /// let config = Config::new(Environment::Dev);
31    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
32    /// let client = RainClient::new(config, auth)?;
33    ///
34    /// let balance = client.get_balances().await?;
35    /// println!("Credit limit: {}", balance.credit_limit);
36    /// println!("Spending power: {}", balance.spending_power);
37    /// # Ok(())
38    /// # }
39    /// ```
40    #[cfg(feature = "async")]
41    pub async fn get_balances(&self) -> Result<BalanceResponse> {
42        let path = "/balances";
43        self.get(path).await
44    }
45
46    /// Get a company's credit balances
47    ///
48    /// # Arguments
49    ///
50    /// * `company_id` - The unique identifier of the company
51    ///
52    /// # Returns
53    ///
54    /// Returns a [`BalanceResponse`] containing the company's balance information.
55    ///
56    /// # Errors
57    ///
58    /// This method can return the following errors:
59    /// - `401` - Invalid authorization
60    /// - `404` - Company not found
61    /// - `500` - Internal server error
62    ///
63    /// # Examples
64    ///
65    /// ```no_run
66    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
67    /// use uuid::Uuid;
68    ///
69    /// # #[cfg(feature = "async")]
70    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
71    /// let config = Config::new(Environment::Dev);
72    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
73    /// let client = RainClient::new(config, auth)?;
74    ///
75    /// let company_id = Uuid::new_v4();
76    /// let balance = client.get_company_balances(&company_id).await?;
77    /// println!("Credit limit: {}", balance.credit_limit);
78    /// # Ok(())
79    /// # }
80    /// ```
81    #[cfg(feature = "async")]
82    pub async fn get_company_balances(&self, company_id: &Uuid) -> Result<BalanceResponse> {
83        let path = format!("/companies/{company_id}/balances");
84        self.get(&path).await
85    }
86
87    /// Get a user's credit balances
88    ///
89    /// # Arguments
90    ///
91    /// * `user_id` - The unique identifier of the user
92    ///
93    /// # Returns
94    ///
95    /// Returns a [`BalanceResponse`] containing the user's balance information.
96    ///
97    /// # Errors
98    ///
99    /// This method can return the following errors:
100    /// - `401` - Invalid authorization
101    /// - `404` - User not found
102    /// - `500` - Internal server error
103    ///
104    /// # Examples
105    ///
106    /// ```no_run
107    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
108    /// use uuid::Uuid;
109    ///
110    /// # #[cfg(feature = "async")]
111    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
112    /// let config = Config::new(Environment::Dev);
113    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
114    /// let client = RainClient::new(config, auth)?;
115    ///
116    /// let user_id = Uuid::new_v4();
117    /// let balance = client.get_user_balances(&user_id).await?;
118    /// println!("Credit limit: {}", balance.credit_limit);
119    /// # Ok(())
120    /// # }
121    /// ```
122    #[cfg(feature = "async")]
123    pub async fn get_user_balances(&self, user_id: &Uuid) -> Result<BalanceResponse> {
124        let path = format!("/users/{user_id}/balances");
125        self.get(&path).await
126    }
127
128    // ============================================================================
129    // Blocking Methods
130    // ============================================================================
131
132    /// Get a tenant's credit balances (blocking)
133    #[cfg(feature = "sync")]
134    pub fn get_balances_blocking(&self) -> Result<BalanceResponse> {
135        let path = "/balances";
136        self.get_blocking(path)
137    }
138
139    /// Get a company's credit balances (blocking)
140    #[cfg(feature = "sync")]
141    pub fn get_company_balances_blocking(&self, company_id: &Uuid) -> Result<BalanceResponse> {
142        let path = format!("/companies/{company_id}/balances");
143        self.get_blocking(&path)
144    }
145
146    /// Get a user's credit balances (blocking)
147    #[cfg(feature = "sync")]
148    pub fn get_user_balances_blocking(&self, user_id: &Uuid) -> Result<BalanceResponse> {
149        let path = format!("/users/{user_id}/balances");
150        self.get_blocking(&path)
151    }
152}