bitwarden_api_api/apis/
settings_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 SettingsApi: Send + Sync {
29 async fn get_domains<'a>(
31 &self,
32 excluded: Option<bool>,
33 ) -> Result<models::DomainsResponseModel, Error<GetDomainsError>>;
34
35 async fn put_domains<'a>(
37 &self,
38 update_domains_request_model: Option<models::UpdateDomainsRequestModel>,
39 ) -> Result<models::DomainsResponseModel, Error<PutDomainsError>>;
40}
41
42pub struct SettingsApiClient {
43 configuration: Arc<configuration::Configuration>,
44}
45
46impl SettingsApiClient {
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 SettingsApi for SettingsApiClient {
55 async fn get_domains<'a>(
56 &self,
57 excluded: Option<bool>,
58 ) -> Result<models::DomainsResponseModel, Error<GetDomainsError>> {
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!("{}/settings/domains", local_var_configuration.base_path);
64 let mut local_var_req_builder =
65 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
66
67 if let Some(ref param_value) = excluded {
68 local_var_req_builder =
69 local_var_req_builder.query(&[("excluded", ¶m_value.to_string())]);
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::DomainsResponseModel`",
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::DomainsResponseModel`"
102 ))));
103 }
104 }
105 } else {
106 let local_var_entity: Option<GetDomainsError> =
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 put_domains<'a>(
118 &self,
119 update_domains_request_model: Option<models::UpdateDomainsRequestModel>,
120 ) -> Result<models::DomainsResponseModel, Error<PutDomainsError>> {
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!("{}/settings/domains", local_var_configuration.base_path);
126 let mut local_var_req_builder =
127 local_var_client.request(reqwest::Method::PUT, local_var_uri_str.as_str());
128
129 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
130 local_var_req_builder = local_var_req_builder
131 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
132 }
133 if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
134 local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
135 };
136 local_var_req_builder = local_var_req_builder.json(&update_domains_request_model);
137
138 let local_var_req = local_var_req_builder.build()?;
139 let local_var_resp = local_var_client.execute(local_var_req).await?;
140
141 let local_var_status = local_var_resp.status();
142 let local_var_content_type = local_var_resp
143 .headers()
144 .get("content-type")
145 .and_then(|v| v.to_str().ok())
146 .unwrap_or("application/octet-stream");
147 let local_var_content_type = super::ContentType::from(local_var_content_type);
148 let local_var_content = local_var_resp.text().await?;
149
150 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
151 match local_var_content_type {
152 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
153 ContentType::Text => {
154 return Err(Error::from(serde_json::Error::custom(
155 "Received `text/plain` content type response that cannot be converted to `models::DomainsResponseModel`",
156 )));
157 }
158 ContentType::Unsupported(local_var_unknown_type) => {
159 return Err(Error::from(serde_json::Error::custom(format!(
160 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::DomainsResponseModel`"
161 ))));
162 }
163 }
164 } else {
165 let local_var_entity: Option<PutDomainsError> =
166 serde_json::from_str(&local_var_content).ok();
167 let local_var_error = ResponseContent {
168 status: local_var_status,
169 content: local_var_content,
170 entity: local_var_entity,
171 };
172 Err(Error::ResponseError(local_var_error))
173 }
174 }
175}
176
177#[derive(Debug, Clone, Serialize, Deserialize)]
179#[serde(untagged)]
180pub enum GetDomainsError {
181 UnknownValue(serde_json::Value),
182}
183#[derive(Debug, Clone, Serialize, Deserialize)]
185#[serde(untagged)]
186pub enum PutDomainsError {
187 UnknownValue(serde_json::Value),
188}