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::{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 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
73 local_var_req_builder = local_var_req_builder
74 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
75 }
76 if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
77 local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
78 };
79
80 let local_var_req = local_var_req_builder.build()?;
81 let local_var_resp = local_var_client.execute(local_var_req).await?;
82
83 let local_var_status = local_var_resp.status();
84 let local_var_content_type = local_var_resp
85 .headers()
86 .get("content-type")
87 .and_then(|v| v.to_str().ok())
88 .unwrap_or("application/octet-stream");
89 let local_var_content_type = super::ContentType::from(local_var_content_type);
90 let local_var_content = local_var_resp.text().await?;
91
92 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
93 match local_var_content_type {
94 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
95 ContentType::Text => {
96 return Err(Error::from(serde_json::Error::custom(
97 "Received `text/plain` content type response that cannot be converted to `models::SmExportResponseModel`",
98 )));
99 }
100 ContentType::Unsupported(local_var_unknown_type) => {
101 return Err(Error::from(serde_json::Error::custom(format!(
102 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::SmExportResponseModel`"
103 ))));
104 }
105 }
106 } else {
107 let local_var_entity: Option<ExportError> =
108 serde_json::from_str(&local_var_content).ok();
109 let local_var_error = ResponseContent {
110 status: local_var_status,
111 content: local_var_content,
112 entity: local_var_entity,
113 };
114 Err(Error::ResponseError(local_var_error))
115 }
116 }
117
118 async fn import<'a>(
119 &self,
120 organization_id: uuid::Uuid,
121 sm_import_request_model: Option<models::SmImportRequestModel>,
122 ) -> Result<(), Error<ImportError>> {
123 let local_var_configuration = &self.configuration;
124
125 let local_var_client = &local_var_configuration.client;
126
127 let local_var_uri_str = format!(
128 "{}/sm/{organizationId}/import",
129 local_var_configuration.base_path,
130 organizationId = organization_id
131 );
132 let mut local_var_req_builder =
133 local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
134
135 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
136 local_var_req_builder = local_var_req_builder
137 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
138 }
139 if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
140 local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
141 };
142 local_var_req_builder = local_var_req_builder.json(&sm_import_request_model);
143
144 let local_var_req = local_var_req_builder.build()?;
145 let local_var_resp = local_var_client.execute(local_var_req).await?;
146
147 let local_var_status = local_var_resp.status();
148 let local_var_content = local_var_resp.text().await?;
149
150 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
151 Ok(())
152 } else {
153 let local_var_entity: Option<ImportError> =
154 serde_json::from_str(&local_var_content).ok();
155 let local_var_error = ResponseContent {
156 status: local_var_status,
157 content: local_var_content,
158 entity: local_var_entity,
159 };
160 Err(Error::ResponseError(local_var_error))
161 }
162 }
163}
164
165#[derive(Debug, Clone, Serialize, Deserialize)]
167#[serde(untagged)]
168pub enum ExportError {
169 UnknownValue(serde_json::Value),
170}
171#[derive(Debug, Clone, Serialize, Deserialize)]
173#[serde(untagged)]
174pub enum ImportError {
175 UnknownValue(serde_json::Value),
176}