Skip to main content

bitwarden_api_api/apis/
self_hosted_organization_licenses_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 SelfHostedOrganizationLicensesApi: Send + Sync {
29    /// POST /organizations/licenses/self-hosted
30    async fn create_license<'a>(
31        &self,
32        key: &'a str,
33        keys_public_key: &'a str,
34        keys_encrypted_private_key: &'a str,
35        license: std::path::PathBuf,
36        collection_name: Option<&'a str>,
37    ) -> Result<models::OrganizationResponseModel, Error<CreateLicenseError>>;
38
39    /// POST /organizations/licenses/self-hosted/{id}/sync
40    async fn sync_license<'a>(&self, id: &'a str) -> Result<(), Error<SyncLicenseError>>;
41
42    /// POST /organizations/licenses/self-hosted/{id}
43    async fn update_license<'a>(
44        &self,
45        id: &'a str,
46        license: std::path::PathBuf,
47    ) -> Result<(), Error<UpdateLicenseError>>;
48}
49
50pub struct SelfHostedOrganizationLicensesApiClient {
51    configuration: Arc<configuration::Configuration>,
52}
53
54impl SelfHostedOrganizationLicensesApiClient {
55    pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
56        Self { configuration }
57    }
58}
59
60#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
61#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
62impl SelfHostedOrganizationLicensesApi for SelfHostedOrganizationLicensesApiClient {
63    async fn create_license<'a>(
64        &self,
65        key: &'a str,
66        keys_public_key: &'a str,
67        keys_encrypted_private_key: &'a str,
68        license: std::path::PathBuf,
69        collection_name: Option<&'a str>,
70    ) -> Result<models::OrganizationResponseModel, Error<CreateLicenseError>> {
71        let local_var_configuration = &self.configuration;
72
73        let local_var_client = &local_var_configuration.client;
74
75        let local_var_uri_str = format!(
76            "{}/organizations/licenses/self-hosted",
77            local_var_configuration.base_path
78        );
79        let mut local_var_req_builder =
80            local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
81
82        local_var_req_builder = local_var_req_builder.query(&[("key", &key.to_string())]);
83        if let Some(ref param_value) = collection_name {
84            local_var_req_builder =
85                local_var_req_builder.query(&[("collectionName", &param_value.to_string())]);
86        }
87        local_var_req_builder =
88            local_var_req_builder.query(&[("keys.publicKey", &keys_public_key.to_string())]);
89        local_var_req_builder = local_var_req_builder.query(&[(
90            "keys.encryptedPrivateKey",
91            &keys_encrypted_private_key.to_string(),
92        )]);
93        local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
94        let mut local_var_form = reqwest::multipart::Form::new();
95        // TODO: support file upload for 'license' parameter
96        local_var_req_builder = local_var_req_builder.multipart(local_var_form);
97
98        let local_var_resp = local_var_req_builder.send().await?;
99
100        let local_var_status = local_var_resp.status();
101        let local_var_content_type = local_var_resp
102            .headers()
103            .get("content-type")
104            .and_then(|v| v.to_str().ok())
105            .unwrap_or("application/octet-stream");
106        let local_var_content_type = super::ContentType::from(local_var_content_type);
107        let local_var_content = local_var_resp.text().await?;
108
109        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
110            match local_var_content_type {
111                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
112                ContentType::Text => {
113                    return Err(Error::from(serde_json::Error::custom(
114                        "Received `text/plain` content type response that cannot be converted to `models::OrganizationResponseModel`",
115                    )));
116                }
117                ContentType::Unsupported(local_var_unknown_type) => {
118                    return Err(Error::from(serde_json::Error::custom(format!(
119                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::OrganizationResponseModel`"
120                    ))));
121                }
122            }
123        } else {
124            let local_var_entity: Option<CreateLicenseError> =
125                serde_json::from_str(&local_var_content).ok();
126            let local_var_error = ResponseContent {
127                status: local_var_status,
128                content: local_var_content,
129                entity: local_var_entity,
130            };
131            Err(Error::ResponseError(local_var_error))
132        }
133    }
134
135    async fn sync_license<'a>(&self, id: &'a str) -> Result<(), Error<SyncLicenseError>> {
136        let local_var_configuration = &self.configuration;
137
138        let local_var_client = &local_var_configuration.client;
139
140        let local_var_uri_str = format!(
141            "{}/organizations/licenses/self-hosted/{id}/sync",
142            local_var_configuration.base_path,
143            id = crate::apis::urlencode(id)
144        );
145        let mut local_var_req_builder =
146            local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
147
148        local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
149
150        let local_var_resp = local_var_req_builder.send().await?;
151
152        let local_var_status = local_var_resp.status();
153        let local_var_content = local_var_resp.text().await?;
154
155        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
156            Ok(())
157        } else {
158            let local_var_entity: Option<SyncLicenseError> =
159                serde_json::from_str(&local_var_content).ok();
160            let local_var_error = ResponseContent {
161                status: local_var_status,
162                content: local_var_content,
163                entity: local_var_entity,
164            };
165            Err(Error::ResponseError(local_var_error))
166        }
167    }
168
169    async fn update_license<'a>(
170        &self,
171        id: &'a str,
172        license: std::path::PathBuf,
173    ) -> Result<(), Error<UpdateLicenseError>> {
174        let local_var_configuration = &self.configuration;
175
176        let local_var_client = &local_var_configuration.client;
177
178        let local_var_uri_str = format!(
179            "{}/organizations/licenses/self-hosted/{id}",
180            local_var_configuration.base_path,
181            id = crate::apis::urlencode(id)
182        );
183        let mut local_var_req_builder =
184            local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
185
186        local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
187        let mut local_var_form = reqwest::multipart::Form::new();
188        // TODO: support file upload for 'license' parameter
189        local_var_req_builder = local_var_req_builder.multipart(local_var_form);
190
191        let local_var_resp = local_var_req_builder.send().await?;
192
193        let local_var_status = local_var_resp.status();
194        let local_var_content = local_var_resp.text().await?;
195
196        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
197            Ok(())
198        } else {
199            let local_var_entity: Option<UpdateLicenseError> =
200                serde_json::from_str(&local_var_content).ok();
201            let local_var_error = ResponseContent {
202                status: local_var_status,
203                content: local_var_content,
204                entity: local_var_entity,
205            };
206            Err(Error::ResponseError(local_var_error))
207        }
208    }
209}
210
211/// struct for typed errors of method [`SelfHostedOrganizationLicensesApi::create_license`]
212#[derive(Debug, Clone, Serialize, Deserialize)]
213#[serde(untagged)]
214pub enum CreateLicenseError {
215    UnknownValue(serde_json::Value),
216}
217/// struct for typed errors of method [`SelfHostedOrganizationLicensesApi::sync_license`]
218#[derive(Debug, Clone, Serialize, Deserialize)]
219#[serde(untagged)]
220pub enum SyncLicenseError {
221    UnknownValue(serde_json::Value),
222}
223/// struct for typed errors of method [`SelfHostedOrganizationLicensesApi::update_license`]
224#[derive(Debug, Clone, Serialize, Deserialize)]
225#[serde(untagged)]
226pub enum UpdateLicenseError {
227    UnknownValue(serde_json::Value),
228}