rain_sdk/api/subtenants.rs
1//! Subtenants API
2//!
3//! This module provides functionality to manage subtenants.
4
5use crate::client::RainClient;
6use crate::error::Result;
7use crate::models::subtenants::*;
8use uuid::Uuid;
9
10impl RainClient {
11 /// Get all subtenants
12 ///
13 /// # Returns
14 ///
15 /// Returns a [`Vec<Subtenant>`] containing the list of subtenants.
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 subtenants = client.list_subtenants().await?;
35 /// println!("Found {} subtenants", subtenants.len());
36 /// # Ok(())
37 /// # }
38 /// ```
39 #[cfg(feature = "async")]
40 pub async fn list_subtenants(&self) -> Result<Vec<Subtenant>> {
41 let path = "/subtenants";
42 self.get(path).await
43 }
44
45 /// Create a subtenant
46 ///
47 /// # Arguments
48 ///
49 /// * `request` - The subtenant creation request
50 ///
51 /// # Returns
52 ///
53 /// Returns a [`Subtenant`] containing the created subtenant information.
54 ///
55 /// # Errors
56 ///
57 /// This method can return the following errors:
58 /// - `400` - Invalid request
59 /// - `401` - Invalid authorization
60 /// - `500` - Internal server error
61 ///
62 /// # Examples
63 ///
64 /// ```no_run
65 /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
66 /// use rain_sdk::models::subtenants::CreateSubtenantRequest;
67 ///
68 /// # #[cfg(feature = "async")]
69 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
70 /// let config = Config::new(Environment::Dev);
71 /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
72 /// let client = RainClient::new(config, auth)?;
73 ///
74 /// let request = CreateSubtenantRequest {
75 /// name: Some("My Subtenant".to_string()),
76 /// };
77 /// let subtenant = client.create_subtenant(&request).await?;
78 /// # Ok(())
79 /// # }
80 /// ```
81 #[cfg(feature = "async")]
82 pub async fn create_subtenant(&self, request: &CreateSubtenantRequest) -> Result<Subtenant> {
83 let path = "/subtenants";
84 self.post(path, request).await
85 }
86
87 /// Get a subtenant by its id
88 ///
89 /// # Arguments
90 ///
91 /// * `subtenant_id` - The unique identifier of the subtenant
92 ///
93 /// # Returns
94 ///
95 /// Returns a [`Subtenant`] containing the subtenant information.
96 ///
97 /// # Errors
98 ///
99 /// This method can return the following errors:
100 /// - `401` - Invalid authorization
101 /// - `404` - Subtenant not found
102 /// - `500` - Internal server error
103 #[cfg(feature = "async")]
104 pub async fn get_subtenant(&self, subtenant_id: &Uuid) -> Result<Subtenant> {
105 let path = format!("/subtenants/{subtenant_id}");
106 self.get(&path).await
107 }
108
109 /// Update a subtenant
110 ///
111 /// # Arguments
112 ///
113 /// * `subtenant_id` - The unique identifier of the subtenant
114 /// * `request` - The update request
115 ///
116 /// # Returns
117 ///
118 /// Returns success (204 No Content) with no response body.
119 ///
120 /// # Errors
121 ///
122 /// This method can return the following errors:
123 /// - `400` - Invalid request
124 /// - `401` - Invalid authorization
125 /// - `404` - Subtenant not found
126 /// - `500` - Internal server error
127 #[cfg(feature = "async")]
128 pub async fn update_subtenant(
129 &self,
130 subtenant_id: &Uuid,
131 request: &UpdateSubtenantRequest,
132 ) -> Result<()> {
133 let path = format!("/subtenants/{subtenant_id}");
134 let _: serde_json::Value = self.patch(&path, request).await?;
135 Ok(())
136 }
137
138 // ============================================================================
139 // Blocking Methods
140 // ============================================================================
141
142 /// Get all subtenants (blocking)
143 #[cfg(feature = "sync")]
144 pub fn list_subtenants_blocking(&self) -> Result<Vec<Subtenant>> {
145 let path = "/subtenants";
146 self.get_blocking(path)
147 }
148
149 /// Create a subtenant (blocking)
150 #[cfg(feature = "sync")]
151 pub fn create_subtenant_blocking(&self, request: &CreateSubtenantRequest) -> Result<Subtenant> {
152 let path = "/subtenants";
153 self.post_blocking(path, request)
154 }
155
156 /// Get a subtenant by its id (blocking)
157 #[cfg(feature = "sync")]
158 pub fn get_subtenant_blocking(&self, subtenant_id: &Uuid) -> Result<Subtenant> {
159 let path = format!("/subtenants/{subtenant_id}");
160 self.get_blocking(&path)
161 }
162
163 /// Update a subtenant (blocking)
164 #[cfg(feature = "sync")]
165 pub fn update_subtenant_blocking(
166 &self,
167 subtenant_id: &Uuid,
168 request: &UpdateSubtenantRequest,
169 ) -> Result<()> {
170 let path = format!("/subtenants/{subtenant_id}");
171 let _: serde_json::Value = self.patch_blocking(&path, request)?;
172 Ok(())
173 }
174}