bitwarden_api_api/apis/
installations_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 InstallationsApi: Send + Sync {
29 async fn get<'a>(
31 &self,
32 id: uuid::Uuid,
33 ) -> Result<models::InstallationResponseModel, Error<GetError>>;
34
35 async fn post<'a>(
37 &self,
38 installation_request_model: Option<models::InstallationRequestModel>,
39 ) -> Result<models::InstallationResponseModel, Error<PostError>>;
40}
41
42pub struct InstallationsApiClient {
43 configuration: Arc<configuration::Configuration>,
44}
45
46impl InstallationsApiClient {
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 InstallationsApi for InstallationsApiClient {
55 async fn get<'a>(
56 &self,
57 id: uuid::Uuid,
58 ) -> Result<models::InstallationResponseModel, Error<GetError>> {
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 "{}/installations/{id}",
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::InstallationResponseModel`",
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::InstallationResponseModel`"
99 ))));
100 }
101 }
102 } else {
103 let local_var_entity: Option<GetError> = serde_json::from_str(&local_var_content).ok();
104 let local_var_error = ResponseContent {
105 status: local_var_status,
106 content: local_var_content,
107 entity: local_var_entity,
108 };
109 Err(Error::ResponseError(local_var_error))
110 }
111 }
112
113 async fn post<'a>(
114 &self,
115 installation_request_model: Option<models::InstallationRequestModel>,
116 ) -> Result<models::InstallationResponseModel, Error<PostError>> {
117 let local_var_configuration = &self.configuration;
118
119 let local_var_client = &local_var_configuration.client;
120
121 let local_var_uri_str = format!("{}/installations", local_var_configuration.base_path);
122 let mut local_var_req_builder =
123 local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
124
125 if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
126 local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
127 };
128 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
129 local_var_req_builder = local_var_req_builder.json(&installation_request_model);
130
131 let local_var_req = local_var_req_builder.build()?;
132 let local_var_resp = local_var_client.execute(local_var_req).await?;
133
134 let local_var_status = local_var_resp.status();
135 let local_var_content_type = local_var_resp
136 .headers()
137 .get("content-type")
138 .and_then(|v| v.to_str().ok())
139 .unwrap_or("application/octet-stream");
140 let local_var_content_type = super::ContentType::from(local_var_content_type);
141 let local_var_content = local_var_resp.text().await?;
142
143 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
144 match local_var_content_type {
145 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
146 ContentType::Text => {
147 return Err(Error::from(serde_json::Error::custom(
148 "Received `text/plain` content type response that cannot be converted to `models::InstallationResponseModel`",
149 )));
150 }
151 ContentType::Unsupported(local_var_unknown_type) => {
152 return Err(Error::from(serde_json::Error::custom(format!(
153 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::InstallationResponseModel`"
154 ))));
155 }
156 }
157 } else {
158 let local_var_entity: Option<PostError> = serde_json::from_str(&local_var_content).ok();
159 let local_var_error = ResponseContent {
160 status: local_var_status,
161 content: local_var_content,
162 entity: local_var_entity,
163 };
164 Err(Error::ResponseError(local_var_error))
165 }
166 }
167}
168
169#[derive(Debug, Clone, Serialize, Deserialize)]
171#[serde(untagged)]
172pub enum GetError {
173 UnknownValue(serde_json::Value),
174}
175#[derive(Debug, Clone, Serialize, Deserialize)]
177#[serde(untagged)]
178pub enum PostError {
179 UnknownValue(serde_json::Value),
180}