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::{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}/public-key
30    async fn get<'a>(&self, id: &'a str) -> Result<models::UserKeyResponseModel, Error<GetError>>;
31}
32
33pub struct UsersApiClient {
34    configuration: Arc<configuration::Configuration>,
35}
36
37impl UsersApiClient {
38    pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
39        Self { configuration }
40    }
41}
42
43#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
44#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
45impl UsersApi for UsersApiClient {
46    async fn get<'a>(&self, id: &'a str) -> Result<models::UserKeyResponseModel, Error<GetError>> {
47        let local_var_configuration = &self.configuration;
48
49        let local_var_client = &local_var_configuration.client;
50
51        let local_var_uri_str = format!(
52            "{}/users/{id}/public-key",
53            local_var_configuration.base_path,
54            id = crate::apis::urlencode(id)
55        );
56        let mut local_var_req_builder =
57            local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
58
59        if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
60            local_var_req_builder = local_var_req_builder
61                .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
62        }
63        if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
64            local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
65        };
66
67        let local_var_req = local_var_req_builder.build()?;
68        let local_var_resp = local_var_client.execute(local_var_req).await?;
69
70        let local_var_status = local_var_resp.status();
71        let local_var_content_type = local_var_resp
72            .headers()
73            .get("content-type")
74            .and_then(|v| v.to_str().ok())
75            .unwrap_or("application/octet-stream");
76        let local_var_content_type = super::ContentType::from(local_var_content_type);
77        let local_var_content = local_var_resp.text().await?;
78
79        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
80            match local_var_content_type {
81                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
82                ContentType::Text => {
83                    return Err(Error::from(serde_json::Error::custom(
84                        "Received `text/plain` content type response that cannot be converted to `models::UserKeyResponseModel`",
85                    )));
86                }
87                ContentType::Unsupported(local_var_unknown_type) => {
88                    return Err(Error::from(serde_json::Error::custom(format!(
89                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::UserKeyResponseModel`"
90                    ))));
91                }
92            }
93        } else {
94            let local_var_entity: Option<GetError> = serde_json::from_str(&local_var_content).ok();
95            let local_var_error = ResponseContent {
96                status: local_var_status,
97                content: local_var_content,
98                entity: local_var_entity,
99            };
100            Err(Error::ResponseError(local_var_error))
101        }
102    }
103}
104
105/// struct for typed errors of method [`UsersApi::get`]
106#[derive(Debug, Clone, Serialize, Deserialize)]
107#[serde(untagged)]
108pub enum GetError {
109    UnknownValue(serde_json::Value),
110}