use crate::{ApiResponse, CreateKeyRequest, CreateKeyResponse, KeyInfo};
use anyhow::{Context, Result};
pub struct GoutAdminClient {
inner: reqwest::Client,
base: String,
admin_key: String,
}
impl GoutAdminClient {
pub fn new(server_addr: &str, admin_key: &str) -> Self {
Self {
inner: reqwest::Client::new(),
base: format!("http://{server_addr}"),
admin_key: admin_key.to_string(),
}
}
pub async fn create_key(&self, name: &str) -> Result<CreateKeyResponse> {
let resp = self
.inner
.post(format!("{}/api/v1/keys", self.base))
.header("X-Admin-Key", &self.admin_key)
.json(&CreateKeyRequest { name: name.into() })
.send()
.await
.context("REST create key failed")?;
if !resp.status().is_success() {
let j: ApiResponse<CreateKeyResponse> = resp.json().await?;
anyhow::bail!("{}", j.error.unwrap_or_default());
}
let j: ApiResponse<CreateKeyResponse> = resp.json().await?;
j.data.context("no key data")
}
pub async fn list_keys(&self) -> Result<Vec<KeyInfo>> {
let resp = self
.inner
.get(format!("{}/api/v1/keys", self.base))
.header("X-Admin-Key", &self.admin_key)
.send()
.await
.context("REST list keys failed")?;
let j: ApiResponse<Vec<KeyInfo>> = resp.json().await?;
Ok(j.data.unwrap_or_default())
}
pub async fn delete_key(&self, key: &str) -> Result<bool> {
let resp = self
.inner
.delete(format!("{}/api/v1/keys/{}", self.base, key))
.header("X-Admin-Key", &self.admin_key)
.send()
.await
.context("REST delete key failed")?;
let j: ApiResponse<()> = resp.json().await?;
Ok(j.success)
}
pub fn admin_key(&self) -> &str {
&self.admin_key
}
}