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