bitwarden_api_api/apis/
secrets_manager_porting_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 SecretsManagerPortingApi: Send + Sync {
29 async fn export<'a>(
31 &self,
32 organization_id: uuid::Uuid,
33 ) -> Result<models::SmExportResponseModel, Error<ExportError>>;
34
35 async fn import<'a>(
37 &self,
38 organization_id: uuid::Uuid,
39 sm_import_request_model: Option<models::SmImportRequestModel>,
40 ) -> Result<(), Error<ImportError>>;
41}
42
43pub struct SecretsManagerPortingApiClient {
44 configuration: Arc<configuration::Configuration>,
45}
46
47impl SecretsManagerPortingApiClient {
48 pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
49 Self { configuration }
50 }
51}
52
53#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
54#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
55impl SecretsManagerPortingApi for SecretsManagerPortingApiClient {
56 async fn export<'a>(
57 &self,
58 organization_id: uuid::Uuid,
59 ) -> Result<models::SmExportResponseModel, Error<ExportError>> {
60 let local_var_configuration = &self.configuration;
61
62 let local_var_client = &local_var_configuration.client;
63
64 let local_var_uri_str = format!(
65 "{}/sm/{organizationId}/export",
66 local_var_configuration.base_path,
67 organizationId = organization_id
68 );
69 let mut local_var_req_builder =
70 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
71
72 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
73
74 let local_var_resp = local_var_req_builder.send().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::SmExportResponseModel`",
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::SmExportResponseModel`"
96 ))));
97 }
98 }
99 } else {
100 let local_var_entity: Option<ExportError> =
101 serde_json::from_str(&local_var_content).ok();
102 let local_var_error = ResponseContent {
103 status: local_var_status,
104 content: local_var_content,
105 entity: local_var_entity,
106 };
107 Err(Error::ResponseError(local_var_error))
108 }
109 }
110
111 async fn import<'a>(
112 &self,
113 organization_id: uuid::Uuid,
114 sm_import_request_model: Option<models::SmImportRequestModel>,
115 ) -> Result<(), Error<ImportError>> {
116 let local_var_configuration = &self.configuration;
117
118 let local_var_client = &local_var_configuration.client;
119
120 let local_var_uri_str = format!(
121 "{}/sm/{organizationId}/import",
122 local_var_configuration.base_path,
123 organizationId = organization_id
124 );
125 let mut local_var_req_builder =
126 local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
127
128 local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
129 local_var_req_builder = local_var_req_builder.json(&sm_import_request_model);
130
131 let local_var_resp = local_var_req_builder.send().await?;
132
133 let local_var_status = local_var_resp.status();
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 Ok(())
138 } else {
139 let local_var_entity: Option<ImportError> =
140 serde_json::from_str(&local_var_content).ok();
141 let local_var_error = ResponseContent {
142 status: local_var_status,
143 content: local_var_content,
144 entity: local_var_entity,
145 };
146 Err(Error::ResponseError(local_var_error))
147 }
148 }
149}
150
151#[derive(Debug, Clone, Serialize, Deserialize)]
153#[serde(untagged)]
154pub enum ExportError {
155 UnknownValue(serde_json::Value),
156}
157#[derive(Debug, Clone, Serialize, Deserialize)]
159#[serde(untagged)]
160pub enum ImportError {
161 UnknownValue(serde_json::Value),
162}