bitwarden_api_api/apis/
misc_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 MiscApi: Send + Sync {
29 async fn post_bit_pay_invoice<'a>(
31 &self,
32 bit_pay_invoice_request_model: Option<models::BitPayInvoiceRequestModel>,
33 ) -> Result<String, Error<PostBitPayInvoiceError>>;
34
35 async fn post_setup_payment(&self) -> Result<String, Error<PostSetupPaymentError>>;
37}
38
39pub struct MiscApiClient {
40 configuration: Arc<configuration::Configuration>,
41}
42
43impl MiscApiClient {
44 pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
45 Self { configuration }
46 }
47}
48
49#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
50#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
51impl MiscApi for MiscApiClient {
52 async fn post_bit_pay_invoice<'a>(
53 &self,
54 bit_pay_invoice_request_model: Option<models::BitPayInvoiceRequestModel>,
55 ) -> Result<String, Error<PostBitPayInvoiceError>> {
56 let local_var_configuration = &self.configuration;
57
58 let local_var_client = &local_var_configuration.client;
59
60 let local_var_uri_str = format!("{}/bitpay-invoice", local_var_configuration.base_path);
61 let mut local_var_req_builder =
62 local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
63
64 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
65 local_var_req_builder = local_var_req_builder
66 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
67 }
68 if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
69 local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
70 };
71 local_var_req_builder = local_var_req_builder.json(&bit_pay_invoice_request_model);
72
73 let local_var_req = local_var_req_builder.build()?;
74 let local_var_resp = local_var_client.execute(local_var_req).await?;
75
76 let local_var_status = local_var_resp.status();
77 let local_var_content_type = local_var_resp
78 .headers()
79 .get("content-type")
80 .and_then(|v| v.to_str().ok())
81 .unwrap_or("application/octet-stream");
82 let local_var_content_type = super::ContentType::from(local_var_content_type);
83 let local_var_content = local_var_resp.text().await?;
84
85 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
86 match local_var_content_type {
87 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
88 ContentType::Text => return Ok(local_var_content),
89 ContentType::Unsupported(local_var_unknown_type) => {
90 return Err(Error::from(serde_json::Error::custom(format!(
91 "Received `{local_var_unknown_type}` content type response that cannot be converted to `String`"
92 ))));
93 }
94 }
95 } else {
96 let local_var_entity: Option<PostBitPayInvoiceError> =
97 serde_json::from_str(&local_var_content).ok();
98 let local_var_error = ResponseContent {
99 status: local_var_status,
100 content: local_var_content,
101 entity: local_var_entity,
102 };
103 Err(Error::ResponseError(local_var_error))
104 }
105 }
106
107 async fn post_setup_payment(&self) -> Result<String, Error<PostSetupPaymentError>> {
108 let local_var_configuration = &self.configuration;
109
110 let local_var_client = &local_var_configuration.client;
111
112 let local_var_uri_str = format!("{}/setup-payment", local_var_configuration.base_path);
113 let mut local_var_req_builder =
114 local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
115
116 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
117 local_var_req_builder = local_var_req_builder
118 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
119 }
120 if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
121 local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
122 };
123
124 let local_var_req = local_var_req_builder.build()?;
125 let local_var_resp = local_var_client.execute(local_var_req).await?;
126
127 let local_var_status = local_var_resp.status();
128 let local_var_content_type = local_var_resp
129 .headers()
130 .get("content-type")
131 .and_then(|v| v.to_str().ok())
132 .unwrap_or("application/octet-stream");
133 let local_var_content_type = super::ContentType::from(local_var_content_type);
134 let local_var_content = local_var_resp.text().await?;
135
136 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
137 match local_var_content_type {
138 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
139 ContentType::Text => return Ok(local_var_content),
140 ContentType::Unsupported(local_var_unknown_type) => {
141 return Err(Error::from(serde_json::Error::custom(format!(
142 "Received `{local_var_unknown_type}` content type response that cannot be converted to `String`"
143 ))));
144 }
145 }
146 } else {
147 let local_var_entity: Option<PostSetupPaymentError> =
148 serde_json::from_str(&local_var_content).ok();
149 let local_var_error = ResponseContent {
150 status: local_var_status,
151 content: local_var_content,
152 entity: local_var_entity,
153 };
154 Err(Error::ResponseError(local_var_error))
155 }
156 }
157}
158
159#[derive(Debug, Clone, Serialize, Deserialize)]
161#[serde(untagged)]
162pub enum PostBitPayInvoiceError {
163 UnknownValue(serde_json::Value),
164}
165#[derive(Debug, Clone, Serialize, Deserialize)]
167#[serde(untagged)]
168pub enum PostSetupPaymentError {
169 UnknownValue(serde_json::Value),
170}