bitwarden_api_api/apis/
users_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::{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 UsersApi: Send + Sync {
29 async fn get_account_keys<'a>(
31 &self,
32 id: uuid::Uuid,
33 ) -> Result<models::PublicKeysResponseModel, Error<GetAccountKeysError>>;
34
35 async fn get_public_key<'a>(
37 &self,
38 id: uuid::Uuid,
39 ) -> Result<models::UserKeyResponseModel, Error<GetPublicKeyError>>;
40}
41
42pub struct UsersApiClient {
43 configuration: Arc<configuration::Configuration>,
44}
45
46impl UsersApiClient {
47 pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
48 Self { configuration }
49 }
50}
51
52#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
53#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
54impl UsersApi for UsersApiClient {
55 async fn get_account_keys<'a>(
56 &self,
57 id: uuid::Uuid,
58 ) -> Result<models::PublicKeysResponseModel, Error<GetAccountKeysError>> {
59 let local_var_configuration = &self.configuration;
60
61 let local_var_client = &local_var_configuration.client;
62
63 let local_var_uri_str = format!(
64 "{}/users/{id}/keys",
65 local_var_configuration.base_path,
66 id = id
67 );
68 let mut local_var_req_builder =
69 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
70
71 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
72 local_var_req_builder = local_var_req_builder
73 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
74 }
75 if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
76 local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
77 };
78
79 let local_var_req = local_var_req_builder.build()?;
80 let local_var_resp = local_var_client.execute(local_var_req).await?;
81
82 let local_var_status = local_var_resp.status();
83 let local_var_content_type = local_var_resp
84 .headers()
85 .get("content-type")
86 .and_then(|v| v.to_str().ok())
87 .unwrap_or("application/octet-stream");
88 let local_var_content_type = super::ContentType::from(local_var_content_type);
89 let local_var_content = local_var_resp.text().await?;
90
91 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
92 match local_var_content_type {
93 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
94 ContentType::Text => {
95 return Err(Error::from(serde_json::Error::custom(
96 "Received `text/plain` content type response that cannot be converted to `models::PublicKeysResponseModel`",
97 )));
98 }
99 ContentType::Unsupported(local_var_unknown_type) => {
100 return Err(Error::from(serde_json::Error::custom(format!(
101 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PublicKeysResponseModel`"
102 ))));
103 }
104 }
105 } else {
106 let local_var_entity: Option<GetAccountKeysError> =
107 serde_json::from_str(&local_var_content).ok();
108 let local_var_error = ResponseContent {
109 status: local_var_status,
110 content: local_var_content,
111 entity: local_var_entity,
112 };
113 Err(Error::ResponseError(local_var_error))
114 }
115 }
116
117 async fn get_public_key<'a>(
118 &self,
119 id: uuid::Uuid,
120 ) -> Result<models::UserKeyResponseModel, Error<GetPublicKeyError>> {
121 let local_var_configuration = &self.configuration;
122
123 let local_var_client = &local_var_configuration.client;
124
125 let local_var_uri_str = format!(
126 "{}/users/{id}/public-key",
127 local_var_configuration.base_path,
128 id = id
129 );
130 let mut local_var_req_builder =
131 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
132
133 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
134 local_var_req_builder = local_var_req_builder
135 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
136 }
137 if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
138 local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
139 };
140
141 let local_var_req = local_var_req_builder.build()?;
142 let local_var_resp = local_var_client.execute(local_var_req).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::UserKeyResponseModel`",
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::UserKeyResponseModel`"
164 ))));
165 }
166 }
167 } else {
168 let local_var_entity: Option<GetPublicKeyError> =
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#[derive(Debug, Clone, Serialize, Deserialize)]
182#[serde(untagged)]
183pub enum GetAccountKeysError {
184 UnknownValue(serde_json::Value),
185}
186#[derive(Debug, Clone, Serialize, Deserialize)]
188#[serde(untagged)]
189pub enum GetPublicKeyError {
190 UnknownValue(serde_json::Value),
191}