bitwarden_api_api/apis/
secrets_manager_porting_api.rs

1/*
2 * Bitwarden Internal API
3 *
4 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
5 *
6 * The version of the OpenAPI document: latest
7 *
8 * Generated by: https://openapi-generator.tech
9 */
10
11use 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    /// GET /sm/{organizationId}/export
30    async fn export<'a>(
31        &self,
32        organization_id: uuid::Uuid,
33    ) -> Result<models::SmExportResponseModel, Error<ExportError>>;
34
35    /// POST /sm/{organizationId}/import
36    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_token) = local_var_configuration.oauth_access_token {
73            local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
74        };
75        local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
76
77        let local_var_req = local_var_req_builder.build()?;
78        let local_var_resp = local_var_client.execute(local_var_req).await?;
79
80        let local_var_status = local_var_resp.status();
81        let local_var_content_type = local_var_resp
82            .headers()
83            .get("content-type")
84            .and_then(|v| v.to_str().ok())
85            .unwrap_or("application/octet-stream");
86        let local_var_content_type = super::ContentType::from(local_var_content_type);
87        let local_var_content = local_var_resp.text().await?;
88
89        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
90            match local_var_content_type {
91                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
92                ContentType::Text => {
93                    return Err(Error::from(serde_json::Error::custom(
94                        "Received `text/plain` content type response that cannot be converted to `models::SmExportResponseModel`",
95                    )));
96                }
97                ContentType::Unsupported(local_var_unknown_type) => {
98                    return Err(Error::from(serde_json::Error::custom(format!(
99                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::SmExportResponseModel`"
100                    ))));
101                }
102            }
103        } else {
104            let local_var_entity: Option<ExportError> =
105                serde_json::from_str(&local_var_content).ok();
106            let local_var_error = ResponseContent {
107                status: local_var_status,
108                content: local_var_content,
109                entity: local_var_entity,
110            };
111            Err(Error::ResponseError(local_var_error))
112        }
113    }
114
115    async fn import<'a>(
116        &self,
117        organization_id: uuid::Uuid,
118        sm_import_request_model: Option<models::SmImportRequestModel>,
119    ) -> Result<(), Error<ImportError>> {
120        let local_var_configuration = &self.configuration;
121
122        let local_var_client = &local_var_configuration.client;
123
124        let local_var_uri_str = format!(
125            "{}/sm/{organizationId}/import",
126            local_var_configuration.base_path,
127            organizationId = organization_id
128        );
129        let mut local_var_req_builder =
130            local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
131
132        if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
133            local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
134        };
135        local_var_req_builder = local_var_req_builder.with_extension(AuthRequired::Bearer);
136        local_var_req_builder = local_var_req_builder.json(&sm_import_request_model);
137
138        let local_var_req = local_var_req_builder.build()?;
139        let local_var_resp = local_var_client.execute(local_var_req).await?;
140
141        let local_var_status = local_var_resp.status();
142        let local_var_content = local_var_resp.text().await?;
143
144        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
145            Ok(())
146        } else {
147            let local_var_entity: Option<ImportError> =
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/// struct for typed errors of method [`SecretsManagerPortingApi::export`]
160#[derive(Debug, Clone, Serialize, Deserialize)]
161#[serde(untagged)]
162pub enum ExportError {
163    UnknownValue(serde_json::Value),
164}
165/// struct for typed errors of method [`SecretsManagerPortingApi::import`]
166#[derive(Debug, Clone, Serialize, Deserialize)]
167#[serde(untagged)]
168pub enum ImportError {
169    UnknownValue(serde_json::Value),
170}