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 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
76
77 let local_var_resp = local_var_req_builder.send().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::BillingHistoryResponseModel`",
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::BillingHistoryResponseModel`"
99 ))));
100 }
101 }
102 } else {
103 let local_var_entity: Option<GetBillingHistoryError> =
104 serde_json::from_str(&local_var_content).ok();
105 let local_var_error = ResponseContent {
106 status: local_var_status,
107 content: local_var_content,
108 entity: local_var_entity,
109 };
110 Err(Error::ResponseError(local_var_error))
111 }
112 }
113
114 async fn get_invoices<'a>(
115 &self,
116 status: Option<&'a str>,
117 start_after: Option<&'a str>,
118 ) -> Result<(), Error<GetInvoicesError>> {
119 let local_var_configuration = &self.configuration;
120
121 let local_var_client = &local_var_configuration.client;
122
123 let local_var_uri_str = format!(
124 "{}/accounts/billing/invoices",
125 local_var_configuration.base_path
126 );
127 let mut local_var_req_builder =
128 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
129
130 if let Some(ref param_value) = status {
131 local_var_req_builder =
132 local_var_req_builder.query(&[("status", ¶m_value.to_string())]);
133 }
134 if let Some(ref param_value) = start_after {
135 local_var_req_builder =
136 local_var_req_builder.query(&[("startAfter", ¶m_value.to_string())]);
137 }
138 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
139
140 let local_var_resp = local_var_req_builder.send().await?;
141
142 let local_var_status = local_var_resp.status();
143 let local_var_content = local_var_resp.text().await?;
144
145 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
146 Ok(())
147 } else {
148 let local_var_entity: Option<GetInvoicesError> =
149 serde_json::from_str(&local_var_content).ok();
150 let local_var_error = ResponseContent {
151 status: local_var_status,
152 content: local_var_content,
153 entity: local_var_entity,
154 };
155 Err(Error::ResponseError(local_var_error))
156 }
157 }
158
159 async fn get_transactions<'a>(
160 &self,
161 start_after: Option<String>,
162 ) -> Result<(), Error<GetTransactionsError>> {
163 let local_var_configuration = &self.configuration;
164
165 let local_var_client = &local_var_configuration.client;
166
167 let local_var_uri_str = format!(
168 "{}/accounts/billing/transactions",
169 local_var_configuration.base_path
170 );
171 let mut local_var_req_builder =
172 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
173
174 if let Some(ref param_value) = start_after {
175 local_var_req_builder =
176 local_var_req_builder.query(&[("startAfter", ¶m_value.to_string())]);
177 }
178 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
179
180 let local_var_resp = local_var_req_builder.send().await?;
181
182 let local_var_status = local_var_resp.status();
183 let local_var_content = local_var_resp.text().await?;
184
185 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
186 Ok(())
187 } else {
188 let local_var_entity: Option<GetTransactionsError> =
189 serde_json::from_str(&local_var_content).ok();
190 let local_var_error = ResponseContent {
191 status: local_var_status,
192 content: local_var_content,
193 entity: local_var_entity,
194 };
195 Err(Error::ResponseError(local_var_error))
196 }
197 }
198}
199
200#[derive(Debug, Clone, Serialize, Deserialize)]
202#[serde(untagged)]
203pub enum GetBillingHistoryError {
204 UnknownValue(serde_json::Value),
205}
206#[derive(Debug, Clone, Serialize, Deserialize)]
208#[serde(untagged)]
209pub enum GetInvoicesError {
210 UnknownValue(serde_json::Value),
211}
212#[derive(Debug, Clone, Serialize, Deserialize)]
214#[serde(untagged)]
215pub enum GetTransactionsError {
216 UnknownValue(serde_json::Value),
217}