bitwarden_api_api/apis/
config_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 ConfigApi: Send + Sync {
29 async fn get_configs(&self) -> Result<models::ConfigResponseModel, Error<GetConfigsError>>;
31}
32
33pub struct ConfigApiClient {
34 configuration: Arc<configuration::Configuration>,
35}
36
37impl ConfigApiClient {
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 ConfigApi for ConfigApiClient {
46 async fn get_configs(&self) -> Result<models::ConfigResponseModel, Error<GetConfigsError>> {
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!("{}/config", local_var_configuration.base_path);
52 let mut local_var_req_builder =
53 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
54
55 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
56
57 let local_var_resp = local_var_req_builder.send().await?;
58
59 let local_var_status = local_var_resp.status();
60 let local_var_content_type = local_var_resp
61 .headers()
62 .get("content-type")
63 .and_then(|v| v.to_str().ok())
64 .unwrap_or("application/octet-stream");
65 let local_var_content_type = super::ContentType::from(local_var_content_type);
66 let local_var_content = local_var_resp.text().await?;
67
68 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
69 match local_var_content_type {
70 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
71 ContentType::Text => {
72 return Err(Error::from(serde_json::Error::custom(
73 "Received `text/plain` content type response that cannot be converted to `models::ConfigResponseModel`",
74 )));
75 }
76 ContentType::Unsupported(local_var_unknown_type) => {
77 return Err(Error::from(serde_json::Error::custom(format!(
78 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::ConfigResponseModel`"
79 ))));
80 }
81 }
82 } else {
83 let local_var_entity: Option<GetConfigsError> =
84 serde_json::from_str(&local_var_content).ok();
85 let local_var_error = ResponseContent {
86 status: local_var_status,
87 content: local_var_content,
88 entity: local_var_entity,
89 };
90 Err(Error::ResponseError(local_var_error))
91 }
92 }
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize)]
97#[serde(untagged)]
98pub enum GetConfigsError {
99 UnknownValue(serde_json::Value),
100}