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