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