Skip to main content

bitwarden_api_api/apis/
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 LicensesApi: Send + Sync {
29    /// GET /licenses/user/{id}
30    async fn get_user<'a>(
31        &self,
32        id: &'a str,
33        key: Option<&'a str>,
34    ) -> Result<models::UserLicense, Error<GetUserError>>;
35
36    /// GET /licenses/organization/{id}
37    async fn organization_sync<'a>(
38        &self,
39        id: &'a str,
40        self_hosted_organization_license_request_model: Option<
41            models::SelfHostedOrganizationLicenseRequestModel,
42        >,
43    ) -> Result<models::OrganizationLicense, Error<OrganizationSyncError>>;
44}
45
46pub struct LicensesApiClient {
47    configuration: Arc<configuration::Configuration>,
48}
49
50impl LicensesApiClient {
51    pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
52        Self { configuration }
53    }
54}
55
56#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
57#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
58impl LicensesApi for LicensesApiClient {
59    async fn get_user<'a>(
60        &self,
61        id: &'a str,
62        key: Option<&'a str>,
63    ) -> Result<models::UserLicense, Error<GetUserError>> {
64        let local_var_configuration = &self.configuration;
65
66        let local_var_client = &local_var_configuration.client;
67
68        let local_var_uri_str = format!(
69            "{}/licenses/user/{id}",
70            local_var_configuration.base_path,
71            id = crate::apis::urlencode(id)
72        );
73        let mut local_var_req_builder =
74            local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
75
76        if let Some(ref param_value) = key {
77            local_var_req_builder =
78                local_var_req_builder.query(&[("key", &param_value.to_string())]);
79        }
80        local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
81
82        let local_var_resp = local_var_req_builder.send().await?;
83
84        let local_var_status = local_var_resp.status();
85        let local_var_content_type = local_var_resp
86            .headers()
87            .get("content-type")
88            .and_then(|v| v.to_str().ok())
89            .unwrap_or("application/octet-stream");
90        let local_var_content_type = super::ContentType::from(local_var_content_type);
91        let local_var_content = local_var_resp.text().await?;
92
93        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
94            match local_var_content_type {
95                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
96                ContentType::Text => {
97                    return Err(Error::from(serde_json::Error::custom(
98                        "Received `text/plain` content type response that cannot be converted to `models::UserLicense`",
99                    )));
100                }
101                ContentType::Unsupported(local_var_unknown_type) => {
102                    return Err(Error::from(serde_json::Error::custom(format!(
103                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::UserLicense`"
104                    ))));
105                }
106            }
107        } else {
108            let local_var_entity: Option<GetUserError> =
109                serde_json::from_str(&local_var_content).ok();
110            let local_var_error = ResponseContent {
111                status: local_var_status,
112                content: local_var_content,
113                entity: local_var_entity,
114            };
115            Err(Error::ResponseError(local_var_error))
116        }
117    }
118
119    async fn organization_sync<'a>(
120        &self,
121        id: &'a str,
122        self_hosted_organization_license_request_model: Option<
123            models::SelfHostedOrganizationLicenseRequestModel,
124        >,
125    ) -> Result<models::OrganizationLicense, Error<OrganizationSyncError>> {
126        let local_var_configuration = &self.configuration;
127
128        let local_var_client = &local_var_configuration.client;
129
130        let local_var_uri_str = format!(
131            "{}/licenses/organization/{id}",
132            local_var_configuration.base_path,
133            id = crate::apis::urlencode(id)
134        );
135        let mut local_var_req_builder =
136            local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
137
138        local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
139        local_var_req_builder =
140            local_var_req_builder.json(&self_hosted_organization_license_request_model);
141
142        let local_var_resp = local_var_req_builder.send().await?;
143
144        let local_var_status = local_var_resp.status();
145        let local_var_content_type = local_var_resp
146            .headers()
147            .get("content-type")
148            .and_then(|v| v.to_str().ok())
149            .unwrap_or("application/octet-stream");
150        let local_var_content_type = super::ContentType::from(local_var_content_type);
151        let local_var_content = local_var_resp.text().await?;
152
153        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
154            match local_var_content_type {
155                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
156                ContentType::Text => {
157                    return Err(Error::from(serde_json::Error::custom(
158                        "Received `text/plain` content type response that cannot be converted to `models::OrganizationLicense`",
159                    )));
160                }
161                ContentType::Unsupported(local_var_unknown_type) => {
162                    return Err(Error::from(serde_json::Error::custom(format!(
163                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::OrganizationLicense`"
164                    ))));
165                }
166            }
167        } else {
168            let local_var_entity: Option<OrganizationSyncError> =
169                serde_json::from_str(&local_var_content).ok();
170            let local_var_error = ResponseContent {
171                status: local_var_status,
172                content: local_var_content,
173                entity: local_var_entity,
174            };
175            Err(Error::ResponseError(local_var_error))
176        }
177    }
178}
179
180/// struct for typed errors of method [`LicensesApi::get_user`]
181#[derive(Debug, Clone, Serialize, Deserialize)]
182#[serde(untagged)]
183pub enum GetUserError {
184    UnknownValue(serde_json::Value),
185}
186/// struct for typed errors of method [`LicensesApi::organization_sync`]
187#[derive(Debug, Clone, Serialize, Deserialize)]
188#[serde(untagged)]
189pub enum OrganizationSyncError {
190    UnknownValue(serde_json::Value),
191}