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::{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        if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
94            local_var_req_builder = local_var_req_builder
95                .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
96        }
97        if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
98            local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
99        };
100        let mut local_var_form = reqwest::multipart::Form::new();
101        // TODO: support file upload for 'license' parameter
102        local_var_req_builder = local_var_req_builder.multipart(local_var_form);
103
104        let local_var_req = local_var_req_builder.build()?;
105        let local_var_resp = local_var_client.execute(local_var_req).await?;
106
107        let local_var_status = local_var_resp.status();
108        let local_var_content_type = local_var_resp
109            .headers()
110            .get("content-type")
111            .and_then(|v| v.to_str().ok())
112            .unwrap_or("application/octet-stream");
113        let local_var_content_type = super::ContentType::from(local_var_content_type);
114        let local_var_content = local_var_resp.text().await?;
115
116        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
117            match local_var_content_type {
118                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
119                ContentType::Text => {
120                    return Err(Error::from(serde_json::Error::custom(
121                        "Received `text/plain` content type response that cannot be converted to `models::OrganizationResponseModel`",
122                    )));
123                }
124                ContentType::Unsupported(local_var_unknown_type) => {
125                    return Err(Error::from(serde_json::Error::custom(format!(
126                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::OrganizationResponseModel`"
127                    ))));
128                }
129            }
130        } else {
131            let local_var_entity: Option<CreateLicenseError> =
132                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 sync_license<'a>(&self, id: &'a str) -> Result<(), Error<SyncLicenseError>> {
143        let local_var_configuration = &self.configuration;
144
145        let local_var_client = &local_var_configuration.client;
146
147        let local_var_uri_str = format!(
148            "{}/organizations/licenses/self-hosted/{id}/sync",
149            local_var_configuration.base_path,
150            id = crate::apis::urlencode(id)
151        );
152        let mut local_var_req_builder =
153            local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
154
155        if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
156            local_var_req_builder = local_var_req_builder
157                .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
158        }
159        if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
160            local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
161        };
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 = local_var_resp.text().await?;
168
169        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
170            Ok(())
171        } else {
172            let local_var_entity: Option<SyncLicenseError> =
173                serde_json::from_str(&local_var_content).ok();
174            let local_var_error = ResponseContent {
175                status: local_var_status,
176                content: local_var_content,
177                entity: local_var_entity,
178            };
179            Err(Error::ResponseError(local_var_error))
180        }
181    }
182
183    async fn update_license<'a>(
184        &self,
185        id: &'a str,
186        license: std::path::PathBuf,
187    ) -> Result<(), Error<UpdateLicenseError>> {
188        let local_var_configuration = &self.configuration;
189
190        let local_var_client = &local_var_configuration.client;
191
192        let local_var_uri_str = format!(
193            "{}/organizations/licenses/self-hosted/{id}",
194            local_var_configuration.base_path,
195            id = crate::apis::urlencode(id)
196        );
197        let mut local_var_req_builder =
198            local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
199
200        if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
201            local_var_req_builder = local_var_req_builder
202                .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
203        }
204        if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
205            local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
206        };
207        let mut local_var_form = reqwest::multipart::Form::new();
208        // TODO: support file upload for 'license' parameter
209        local_var_req_builder = local_var_req_builder.multipart(local_var_form);
210
211        let local_var_req = local_var_req_builder.build()?;
212        let local_var_resp = local_var_client.execute(local_var_req).await?;
213
214        let local_var_status = local_var_resp.status();
215        let local_var_content = local_var_resp.text().await?;
216
217        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
218            Ok(())
219        } else {
220            let local_var_entity: Option<UpdateLicenseError> =
221                serde_json::from_str(&local_var_content).ok();
222            let local_var_error = ResponseContent {
223                status: local_var_status,
224                content: local_var_content,
225                entity: local_var_entity,
226            };
227            Err(Error::ResponseError(local_var_error))
228        }
229    }
230}
231
232/// struct for typed errors of method [`SelfHostedOrganizationLicensesApi::create_license`]
233#[derive(Debug, Clone, Serialize, Deserialize)]
234#[serde(untagged)]
235pub enum CreateLicenseError {
236    UnknownValue(serde_json::Value),
237}
238/// struct for typed errors of method [`SelfHostedOrganizationLicensesApi::sync_license`]
239#[derive(Debug, Clone, Serialize, Deserialize)]
240#[serde(untagged)]
241pub enum SyncLicenseError {
242    UnknownValue(serde_json::Value),
243}
244/// struct for typed errors of method [`SelfHostedOrganizationLicensesApi::update_license`]
245#[derive(Debug, Clone, Serialize, Deserialize)]
246#[serde(untagged)]
247pub enum UpdateLicenseError {
248    UnknownValue(serde_json::Value),
249}