1use crate::{ApiResponse, CreateKeyRequest, CreateKeyResponse, KeyInfo};
7use anyhow::{Context, Result};
8
9pub struct GoutAdminClient {
26 inner: reqwest::Client,
27 base: String,
28 admin_key: String,
29}
30
31impl GoutAdminClient {
32 pub fn new(server_addr: &str, admin_key: &str) -> Self {
39 Self {
40 inner: reqwest::Client::new(),
41 base: format!("http://{server_addr}"),
42 admin_key: admin_key.to_string(),
43 }
44 }
45
46 pub async fn create_key(&self, name: &str) -> Result<CreateKeyResponse> {
54 let resp = self
55 .inner
56 .post(format!("{}/api/v1/keys", self.base))
57 .header("X-Admin-Key", &self.admin_key)
58 .json(&CreateKeyRequest { name: name.into() })
59 .send()
60 .await
61 .context("REST create key failed")?;
62
63 crate::parse_api_response(resp).await
64 }
65
66 pub async fn list_keys(&self) -> Result<Vec<KeyInfo>> {
70 let resp = self
71 .inner
72 .get(format!("{}/api/v1/keys", self.base))
73 .header("X-Admin-Key", &self.admin_key)
74 .send()
75 .await
76 .context("REST list keys failed")?;
77
78 crate::parse_api_response(resp).await
79 }
80
81 pub async fn delete_key(&self, key: &str) -> Result<bool> {
86 let resp = self
87 .inner
88 .delete(format!("{}/api/v1/keys/{}", self.base, key))
89 .header("X-Admin-Key", &self.admin_key)
90 .send()
91 .await
92 .context("REST delete key failed")?;
93
94 let j: ApiResponse<()> = resp.json().await?;
95 Ok(j.success)
96 }
97
98 pub fn admin_key(&self) -> &str {
100 &self.admin_key
101 }
102}