use serde::Deserialize;
use serde_json::Value;
use crate::error::Result;
use crate::api_client::CxClient;
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DashboardFolder {
pub id: Option<String>,
pub name: Option<String>,
pub parent_id: Option<String>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DashboardCatalogItem {
pub id: Option<String>,
pub name: Option<String>,
pub description: Option<String>,
pub slug_name: Option<String>,
pub create_time: Option<String>,
pub update_time: Option<String>,
pub is_default: Option<bool>,
pub is_pinned: Option<bool>,
pub is_locked: Option<bool>,
pub folder: Option<DashboardFolder>,
}
#[derive(Debug, Deserialize)]
pub struct DashboardCatalogResponse {
pub items: Vec<DashboardCatalogItem>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DashboardFolderItem {
pub id: Option<String>,
pub name: Option<String>,
pub parent_id: Option<String>,
}
impl DashboardFolderItem {
pub fn id_str(&self) -> Option<&str> {
self.id.as_deref()
}
pub fn parent_id_str(&self) -> Option<&str> {
self.parent_id.as_deref()
}
}
#[derive(Debug, Deserialize)]
pub struct DashboardFoldersResponse {
#[serde(default, rename = "folder")]
pub folders: Vec<DashboardFolderItem>,
}
const DASHBOARDS_BASE: &str = "/mgmt/openapi/5/dashboards/dashboards/v1";
const FOLDERS_BASE: &str = "/mgmt/openapi/5/dashboards/folders/v1";
#[derive(Debug, Deserialize)]
pub struct DeleteDashboardResponse {}
#[derive(Debug, Deserialize)]
pub struct DeleteDashboardFolderResponse {}
pub struct DashboardsApi<'a> {
client: &'a CxClient,
}
impl<'a> DashboardsApi<'a> {
pub fn new(client: &'a CxClient) -> Self {
Self { client }
}
pub async fn catalog(&self) -> Result<DashboardCatalogResponse> {
self.client
.get(&format!("{DASHBOARDS_BASE}/catalog"), &[])
.await
}
pub async fn get(&self, id: &str) -> Result<Value> {
self.client
.get(&format!("{DASHBOARDS_BASE}/{id}"), &[])
.await
}
pub async fn create(&self, body: &Value) -> Result<Value> {
self.client.post(DASHBOARDS_BASE, body).await
}
pub async fn folders(&self) -> Result<DashboardFoldersResponse> {
self.client.get(FOLDERS_BASE, &[]).await
}
pub async fn folders_create(&self, body: &Value) -> Result<Value> {
self.client.post(FOLDERS_BASE, body).await
}
pub async fn delete(&self, id: &str) -> Result<DeleteDashboardResponse> {
let path = format!("{DASHBOARDS_BASE}/{id}");
self.client.delete(&path).await
}
pub async fn folders_delete(&self, id: &str) -> Result<DeleteDashboardFolderResponse> {
let path = format!("{FOLDERS_BASE}/{id}");
self.client.delete(&path).await
}
}