1use std::sync::Arc;
12
13use async_trait::async_trait;
14#[cfg(feature = "mockall")]
15use mockall::automock;
16use reqwest;
17use serde::{Deserialize, Serialize, de::Error as _};
18
19use super::{Error, configuration};
20use crate::{
21 apis::{AuthRequired, ContentType, ResponseContent},
22 models,
23};
24
25#[cfg_attr(feature = "mockall", automock)]
26#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
27#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
28pub trait PoliciesApi: Send + Sync {
29 async fn get<'a>(
31 &self,
32 org_id: uuid::Uuid,
33 r#type: models::PolicyType,
34 ) -> Result<models::PolicyStatusResponseModel, Error<GetError>>;
35
36 async fn get_all<'a>(
38 &self,
39 org_id: &'a str,
40 ) -> Result<models::PolicyResponseModelListResponseModel, Error<GetAllError>>;
41
42 async fn get_by_token<'a>(
44 &self,
45 org_id: uuid::Uuid,
46 email: Option<&'a str>,
47 token: Option<&'a str>,
48 organization_user_id: Option<uuid::Uuid>,
49 ) -> Result<models::PolicyResponseModelListResponseModel, Error<GetByTokenError>>;
50
51 async fn get_master_password_policy<'a>(
53 &self,
54 org_id: uuid::Uuid,
55 ) -> Result<models::PolicyResponseModel, Error<GetMasterPasswordPolicyError>>;
56
57 async fn put<'a>(
59 &self,
60 org_id: uuid::Uuid,
61 r#type: models::PolicyType,
62 policy_request_model: Option<models::PolicyRequestModel>,
63 ) -> Result<models::PolicyResponseModel, Error<PutError>>;
64
65 async fn put_v_next<'a>(
67 &self,
68 org_id: uuid::Uuid,
69 r#type: models::PolicyType,
70 save_policy_request: Option<models::SavePolicyRequest>,
71 ) -> Result<models::PolicyResponseModel, Error<PutVNextError>>;
72}
73
74pub struct PoliciesApiClient {
75 configuration: Arc<configuration::Configuration>,
76}
77
78impl PoliciesApiClient {
79 pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
80 Self { configuration }
81 }
82}
83
84#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
85#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
86impl PoliciesApi for PoliciesApiClient {
87 async fn get<'a>(
88 &self,
89 org_id: uuid::Uuid,
90 r#type: models::PolicyType,
91 ) -> Result<models::PolicyStatusResponseModel, Error<GetError>> {
92 let local_var_configuration = &self.configuration;
93
94 let local_var_client = &local_var_configuration.client;
95
96 let local_var_uri_str = format!("{}/organizations/{orgId}/policies/{type}", local_var_configuration.base_path, orgId=org_id, type=r#type.to_string());
97 let mut local_var_req_builder =
98 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
99
100 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
101
102 let local_var_resp = local_var_req_builder.send().await?;
103
104 let local_var_status = local_var_resp.status();
105 let local_var_content_type = local_var_resp
106 .headers()
107 .get("content-type")
108 .and_then(|v| v.to_str().ok())
109 .unwrap_or("application/octet-stream");
110 let local_var_content_type = super::ContentType::from(local_var_content_type);
111 let local_var_content = local_var_resp.text().await?;
112
113 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
114 match local_var_content_type {
115 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
116 ContentType::Text => {
117 return Err(Error::from(serde_json::Error::custom(
118 "Received `text/plain` content type response that cannot be converted to `models::PolicyStatusResponseModel`",
119 )));
120 }
121 ContentType::Unsupported(local_var_unknown_type) => {
122 return Err(Error::from(serde_json::Error::custom(format!(
123 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PolicyStatusResponseModel`"
124 ))));
125 }
126 }
127 } else {
128 let local_var_entity: Option<GetError> = serde_json::from_str(&local_var_content).ok();
129 let local_var_error = ResponseContent {
130 status: local_var_status,
131 content: local_var_content,
132 entity: local_var_entity,
133 };
134 Err(Error::ResponseError(local_var_error))
135 }
136 }
137
138 async fn get_all<'a>(
139 &self,
140 org_id: &'a str,
141 ) -> Result<models::PolicyResponseModelListResponseModel, Error<GetAllError>> {
142 let local_var_configuration = &self.configuration;
143
144 let local_var_client = &local_var_configuration.client;
145
146 let local_var_uri_str = format!(
147 "{}/organizations/{orgId}/policies",
148 local_var_configuration.base_path,
149 orgId = crate::apis::urlencode(org_id)
150 );
151 let mut local_var_req_builder =
152 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
153
154 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
155
156 let local_var_resp = local_var_req_builder.send().await?;
157
158 let local_var_status = local_var_resp.status();
159 let local_var_content_type = local_var_resp
160 .headers()
161 .get("content-type")
162 .and_then(|v| v.to_str().ok())
163 .unwrap_or("application/octet-stream");
164 let local_var_content_type = super::ContentType::from(local_var_content_type);
165 let local_var_content = local_var_resp.text().await?;
166
167 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
168 match local_var_content_type {
169 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
170 ContentType::Text => {
171 return Err(Error::from(serde_json::Error::custom(
172 "Received `text/plain` content type response that cannot be converted to `models::PolicyResponseModelListResponseModel`",
173 )));
174 }
175 ContentType::Unsupported(local_var_unknown_type) => {
176 return Err(Error::from(serde_json::Error::custom(format!(
177 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PolicyResponseModelListResponseModel`"
178 ))));
179 }
180 }
181 } else {
182 let local_var_entity: Option<GetAllError> =
183 serde_json::from_str(&local_var_content).ok();
184 let local_var_error = ResponseContent {
185 status: local_var_status,
186 content: local_var_content,
187 entity: local_var_entity,
188 };
189 Err(Error::ResponseError(local_var_error))
190 }
191 }
192
193 async fn get_by_token<'a>(
194 &self,
195 org_id: uuid::Uuid,
196 email: Option<&'a str>,
197 token: Option<&'a str>,
198 organization_user_id: Option<uuid::Uuid>,
199 ) -> Result<models::PolicyResponseModelListResponseModel, Error<GetByTokenError>> {
200 let local_var_configuration = &self.configuration;
201
202 let local_var_client = &local_var_configuration.client;
203
204 let local_var_uri_str = format!(
205 "{}/organizations/{orgId}/policies/token",
206 local_var_configuration.base_path,
207 orgId = org_id
208 );
209 let mut local_var_req_builder =
210 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
211
212 if let Some(ref param_value) = email {
213 local_var_req_builder =
214 local_var_req_builder.query(&[("email", ¶m_value.to_string())]);
215 }
216 if let Some(ref param_value) = token {
217 local_var_req_builder =
218 local_var_req_builder.query(&[("token", ¶m_value.to_string())]);
219 }
220 if let Some(ref param_value) = organization_user_id {
221 local_var_req_builder =
222 local_var_req_builder.query(&[("organizationUserId", ¶m_value.to_string())]);
223 }
224 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
225
226 let local_var_resp = local_var_req_builder.send().await?;
227
228 let local_var_status = local_var_resp.status();
229 let local_var_content_type = local_var_resp
230 .headers()
231 .get("content-type")
232 .and_then(|v| v.to_str().ok())
233 .unwrap_or("application/octet-stream");
234 let local_var_content_type = super::ContentType::from(local_var_content_type);
235 let local_var_content = local_var_resp.text().await?;
236
237 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
238 match local_var_content_type {
239 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
240 ContentType::Text => {
241 return Err(Error::from(serde_json::Error::custom(
242 "Received `text/plain` content type response that cannot be converted to `models::PolicyResponseModelListResponseModel`",
243 )));
244 }
245 ContentType::Unsupported(local_var_unknown_type) => {
246 return Err(Error::from(serde_json::Error::custom(format!(
247 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PolicyResponseModelListResponseModel`"
248 ))));
249 }
250 }
251 } else {
252 let local_var_entity: Option<GetByTokenError> =
253 serde_json::from_str(&local_var_content).ok();
254 let local_var_error = ResponseContent {
255 status: local_var_status,
256 content: local_var_content,
257 entity: local_var_entity,
258 };
259 Err(Error::ResponseError(local_var_error))
260 }
261 }
262
263 async fn get_master_password_policy<'a>(
264 &self,
265 org_id: uuid::Uuid,
266 ) -> Result<models::PolicyResponseModel, Error<GetMasterPasswordPolicyError>> {
267 let local_var_configuration = &self.configuration;
268
269 let local_var_client = &local_var_configuration.client;
270
271 let local_var_uri_str = format!(
272 "{}/organizations/{orgId}/policies/master-password",
273 local_var_configuration.base_path,
274 orgId = org_id
275 );
276 let mut local_var_req_builder =
277 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
278
279 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
280
281 let local_var_resp = local_var_req_builder.send().await?;
282
283 let local_var_status = local_var_resp.status();
284 let local_var_content_type = local_var_resp
285 .headers()
286 .get("content-type")
287 .and_then(|v| v.to_str().ok())
288 .unwrap_or("application/octet-stream");
289 let local_var_content_type = super::ContentType::from(local_var_content_type);
290 let local_var_content = local_var_resp.text().await?;
291
292 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
293 match local_var_content_type {
294 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
295 ContentType::Text => {
296 return Err(Error::from(serde_json::Error::custom(
297 "Received `text/plain` content type response that cannot be converted to `models::PolicyResponseModel`",
298 )));
299 }
300 ContentType::Unsupported(local_var_unknown_type) => {
301 return Err(Error::from(serde_json::Error::custom(format!(
302 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PolicyResponseModel`"
303 ))));
304 }
305 }
306 } else {
307 let local_var_entity: Option<GetMasterPasswordPolicyError> =
308 serde_json::from_str(&local_var_content).ok();
309 let local_var_error = ResponseContent {
310 status: local_var_status,
311 content: local_var_content,
312 entity: local_var_entity,
313 };
314 Err(Error::ResponseError(local_var_error))
315 }
316 }
317
318 async fn put<'a>(
319 &self,
320 org_id: uuid::Uuid,
321 r#type: models::PolicyType,
322 policy_request_model: Option<models::PolicyRequestModel>,
323 ) -> Result<models::PolicyResponseModel, Error<PutError>> {
324 let local_var_configuration = &self.configuration;
325
326 let local_var_client = &local_var_configuration.client;
327
328 let local_var_uri_str = format!("{}/organizations/{orgId}/policies/{type}", local_var_configuration.base_path, orgId=org_id, type=r#type.to_string());
329 let mut local_var_req_builder =
330 local_var_client.request(reqwest::Method::PUT, local_var_uri_str.as_str());
331
332 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
333 local_var_req_builder = local_var_req_builder.json(&policy_request_model);
334
335 let local_var_resp = local_var_req_builder.send().await?;
336
337 let local_var_status = local_var_resp.status();
338 let local_var_content_type = local_var_resp
339 .headers()
340 .get("content-type")
341 .and_then(|v| v.to_str().ok())
342 .unwrap_or("application/octet-stream");
343 let local_var_content_type = super::ContentType::from(local_var_content_type);
344 let local_var_content = local_var_resp.text().await?;
345
346 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
347 match local_var_content_type {
348 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
349 ContentType::Text => {
350 return Err(Error::from(serde_json::Error::custom(
351 "Received `text/plain` content type response that cannot be converted to `models::PolicyResponseModel`",
352 )));
353 }
354 ContentType::Unsupported(local_var_unknown_type) => {
355 return Err(Error::from(serde_json::Error::custom(format!(
356 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PolicyResponseModel`"
357 ))));
358 }
359 }
360 } else {
361 let local_var_entity: Option<PutError> = serde_json::from_str(&local_var_content).ok();
362 let local_var_error = ResponseContent {
363 status: local_var_status,
364 content: local_var_content,
365 entity: local_var_entity,
366 };
367 Err(Error::ResponseError(local_var_error))
368 }
369 }
370
371 async fn put_v_next<'a>(
372 &self,
373 org_id: uuid::Uuid,
374 r#type: models::PolicyType,
375 save_policy_request: Option<models::SavePolicyRequest>,
376 ) -> Result<models::PolicyResponseModel, Error<PutVNextError>> {
377 let local_var_configuration = &self.configuration;
378
379 let local_var_client = &local_var_configuration.client;
380
381 let local_var_uri_str = format!("{}/organizations/{orgId}/policies/{type}/vnext", local_var_configuration.base_path, orgId=org_id, type=r#type.to_string());
382 let mut local_var_req_builder =
383 local_var_client.request(reqwest::Method::PUT, local_var_uri_str.as_str());
384
385 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
386 local_var_req_builder = local_var_req_builder.json(&save_policy_request);
387
388 let local_var_resp = local_var_req_builder.send().await?;
389
390 let local_var_status = local_var_resp.status();
391 let local_var_content_type = local_var_resp
392 .headers()
393 .get("content-type")
394 .and_then(|v| v.to_str().ok())
395 .unwrap_or("application/octet-stream");
396 let local_var_content_type = super::ContentType::from(local_var_content_type);
397 let local_var_content = local_var_resp.text().await?;
398
399 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
400 match local_var_content_type {
401 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
402 ContentType::Text => {
403 return Err(Error::from(serde_json::Error::custom(
404 "Received `text/plain` content type response that cannot be converted to `models::PolicyResponseModel`",
405 )));
406 }
407 ContentType::Unsupported(local_var_unknown_type) => {
408 return Err(Error::from(serde_json::Error::custom(format!(
409 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PolicyResponseModel`"
410 ))));
411 }
412 }
413 } else {
414 let local_var_entity: Option<PutVNextError> =
415 serde_json::from_str(&local_var_content).ok();
416 let local_var_error = ResponseContent {
417 status: local_var_status,
418 content: local_var_content,
419 entity: local_var_entity,
420 };
421 Err(Error::ResponseError(local_var_error))
422 }
423 }
424}
425
426#[derive(Debug, Clone, Serialize, Deserialize)]
428#[serde(untagged)]
429pub enum GetError {
430 UnknownValue(serde_json::Value),
431}
432#[derive(Debug, Clone, Serialize, Deserialize)]
434#[serde(untagged)]
435pub enum GetAllError {
436 UnknownValue(serde_json::Value),
437}
438#[derive(Debug, Clone, Serialize, Deserialize)]
440#[serde(untagged)]
441pub enum GetByTokenError {
442 UnknownValue(serde_json::Value),
443}
444#[derive(Debug, Clone, Serialize, Deserialize)]
446#[serde(untagged)]
447pub enum GetMasterPasswordPolicyError {
448 UnknownValue(serde_json::Value),
449}
450#[derive(Debug, Clone, Serialize, Deserialize)]
452#[serde(untagged)]
453pub enum PutError {
454 UnknownValue(serde_json::Value),
455}
456#[derive(Debug, Clone, Serialize, Deserialize)]
458#[serde(untagged)]
459pub enum PutVNextError {
460 UnknownValue(serde_json::Value),
461}