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