bitwarden_api_api/apis/
sync_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 SyncApi: Send + Sync {
29 async fn get<'a>(
31 &self,
32 exclude_domains: Option<bool>,
33 ) -> Result<models::SyncResponseModel, Error<GetError>>;
34}
35
36pub struct SyncApiClient {
37 configuration: Arc<configuration::Configuration>,
38}
39
40impl SyncApiClient {
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 SyncApi for SyncApiClient {
49 async fn get<'a>(
50 &self,
51 exclude_domains: Option<bool>,
52 ) -> Result<models::SyncResponseModel, Error<GetError>> {
53 let local_var_configuration = &self.configuration;
54
55 let local_var_client = &local_var_configuration.client;
56
57 let local_var_uri_str = format!("{}/sync", local_var_configuration.base_path);
58 let mut local_var_req_builder =
59 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
60
61 if let Some(ref param_value) = exclude_domains {
62 local_var_req_builder =
63 local_var_req_builder.query(&[("excludeDomains", ¶m_value.to_string())]);
64 }
65 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
66 local_var_req_builder = local_var_req_builder
67 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
68 }
69 if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
70 local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
71 };
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 => {
89 return Err(Error::from(serde_json::Error::custom(
90 "Received `text/plain` content type response that cannot be converted to `models::SyncResponseModel`",
91 )));
92 }
93 ContentType::Unsupported(local_var_unknown_type) => {
94 return Err(Error::from(serde_json::Error::custom(format!(
95 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::SyncResponseModel`"
96 ))));
97 }
98 }
99 } else {
100 let local_var_entity: Option<GetError> = serde_json::from_str(&local_var_content).ok();
101 let local_var_error = ResponseContent {
102 status: local_var_status,
103 content: local_var_content,
104 entity: local_var_entity,
105 };
106 Err(Error::ResponseError(local_var_error))
107 }
108 }
109}
110
111#[derive(Debug, Clone, Serialize, Deserialize)]
113#[serde(untagged)]
114pub enum GetError {
115 UnknownValue(serde_json::Value),
116}