rain_sdk/api/
companies.rs

1//! Companies API
2//!
3//! This module provides functionality to manage companies.
4
5use crate::client::RainClient;
6use crate::error::Result;
7use crate::models::charges::*;
8use crate::models::companies::*;
9use uuid::Uuid;
10
11impl RainClient {
12    /// Get all companies
13    ///
14    /// # Arguments
15    ///
16    /// * `params` - Query parameters for filtering companies
17    ///
18    /// # Returns
19    ///
20    /// Returns a [`Vec<Company>`] containing the list of companies.
21    ///
22    /// # Errors
23    ///
24    /// This method can return the following errors:
25    /// - `401` - Invalid authorization
26    /// - `500` - Internal server error
27    ///
28    /// # Examples
29    ///
30    /// ```no_run
31    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
32    /// use rain_sdk::models::companies::ListCompaniesParams;
33    ///
34    /// # #[cfg(feature = "async")]
35    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
36    /// let config = Config::new(Environment::Dev);
37    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
38    /// let client = RainClient::new(config, auth)?;
39    ///
40    /// let params = ListCompaniesParams {
41    ///     cursor: None,
42    ///     limit: Some(20),
43    /// };
44    /// let companies = client.list_companies(&params).await?;
45    /// # Ok(())
46    /// # }
47    /// ```
48    #[cfg(feature = "async")]
49    pub async fn list_companies(&self, params: &ListCompaniesParams) -> Result<Vec<Company>> {
50        let mut path = "/companies".to_string();
51        let mut query_parts = Vec::new();
52
53        if let Some(ref cursor) = params.cursor {
54            query_parts.push(format!("cursor={cursor}"));
55        }
56        if let Some(limit) = params.limit {
57            query_parts.push(format!("limit={limit}"));
58        }
59
60        if !query_parts.is_empty() {
61            path.push('?');
62            path.push_str(&query_parts.join("&"));
63        }
64
65        self.get(&path).await
66    }
67
68    /// Get a company by its ID
69    ///
70    /// # Arguments
71    ///
72    /// * `company_id` - The unique identifier of the company
73    ///
74    /// # Returns
75    ///
76    /// Returns a [`Company`] containing the company information.
77    ///
78    /// # Errors
79    ///
80    /// This method can return the following errors:
81    /// - `401` - Invalid authorization
82    /// - `404` - Team not found
83    /// - `500` - Internal server error
84    ///
85    /// # Examples
86    ///
87    /// ```no_run
88    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
89    /// use uuid::Uuid;
90    ///
91    /// # #[cfg(feature = "async")]
92    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
93    /// let config = Config::new(Environment::Dev);
94    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
95    /// let client = RainClient::new(config, auth)?;
96    ///
97    /// let company_id = Uuid::new_v4();
98    /// let company = client.get_company(&company_id).await?;
99    /// # Ok(())
100    /// # }
101    /// ```
102    #[cfg(feature = "async")]
103    pub async fn get_company(&self, company_id: &Uuid) -> Result<Company> {
104        let path = format!("/companies/{company_id}");
105        self.get(&path).await
106    }
107
108    /// Update a company
109    ///
110    /// # Arguments
111    ///
112    /// * `company_id` - The unique identifier of the company
113    /// * `request` - The update request
114    ///
115    /// # Returns
116    ///
117    /// Returns a [`Company`] containing the updated company information.
118    ///
119    /// # Errors
120    ///
121    /// This method can return the following errors:
122    /// - `400` - Invalid request
123    /// - `401` - Invalid authorization
124    /// - `404` - Team not found
125    /// - `500` - Internal server error
126    ///
127    /// # Examples
128    ///
129    /// ```no_run
130    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
131    /// use rain_sdk::models::companies::UpdateCompanyRequest;
132    /// use uuid::Uuid;
133    ///
134    /// # #[cfg(feature = "async")]
135    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
136    /// let config = Config::new(Environment::Dev);
137    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
138    /// let client = RainClient::new(config, auth)?;
139    ///
140    /// let company_id = Uuid::new_v4();
141    /// let request = UpdateCompanyRequest {
142    ///     name: Some("New Company Name".to_string()),
143    ///     address: None,
144    /// };
145    /// let company = client.update_company(&company_id, &request).await?;
146    /// # Ok(())
147    /// # }
148    /// ```
149    #[cfg(feature = "async")]
150    pub async fn update_company(
151        &self,
152        company_id: &Uuid,
153        request: &UpdateCompanyRequest,
154    ) -> Result<Company> {
155        let path = format!("/companies/{company_id}");
156        self.patch(&path, request).await
157    }
158
159    /// Charge a company a custom fee
160    ///
161    /// # Arguments
162    ///
163    /// * `company_id` - The unique identifier of the company
164    /// * `request` - The charge request
165    ///
166    /// # Returns
167    ///
168    /// Returns a [`Charge`] containing the created charge information.
169    ///
170    /// # Errors
171    ///
172    /// This method can return the following errors:
173    /// - `400` - Invalid request
174    /// - `401` - Invalid authorization
175    /// - `404` - Company not found
176    /// - `500` - Internal server error
177    ///
178    /// # Examples
179    ///
180    /// ```no_run
181    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
182    /// use rain_sdk::models::charges::CreateChargeRequest;
183    /// use uuid::Uuid;
184    ///
185    /// # #[cfg(feature = "async")]
186    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
187    /// let config = Config::new(Environment::Dev);
188    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
189    /// let client = RainClient::new(config, auth)?;
190    ///
191    /// let company_id = Uuid::new_v4();
192    /// let request = CreateChargeRequest {
193    ///     amount: 1000, // $10.00 in cents
194    ///     description: "Custom fee".to_string(),
195    /// };
196    /// let charge = client.charge_company(&company_id, &request).await?;
197    /// # Ok(())
198    /// # }
199    /// ```
200    #[cfg(feature = "async")]
201    pub async fn charge_company(
202        &self,
203        company_id: &Uuid,
204        request: &CreateChargeRequest,
205    ) -> Result<Charge> {
206        let path = format!("/companies/{company_id}/charges");
207        self.post(&path, request).await
208    }
209
210    /// Create a user in a company
211    ///
212    /// # Arguments
213    ///
214    /// * `company_id` - The unique identifier of the company
215    /// * `request` - The user creation request
216    ///
217    /// # Returns
218    ///
219    /// Returns a [`crate::models::User`] containing the created user information.
220    ///
221    /// # Errors
222    ///
223    /// This method can return the following errors:
224    /// - `400` - Invalid request
225    /// - `401` - Invalid authorization
226    /// - `404` - Company not found
227    /// - `500` - Internal server error
228    ///
229    /// # Examples
230    ///
231    /// ```no_run
232    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
233    /// use rain_sdk::models::users::CreateCompanyUserRequest;
234    /// use uuid::Uuid;
235    ///
236    /// # #[cfg(feature = "async")]
237    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
238    /// let config = Config::new(Environment::Dev);
239    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
240    /// let client = RainClient::new(config, auth)?;
241    ///
242    /// let company_id = Uuid::new_v4();
243    /// let request = CreateCompanyUserRequest {
244    ///     first_name: "John".to_string(),
245    ///     last_name: "Doe".to_string(),
246    ///     email: "john@example.com".to_string(),
247    ///     is_terms_of_service_accepted: true,
248    ///     birth_date: None,
249    ///     wallet_address: None,
250    ///     solana_address: None,
251    ///     address: None,
252    ///     phone_country_code: None,
253    ///     phone_number: None,
254    /// };
255    /// let user = client.create_company_user(&company_id, &request).await?;
256    /// # Ok(())
257    /// # }
258    /// ```
259    #[cfg(feature = "async")]
260    pub async fn create_company_user(
261        &self,
262        company_id: &Uuid,
263        request: &crate::models::users::CreateCompanyUserRequest,
264    ) -> Result<crate::models::users::User> {
265        let path = format!("/companies/{company_id}/users");
266        self.post(&path, request).await
267    }
268
269    // ============================================================================
270    // Blocking Methods
271    // ============================================================================
272
273    /// Get all companies (blocking)
274    #[cfg(feature = "sync")]
275    pub fn list_companies_blocking(&self, params: &ListCompaniesParams) -> Result<Vec<Company>> {
276        let mut path = "/companies".to_string();
277        let mut query_parts = Vec::new();
278
279        if let Some(ref cursor) = params.cursor {
280            query_parts.push(format!("cursor={cursor}"));
281        }
282        if let Some(limit) = params.limit {
283            query_parts.push(format!("limit={limit}"));
284        }
285
286        if !query_parts.is_empty() {
287            path.push('?');
288            path.push_str(&query_parts.join("&"));
289        }
290
291        self.get_blocking(&path)
292    }
293
294    /// Get a company by its ID (blocking)
295    #[cfg(feature = "sync")]
296    pub fn get_company_blocking(&self, company_id: &Uuid) -> Result<Company> {
297        let path = format!("/companies/{company_id}");
298        self.get_blocking(&path)
299    }
300
301    /// Update a company (blocking)
302    #[cfg(feature = "sync")]
303    pub fn update_company_blocking(
304        &self,
305        company_id: &Uuid,
306        request: &UpdateCompanyRequest,
307    ) -> Result<Company> {
308        let path = format!("/companies/{company_id}");
309        self.patch_blocking(&path, request)
310    }
311
312    /// Charge a company a custom fee (blocking)
313    #[cfg(feature = "sync")]
314    pub fn charge_company_blocking(
315        &self,
316        company_id: &Uuid,
317        request: &CreateChargeRequest,
318    ) -> Result<Charge> {
319        let path = format!("/companies/{company_id}/charges");
320        self.post_blocking(&path, request)
321    }
322
323    /// Create a user in a company (blocking)
324    #[cfg(feature = "sync")]
325    pub fn create_company_user_blocking(
326        &self,
327        company_id: &Uuid,
328        request: &crate::models::users::CreateCompanyUserRequest,
329    ) -> Result<crate::models::users::User> {
330        let path = format!("/companies/{company_id}/users");
331        self.post_blocking(&path, request)
332    }
333}