bitwarden_api_api/apis/
plans_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 PlansApi: Send + Sync {
29 async fn get(&self) -> Result<models::PlanResponseModelListResponseModel, Error<GetError>>;
31
32 async fn get_premium_plan(&self) -> Result<(), Error<GetPremiumPlanError>>;
34}
35
36pub struct PlansApiClient {
37 configuration: Arc<configuration::Configuration>,
38}
39
40impl PlansApiClient {
41 pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
42 Self { configuration }
43 }
44}
45
46#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
47#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
48impl PlansApi for PlansApiClient {
49 async fn get(&self) -> Result<models::PlanResponseModelListResponseModel, Error<GetError>> {
50 let local_var_configuration = &self.configuration;
51
52 let local_var_client = &local_var_configuration.client;
53
54 let local_var_uri_str = format!("{}/plans", local_var_configuration.base_path);
55 let mut local_var_req_builder =
56 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
57
58 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
59
60 let local_var_resp = local_var_req_builder.send().await?;
61
62 let local_var_status = local_var_resp.status();
63 let local_var_content_type = local_var_resp
64 .headers()
65 .get("content-type")
66 .and_then(|v| v.to_str().ok())
67 .unwrap_or("application/octet-stream");
68 let local_var_content_type = super::ContentType::from(local_var_content_type);
69 let local_var_content = local_var_resp.text().await?;
70
71 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
72 match local_var_content_type {
73 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
74 ContentType::Text => {
75 return Err(Error::from(serde_json::Error::custom(
76 "Received `text/plain` content type response that cannot be converted to `models::PlanResponseModelListResponseModel`",
77 )));
78 }
79 ContentType::Unsupported(local_var_unknown_type) => {
80 return Err(Error::from(serde_json::Error::custom(format!(
81 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::PlanResponseModelListResponseModel`"
82 ))));
83 }
84 }
85 } else {
86 let local_var_entity: Option<GetError> = serde_json::from_str(&local_var_content).ok();
87 let local_var_error = ResponseContent {
88 status: local_var_status,
89 content: local_var_content,
90 entity: local_var_entity,
91 };
92 Err(Error::ResponseError(local_var_error))
93 }
94 }
95
96 async fn get_premium_plan(&self) -> Result<(), Error<GetPremiumPlanError>> {
97 let local_var_configuration = &self.configuration;
98
99 let local_var_client = &local_var_configuration.client;
100
101 let local_var_uri_str = format!("{}/plans/premium", local_var_configuration.base_path);
102 let mut local_var_req_builder =
103 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
104
105 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
106
107 let local_var_resp = local_var_req_builder.send().await?;
108
109 let local_var_status = local_var_resp.status();
110 let local_var_content = local_var_resp.text().await?;
111
112 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
113 Ok(())
114 } else {
115 let local_var_entity: Option<GetPremiumPlanError> =
116 serde_json::from_str(&local_var_content).ok();
117 let local_var_error = ResponseContent {
118 status: local_var_status,
119 content: local_var_content,
120 entity: local_var_entity,
121 };
122 Err(Error::ResponseError(local_var_error))
123 }
124 }
125}
126
127#[derive(Debug, Clone, Serialize, Deserialize)]
129#[serde(untagged)]
130pub enum GetError {
131 UnknownValue(serde_json::Value),
132}
133#[derive(Debug, Clone, Serialize, Deserialize)]
135#[serde(untagged)]
136pub enum GetPremiumPlanError {
137 UnknownValue(serde_json::Value),
138}