bitwarden_api_api/apis/
accounts_billing_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 AccountsBillingApi: Send + Sync {
29 async fn get_billing_history(
31 &self,
32 ) -> Result<models::BillingHistoryResponseModel, Error<GetBillingHistoryError>>;
33
34 async fn get_invoices<'a>(
36 &self,
37 status: Option<&'a str>,
38 start_after: Option<&'a str>,
39 ) -> Result<(), Error<GetInvoicesError>>;
40
41 async fn get_transactions<'a>(
43 &self,
44 start_after: Option<String>,
45 ) -> Result<(), Error<GetTransactionsError>>;
46}
47
48pub struct AccountsBillingApiClient {
49 configuration: Arc<configuration::Configuration>,
50}
51
52impl AccountsBillingApiClient {
53 pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
54 Self { configuration }
55 }
56}
57
58#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
59#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
60impl AccountsBillingApi for AccountsBillingApiClient {
61 async fn get_billing_history(
62 &self,
63 ) -> Result<models::BillingHistoryResponseModel, Error<GetBillingHistoryError>> {
64 let local_var_configuration = &self.configuration;
65
66 let local_var_client = &local_var_configuration.client;
67
68 let local_var_uri_str = format!(
69 "{}/accounts/billing/history",
70 local_var_configuration.base_path
71 );
72 let mut local_var_req_builder =
73 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
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 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
79
80 let local_var_req = local_var_req_builder.build()?;
81 let local_var_resp = local_var_client.execute(local_var_req).await?;
82
83 let local_var_status = local_var_resp.status();
84 let local_var_content_type = local_var_resp
85 .headers()
86 .get("content-type")
87 .and_then(|v| v.to_str().ok())
88 .unwrap_or("application/octet-stream");
89 let local_var_content_type = super::ContentType::from(local_var_content_type);
90 let local_var_content = local_var_resp.text().await?;
91
92 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
93 match local_var_content_type {
94 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
95 ContentType::Text => {
96 return Err(Error::from(serde_json::Error::custom(
97 "Received `text/plain` content type response that cannot be converted to `models::BillingHistoryResponseModel`",
98 )));
99 }
100 ContentType::Unsupported(local_var_unknown_type) => {
101 return Err(Error::from(serde_json::Error::custom(format!(
102 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::BillingHistoryResponseModel`"
103 ))));
104 }
105 }
106 } else {
107 let local_var_entity: Option<GetBillingHistoryError> =
108 serde_json::from_str(&local_var_content).ok();
109 let local_var_error = ResponseContent {
110 status: local_var_status,
111 content: local_var_content,
112 entity: local_var_entity,
113 };
114 Err(Error::ResponseError(local_var_error))
115 }
116 }
117
118 async fn get_invoices<'a>(
119 &self,
120 status: Option<&'a str>,
121 start_after: Option<&'a str>,
122 ) -> Result<(), Error<GetInvoicesError>> {
123 let local_var_configuration = &self.configuration;
124
125 let local_var_client = &local_var_configuration.client;
126
127 let local_var_uri_str = format!(
128 "{}/accounts/billing/invoices",
129 local_var_configuration.base_path
130 );
131 let mut local_var_req_builder =
132 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
133
134 if let Some(ref param_value) = status {
135 local_var_req_builder =
136 local_var_req_builder.query(&[("status", ¶m_value.to_string())]);
137 }
138 if let Some(ref param_value) = start_after {
139 local_var_req_builder =
140 local_var_req_builder.query(&[("startAfter", ¶m_value.to_string())]);
141 }
142 if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
143 local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
144 };
145 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
146
147 let local_var_req = local_var_req_builder.build()?;
148 let local_var_resp = local_var_client.execute(local_var_req).await?;
149
150 let local_var_status = local_var_resp.status();
151 let local_var_content = local_var_resp.text().await?;
152
153 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
154 Ok(())
155 } else {
156 let local_var_entity: Option<GetInvoicesError> =
157 serde_json::from_str(&local_var_content).ok();
158 let local_var_error = ResponseContent {
159 status: local_var_status,
160 content: local_var_content,
161 entity: local_var_entity,
162 };
163 Err(Error::ResponseError(local_var_error))
164 }
165 }
166
167 async fn get_transactions<'a>(
168 &self,
169 start_after: Option<String>,
170 ) -> Result<(), Error<GetTransactionsError>> {
171 let local_var_configuration = &self.configuration;
172
173 let local_var_client = &local_var_configuration.client;
174
175 let local_var_uri_str = format!(
176 "{}/accounts/billing/transactions",
177 local_var_configuration.base_path
178 );
179 let mut local_var_req_builder =
180 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
181
182 if let Some(ref param_value) = start_after {
183 local_var_req_builder =
184 local_var_req_builder.query(&[("startAfter", ¶m_value.to_string())]);
185 }
186 if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
187 local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
188 };
189 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
190
191 let local_var_req = local_var_req_builder.build()?;
192 let local_var_resp = local_var_client.execute(local_var_req).await?;
193
194 let local_var_status = local_var_resp.status();
195 let local_var_content = local_var_resp.text().await?;
196
197 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
198 Ok(())
199 } else {
200 let local_var_entity: Option<GetTransactionsError> =
201 serde_json::from_str(&local_var_content).ok();
202 let local_var_error = ResponseContent {
203 status: local_var_status,
204 content: local_var_content,
205 entity: local_var_entity,
206 };
207 Err(Error::ResponseError(local_var_error))
208 }
209 }
210}
211
212#[derive(Debug, Clone, Serialize, Deserialize)]
214#[serde(untagged)]
215pub enum GetBillingHistoryError {
216 UnknownValue(serde_json::Value),
217}
218#[derive(Debug, Clone, Serialize, Deserialize)]
220#[serde(untagged)]
221pub enum GetInvoicesError {
222 UnknownValue(serde_json::Value),
223}
224#[derive(Debug, Clone, Serialize, Deserialize)]
226#[serde(untagged)]
227pub enum GetTransactionsError {
228 UnknownValue(serde_json::Value),
229}