bitwarden_api_api/apis/
policies_api.rs

1/*
2 * Bitwarden Internal API
3 *
4 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
5 *
6 * The version of the OpenAPI document: latest
7 *
8 * Generated by: https://openapi-generator.tech
9 */
10
11use 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::{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    /// GET /organizations/{orgId}/policies/{type}
30    async fn get<'a>(
31        &self,
32        org_id: uuid::Uuid,
33        r#type: i32,
34    ) -> Result<models::PolicyDetailResponseModel, Error<GetError>>;
35
36    /// GET /organizations/{orgId}/policies
37    async fn get_all<'a>(
38        &self,
39        org_id: &'a str,
40    ) -> Result<models::PolicyResponseModelListResponseModel, Error<GetAllError>>;
41
42    /// GET /organizations/{orgId}/policies/token
43    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    /// GET /organizations/{orgId}/policies/master-password
52    async fn get_master_password_policy<'a>(
53        &self,
54        org_id: uuid::Uuid,
55    ) -> Result<models::PolicyResponseModel, Error<GetMasterPasswordPolicyError>>;
56
57    /// PUT /organizations/{orgId}/policies/{type}
58    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    /// PUT /organizations/{orgId}/policies/{type}/vnext
66    async fn put_v_next<'a>(
67        &self,
68        org_id: uuid::Uuid,
69        r#type: &'a str,
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: i32,
91    ) -> Result<models::PolicyDetailResponseModel, 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);
97        let mut local_var_req_builder =
98            local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
99
100        if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
101            local_var_req_builder = local_var_req_builder
102                .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
103        }
104        if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
105            local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
106        };
107
108        let local_var_req = local_var_req_builder.build()?;
109        let local_var_resp = local_var_client.execute(local_var_req).await?;
110
111        let local_var_status = local_var_resp.status();
112        let local_var_content_type = local_var_resp
113            .headers()
114            .get("content-type")
115            .and_then(|v| v.to_str().ok())
116            .unwrap_or("application/octet-stream");
117        let local_var_content_type = super::ContentType::from(local_var_content_type);
118        let local_var_content = local_var_resp.text().await?;
119
120        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
121            match local_var_content_type {
122                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
123                ContentType::Text => {
124                    return Err(Error::from(serde_json::Error::custom(
125                        "Received `text/plain` content type response that cannot be converted to `models::PolicyDetailResponseModel`",
126                    )));
127                }
128                ContentType::Unsupported(local_var_unknown_type) => {
129                    return Err(Error::from(serde_json::Error::custom(format!(
130                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PolicyDetailResponseModel`"
131                    ))));
132                }
133            }
134        } else {
135            let local_var_entity: Option<GetError> = serde_json::from_str(&local_var_content).ok();
136            let local_var_error = ResponseContent {
137                status: local_var_status,
138                content: local_var_content,
139                entity: local_var_entity,
140            };
141            Err(Error::ResponseError(local_var_error))
142        }
143    }
144
145    async fn get_all<'a>(
146        &self,
147        org_id: &'a str,
148    ) -> Result<models::PolicyResponseModelListResponseModel, Error<GetAllError>> {
149        let local_var_configuration = &self.configuration;
150
151        let local_var_client = &local_var_configuration.client;
152
153        let local_var_uri_str = format!(
154            "{}/organizations/{orgId}/policies",
155            local_var_configuration.base_path,
156            orgId = crate::apis::urlencode(org_id)
157        );
158        let mut local_var_req_builder =
159            local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
160
161        if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
162            local_var_req_builder = local_var_req_builder
163                .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
164        }
165        if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
166            local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
167        };
168
169        let local_var_req = local_var_req_builder.build()?;
170        let local_var_resp = local_var_client.execute(local_var_req).await?;
171
172        let local_var_status = local_var_resp.status();
173        let local_var_content_type = local_var_resp
174            .headers()
175            .get("content-type")
176            .and_then(|v| v.to_str().ok())
177            .unwrap_or("application/octet-stream");
178        let local_var_content_type = super::ContentType::from(local_var_content_type);
179        let local_var_content = local_var_resp.text().await?;
180
181        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
182            match local_var_content_type {
183                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
184                ContentType::Text => {
185                    return Err(Error::from(serde_json::Error::custom(
186                        "Received `text/plain` content type response that cannot be converted to `models::PolicyResponseModelListResponseModel`",
187                    )));
188                }
189                ContentType::Unsupported(local_var_unknown_type) => {
190                    return Err(Error::from(serde_json::Error::custom(format!(
191                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PolicyResponseModelListResponseModel`"
192                    ))));
193                }
194            }
195        } else {
196            let local_var_entity: Option<GetAllError> =
197                serde_json::from_str(&local_var_content).ok();
198            let local_var_error = ResponseContent {
199                status: local_var_status,
200                content: local_var_content,
201                entity: local_var_entity,
202            };
203            Err(Error::ResponseError(local_var_error))
204        }
205    }
206
207    async fn get_by_token<'a>(
208        &self,
209        org_id: uuid::Uuid,
210        email: Option<&'a str>,
211        token: Option<&'a str>,
212        organization_user_id: Option<uuid::Uuid>,
213    ) -> Result<models::PolicyResponseModelListResponseModel, Error<GetByTokenError>> {
214        let local_var_configuration = &self.configuration;
215
216        let local_var_client = &local_var_configuration.client;
217
218        let local_var_uri_str = format!(
219            "{}/organizations/{orgId}/policies/token",
220            local_var_configuration.base_path,
221            orgId = org_id
222        );
223        let mut local_var_req_builder =
224            local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
225
226        if let Some(ref param_value) = email {
227            local_var_req_builder =
228                local_var_req_builder.query(&[("email", &param_value.to_string())]);
229        }
230        if let Some(ref param_value) = token {
231            local_var_req_builder =
232                local_var_req_builder.query(&[("token", &param_value.to_string())]);
233        }
234        if let Some(ref param_value) = organization_user_id {
235            local_var_req_builder =
236                local_var_req_builder.query(&[("organizationUserId", &param_value.to_string())]);
237        }
238        if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
239            local_var_req_builder = local_var_req_builder
240                .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
241        }
242        if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
243            local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
244        };
245
246        let local_var_req = local_var_req_builder.build()?;
247        let local_var_resp = local_var_client.execute(local_var_req).await?;
248
249        let local_var_status = local_var_resp.status();
250        let local_var_content_type = local_var_resp
251            .headers()
252            .get("content-type")
253            .and_then(|v| v.to_str().ok())
254            .unwrap_or("application/octet-stream");
255        let local_var_content_type = super::ContentType::from(local_var_content_type);
256        let local_var_content = local_var_resp.text().await?;
257
258        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
259            match local_var_content_type {
260                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
261                ContentType::Text => {
262                    return Err(Error::from(serde_json::Error::custom(
263                        "Received `text/plain` content type response that cannot be converted to `models::PolicyResponseModelListResponseModel`",
264                    )));
265                }
266                ContentType::Unsupported(local_var_unknown_type) => {
267                    return Err(Error::from(serde_json::Error::custom(format!(
268                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PolicyResponseModelListResponseModel`"
269                    ))));
270                }
271            }
272        } else {
273            let local_var_entity: Option<GetByTokenError> =
274                serde_json::from_str(&local_var_content).ok();
275            let local_var_error = ResponseContent {
276                status: local_var_status,
277                content: local_var_content,
278                entity: local_var_entity,
279            };
280            Err(Error::ResponseError(local_var_error))
281        }
282    }
283
284    async fn get_master_password_policy<'a>(
285        &self,
286        org_id: uuid::Uuid,
287    ) -> Result<models::PolicyResponseModel, Error<GetMasterPasswordPolicyError>> {
288        let local_var_configuration = &self.configuration;
289
290        let local_var_client = &local_var_configuration.client;
291
292        let local_var_uri_str = format!(
293            "{}/organizations/{orgId}/policies/master-password",
294            local_var_configuration.base_path,
295            orgId = org_id
296        );
297        let mut local_var_req_builder =
298            local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
299
300        if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
301            local_var_req_builder = local_var_req_builder
302                .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
303        }
304        if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
305            local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
306        };
307
308        let local_var_req = local_var_req_builder.build()?;
309        let local_var_resp = local_var_client.execute(local_var_req).await?;
310
311        let local_var_status = local_var_resp.status();
312        let local_var_content_type = local_var_resp
313            .headers()
314            .get("content-type")
315            .and_then(|v| v.to_str().ok())
316            .unwrap_or("application/octet-stream");
317        let local_var_content_type = super::ContentType::from(local_var_content_type);
318        let local_var_content = local_var_resp.text().await?;
319
320        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
321            match local_var_content_type {
322                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
323                ContentType::Text => {
324                    return Err(Error::from(serde_json::Error::custom(
325                        "Received `text/plain` content type response that cannot be converted to `models::PolicyResponseModel`",
326                    )));
327                }
328                ContentType::Unsupported(local_var_unknown_type) => {
329                    return Err(Error::from(serde_json::Error::custom(format!(
330                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PolicyResponseModel`"
331                    ))));
332                }
333            }
334        } else {
335            let local_var_entity: Option<GetMasterPasswordPolicyError> =
336                serde_json::from_str(&local_var_content).ok();
337            let local_var_error = ResponseContent {
338                status: local_var_status,
339                content: local_var_content,
340                entity: local_var_entity,
341            };
342            Err(Error::ResponseError(local_var_error))
343        }
344    }
345
346    async fn put<'a>(
347        &self,
348        org_id: uuid::Uuid,
349        r#type: models::PolicyType,
350        policy_request_model: Option<models::PolicyRequestModel>,
351    ) -> Result<models::PolicyResponseModel, Error<PutError>> {
352        let local_var_configuration = &self.configuration;
353
354        let local_var_client = &local_var_configuration.client;
355
356        let local_var_uri_str = format!("{}/organizations/{orgId}/policies/{type}", local_var_configuration.base_path, orgId=org_id, type=r#type.to_string());
357        let mut local_var_req_builder =
358            local_var_client.request(reqwest::Method::PUT, local_var_uri_str.as_str());
359
360        if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
361            local_var_req_builder = local_var_req_builder
362                .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
363        }
364        if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
365            local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
366        };
367        local_var_req_builder = local_var_req_builder.json(&policy_request_model);
368
369        let local_var_req = local_var_req_builder.build()?;
370        let local_var_resp = local_var_client.execute(local_var_req).await?;
371
372        let local_var_status = local_var_resp.status();
373        let local_var_content_type = local_var_resp
374            .headers()
375            .get("content-type")
376            .and_then(|v| v.to_str().ok())
377            .unwrap_or("application/octet-stream");
378        let local_var_content_type = super::ContentType::from(local_var_content_type);
379        let local_var_content = local_var_resp.text().await?;
380
381        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
382            match local_var_content_type {
383                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
384                ContentType::Text => {
385                    return Err(Error::from(serde_json::Error::custom(
386                        "Received `text/plain` content type response that cannot be converted to `models::PolicyResponseModel`",
387                    )));
388                }
389                ContentType::Unsupported(local_var_unknown_type) => {
390                    return Err(Error::from(serde_json::Error::custom(format!(
391                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PolicyResponseModel`"
392                    ))));
393                }
394            }
395        } else {
396            let local_var_entity: Option<PutError> = serde_json::from_str(&local_var_content).ok();
397            let local_var_error = ResponseContent {
398                status: local_var_status,
399                content: local_var_content,
400                entity: local_var_entity,
401            };
402            Err(Error::ResponseError(local_var_error))
403        }
404    }
405
406    async fn put_v_next<'a>(
407        &self,
408        org_id: uuid::Uuid,
409        r#type: &'a str,
410        save_policy_request: Option<models::SavePolicyRequest>,
411    ) -> Result<models::PolicyResponseModel, Error<PutVNextError>> {
412        let local_var_configuration = &self.configuration;
413
414        let local_var_client = &local_var_configuration.client;
415
416        let local_var_uri_str = format!("{}/organizations/{orgId}/policies/{type}/vnext", local_var_configuration.base_path, orgId=org_id, type=crate::apis::urlencode(r#type));
417        let mut local_var_req_builder =
418            local_var_client.request(reqwest::Method::PUT, local_var_uri_str.as_str());
419
420        if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
421            local_var_req_builder = local_var_req_builder
422                .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
423        }
424        if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
425            local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
426        };
427        local_var_req_builder = local_var_req_builder.json(&save_policy_request);
428
429        let local_var_req = local_var_req_builder.build()?;
430        let local_var_resp = local_var_client.execute(local_var_req).await?;
431
432        let local_var_status = local_var_resp.status();
433        let local_var_content_type = local_var_resp
434            .headers()
435            .get("content-type")
436            .and_then(|v| v.to_str().ok())
437            .unwrap_or("application/octet-stream");
438        let local_var_content_type = super::ContentType::from(local_var_content_type);
439        let local_var_content = local_var_resp.text().await?;
440
441        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
442            match local_var_content_type {
443                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
444                ContentType::Text => {
445                    return Err(Error::from(serde_json::Error::custom(
446                        "Received `text/plain` content type response that cannot be converted to `models::PolicyResponseModel`",
447                    )));
448                }
449                ContentType::Unsupported(local_var_unknown_type) => {
450                    return Err(Error::from(serde_json::Error::custom(format!(
451                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PolicyResponseModel`"
452                    ))));
453                }
454            }
455        } else {
456            let local_var_entity: Option<PutVNextError> =
457                serde_json::from_str(&local_var_content).ok();
458            let local_var_error = ResponseContent {
459                status: local_var_status,
460                content: local_var_content,
461                entity: local_var_entity,
462            };
463            Err(Error::ResponseError(local_var_error))
464        }
465    }
466}
467
468/// struct for typed errors of method [`PoliciesApi::get`]
469#[derive(Debug, Clone, Serialize, Deserialize)]
470#[serde(untagged)]
471pub enum GetError {
472    UnknownValue(serde_json::Value),
473}
474/// struct for typed errors of method [`PoliciesApi::get_all`]
475#[derive(Debug, Clone, Serialize, Deserialize)]
476#[serde(untagged)]
477pub enum GetAllError {
478    UnknownValue(serde_json::Value),
479}
480/// struct for typed errors of method [`PoliciesApi::get_by_token`]
481#[derive(Debug, Clone, Serialize, Deserialize)]
482#[serde(untagged)]
483pub enum GetByTokenError {
484    UnknownValue(serde_json::Value),
485}
486/// struct for typed errors of method [`PoliciesApi::get_master_password_policy`]
487#[derive(Debug, Clone, Serialize, Deserialize)]
488#[serde(untagged)]
489pub enum GetMasterPasswordPolicyError {
490    UnknownValue(serde_json::Value),
491}
492/// struct for typed errors of method [`PoliciesApi::put`]
493#[derive(Debug, Clone, Serialize, Deserialize)]
494#[serde(untagged)]
495pub enum PutError {
496    UnknownValue(serde_json::Value),
497}
498/// struct for typed errors of method [`PoliciesApi::put_v_next`]
499#[derive(Debug, Clone, Serialize, Deserialize)]
500#[serde(untagged)]
501pub enum PutVNextError {
502    UnknownValue(serde_json::Value),
503}