1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Internal module for interacting with the CipherStash Console API.

use crate::data_service_credentials::DataServiceCredentials;
use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WorkSpaceInfo {
    pub key_id: String,
    pub key_region: String,
    pub key_role_arn: String,
    pub naming_key: String,
}

#[derive(thiserror::Error, Debug)]
pub enum WorkSpaceInfoError {
    #[error("{0}")]
    RequestError(String),
}

pub async fn workspace_info(
    workspace: &str,
    credentials: &DataServiceCredentials,
) -> Result<WorkSpaceInfo, WorkSpaceInfoError> {
    let client = reqwest::Client::new();

    let resp = client
        .get(format!(
            "https://console.cipherstash.com/api/meta/workspaces/{}",
            &workspace,
        ))
        .header("authorization", credentials.as_header())
        .send()
        .await
        .map_err(|e| {
            WorkSpaceInfoError::RequestError(format!("Failed to fetch workspace info: {}", e))
        })?;

    if resp.status() != 200 {
        return Err(WorkSpaceInfoError::RequestError(format!(
            "Failed to fetch workspace info: status: {}",
            resp.status()
        )));
    }

    let workspace_info = resp.json().await.map_err(|e| {
        WorkSpaceInfoError::RequestError(format!("Failed to parse workspace info response: {}", e))
    })?;

    Ok(workspace_info)
}