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::{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 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 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
72
73 let local_var_resp = local_var_req_builder.send().await?;
74
75 let local_var_status = local_var_resp.status();
76 let local_var_content_type = local_var_resp
77 .headers()
78 .get("content-type")
79 .and_then(|v| v.to_str().ok())
80 .unwrap_or("application/octet-stream");
81 let local_var_content_type = super::ContentType::from(local_var_content_type);
82 let local_var_content = local_var_resp.text().await?;
83
84 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
85 match local_var_content_type {
86 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
87 ContentType::Text => {
88 return Err(Error::from(serde_json::Error::custom(
89 "Received `text/plain` content type response that cannot be converted to `models::PublicKeysResponseModel`",
90 )));
91 }
92 ContentType::Unsupported(local_var_unknown_type) => {
93 return Err(Error::from(serde_json::Error::custom(format!(
94 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PublicKeysResponseModel`"
95 ))));
96 }
97 }
98 } else {
99 let local_var_entity: Option<GetAccountKeysError> =
100 serde_json::from_str(&local_var_content).ok();
101 let local_var_error = ResponseContent {
102 status: local_var_status,
103 content: local_var_content,
104 entity: local_var_entity,
105 };
106 Err(Error::ResponseError(local_var_error))
107 }
108 }
109
110 async fn get_public_key<'a>(
111 &self,
112 id: uuid::Uuid,
113 ) -> Result<models::UserKeyResponseModel, Error<GetPublicKeyError>> {
114 let local_var_configuration = &self.configuration;
115
116 let local_var_client = &local_var_configuration.client;
117
118 let local_var_uri_str = format!(
119 "{}/users/{id}/public-key",
120 local_var_configuration.base_path,
121 id = id
122 );
123 let mut local_var_req_builder =
124 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
125
126 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
127
128 let local_var_resp = local_var_req_builder.send().await?;
129
130 let local_var_status = local_var_resp.status();
131 let local_var_content_type = local_var_resp
132 .headers()
133 .get("content-type")
134 .and_then(|v| v.to_str().ok())
135 .unwrap_or("application/octet-stream");
136 let local_var_content_type = super::ContentType::from(local_var_content_type);
137 let local_var_content = local_var_resp.text().await?;
138
139 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
140 match local_var_content_type {
141 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
142 ContentType::Text => {
143 return Err(Error::from(serde_json::Error::custom(
144 "Received `text/plain` content type response that cannot be converted to `models::UserKeyResponseModel`",
145 )));
146 }
147 ContentType::Unsupported(local_var_unknown_type) => {
148 return Err(Error::from(serde_json::Error::custom(format!(
149 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::UserKeyResponseModel`"
150 ))));
151 }
152 }
153 } else {
154 let local_var_entity: Option<GetPublicKeyError> =
155 serde_json::from_str(&local_var_content).ok();
156 let local_var_error = ResponseContent {
157 status: local_var_status,
158 content: local_var_content,
159 entity: local_var_entity,
160 };
161 Err(Error::ResponseError(local_var_error))
162 }
163 }
164}
165
166#[derive(Debug, Clone, Serialize, Deserialize)]
168#[serde(untagged)]
169pub enum GetAccountKeysError {
170 UnknownValue(serde_json::Value),
171}
172#[derive(Debug, Clone, Serialize, Deserialize)]
174#[serde(untagged)]
175pub enum GetPublicKeyError {
176 UnknownValue(serde_json::Value),
177}