redis_cloud/handlers/
api_keys.rs

1//! API keys management handler
2
3use crate::{Result, client::CloudClient};
4use serde_json::Value;
5
6/// Handler for Cloud API keys management
7pub struct CloudApiKeysHandler {
8    client: CloudClient,
9}
10
11impl CloudApiKeysHandler {
12    pub fn new(client: CloudClient) -> Self {
13        CloudApiKeysHandler { client }
14    }
15
16    /// List all API keys
17    pub async fn list(&self) -> Result<Value> {
18        self.client.get("/api-keys").await
19    }
20
21    /// Get API key by ID
22    pub async fn get(&self, key_id: u32) -> Result<Value> {
23        self.client.get(&format!("/api-keys/{}", key_id)).await
24    }
25
26    /// Create API key
27    pub async fn create(&self, request: Value) -> Result<Value> {
28        self.client.post("/api-keys", &request).await
29    }
30
31    /// Update API key
32    pub async fn update(&self, key_id: u32, request: Value) -> Result<Value> {
33        self.client
34            .put(&format!("/api-keys/{}", key_id), &request)
35            .await
36    }
37
38    /// Delete API key
39    pub async fn delete(&self, key_id: u32) -> Result<Value> {
40        self.client.delete(&format!("/api-keys/{}", key_id)).await?;
41        Ok(serde_json::json!({"message": format!("API key {} deleted", key_id)}))
42    }
43
44    /// Regenerate API key secret
45    pub async fn regenerate(&self, key_id: u32) -> Result<Value> {
46        self.client
47            .post(&format!("/api-keys/{}/regenerate", key_id), &Value::Null)
48            .await
49    }
50
51    /// Get API key permissions
52    pub async fn get_permissions(&self, key_id: u32) -> Result<Value> {
53        self.client
54            .get(&format!("/api-keys/{}/permissions", key_id))
55            .await
56    }
57
58    /// Update API key permissions
59    pub async fn update_permissions(&self, key_id: u32, request: Value) -> Result<Value> {
60        self.client
61            .put(&format!("/api-keys/{}/permissions", key_id), &request)
62            .await
63    }
64
65    /// Enable API key
66    pub async fn enable(&self, key_id: u32) -> Result<Value> {
67        self.client
68            .post(&format!("/api-keys/{}/enable", key_id), &Value::Null)
69            .await
70    }
71
72    /// Disable API key
73    pub async fn disable(&self, key_id: u32) -> Result<Value> {
74        self.client
75            .post(&format!("/api-keys/{}/disable", key_id), &Value::Null)
76            .await
77    }
78
79    /// Get API key usage statistics
80    pub async fn get_usage(&self, key_id: u32, period: &str) -> Result<Value> {
81        self.client
82            .get(&format!("/api-keys/{}/usage?period={}", key_id, period))
83            .await
84    }
85
86    /// List API key audit logs
87    pub async fn get_audit_logs(&self, key_id: u32) -> Result<Value> {
88        self.client
89            .get(&format!("/api-keys/{}/audit", key_id))
90            .await
91    }
92}