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::{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        if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
81            local_var_req_builder = local_var_req_builder
82                .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
83        }
84        if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
85            local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
86        };
87
88        let local_var_req = local_var_req_builder.build()?;
89        let local_var_resp = local_var_client.execute(local_var_req).await?;
90
91        let local_var_status = local_var_resp.status();
92        let local_var_content_type = local_var_resp
93            .headers()
94            .get("content-type")
95            .and_then(|v| v.to_str().ok())
96            .unwrap_or("application/octet-stream");
97        let local_var_content_type = super::ContentType::from(local_var_content_type);
98        let local_var_content = local_var_resp.text().await?;
99
100        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
101            match local_var_content_type {
102                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
103                ContentType::Text => {
104                    return Err(Error::from(serde_json::Error::custom(
105                        "Received `text/plain` content type response that cannot be converted to `models::UserLicense`",
106                    )));
107                }
108                ContentType::Unsupported(local_var_unknown_type) => {
109                    return Err(Error::from(serde_json::Error::custom(format!(
110                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::UserLicense`"
111                    ))));
112                }
113            }
114        } else {
115            let local_var_entity: Option<GetUserError> =
116                serde_json::from_str(&local_var_content).ok();
117            let local_var_error = ResponseContent {
118                status: local_var_status,
119                content: local_var_content,
120                entity: local_var_entity,
121            };
122            Err(Error::ResponseError(local_var_error))
123        }
124    }
125
126    async fn organization_sync<'a>(
127        &self,
128        id: &'a str,
129        self_hosted_organization_license_request_model: Option<
130            models::SelfHostedOrganizationLicenseRequestModel,
131        >,
132    ) -> Result<models::OrganizationLicense, Error<OrganizationSyncError>> {
133        let local_var_configuration = &self.configuration;
134
135        let local_var_client = &local_var_configuration.client;
136
137        let local_var_uri_str = format!(
138            "{}/licenses/organization/{id}",
139            local_var_configuration.base_path,
140            id = crate::apis::urlencode(id)
141        );
142        let mut local_var_req_builder =
143            local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
144
145        if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
146            local_var_req_builder = local_var_req_builder
147                .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
148        }
149        if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
150            local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
151        };
152        local_var_req_builder =
153            local_var_req_builder.json(&self_hosted_organization_license_request_model);
154
155        let local_var_req = local_var_req_builder.build()?;
156        let local_var_resp = local_var_client.execute(local_var_req).await?;
157
158        let local_var_status = local_var_resp.status();
159        let local_var_content_type = local_var_resp
160            .headers()
161            .get("content-type")
162            .and_then(|v| v.to_str().ok())
163            .unwrap_or("application/octet-stream");
164        let local_var_content_type = super::ContentType::from(local_var_content_type);
165        let local_var_content = local_var_resp.text().await?;
166
167        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
168            match local_var_content_type {
169                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
170                ContentType::Text => {
171                    return Err(Error::from(serde_json::Error::custom(
172                        "Received `text/plain` content type response that cannot be converted to `models::OrganizationLicense`",
173                    )));
174                }
175                ContentType::Unsupported(local_var_unknown_type) => {
176                    return Err(Error::from(serde_json::Error::custom(format!(
177                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::OrganizationLicense`"
178                    ))));
179                }
180            }
181        } else {
182            let local_var_entity: Option<OrganizationSyncError> =
183                serde_json::from_str(&local_var_content).ok();
184            let local_var_error = ResponseContent {
185                status: local_var_status,
186                content: local_var_content,
187                entity: local_var_entity,
188            };
189            Err(Error::ResponseError(local_var_error))
190        }
191    }
192}
193
194/// struct for typed errors of method [`LicensesApi::get_user`]
195#[derive(Debug, Clone, Serialize, Deserialize)]
196#[serde(untagged)]
197pub enum GetUserError {
198    UnknownValue(serde_json::Value),
199}
200/// struct for typed errors of method [`LicensesApi::organization_sync`]
201#[derive(Debug, Clone, Serialize, Deserialize)]
202#[serde(untagged)]
203pub enum OrganizationSyncError {
204    UnknownValue(serde_json::Value),
205}