use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use super::{ManagementClient, Result};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiKeyListResponse {
pub data: Vec<ApiKeyEnvelope>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiKeyEnvelope {
pub id: String,
pub display_name: String,
pub key_prefix: String,
pub scopes: Vec<String>,
#[serde(default)]
pub expires_at: Option<DateTime<Utc>>,
#[serde(default)]
pub last_used_at: Option<DateTime<Utc>>,
#[serde(default)]
pub revoked_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize)]
pub struct MintApiKeyRequest {
pub display_name: String,
pub scopes: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub expires_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MintApiKeyResponse {
pub token: String,
pub id: String,
pub key_prefix: String,
pub display_name: String,
pub scopes: Vec<String>,
#[serde(default)]
pub expires_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RevokeApiKeyResponse {
pub revoked: bool,
}
impl ManagementClient {
pub async fn list_keys(&self) -> Result<ApiKeyListResponse> {
let path = self.namespaced("/keys")?;
self.get_json(&path).await
}
pub async fn mint_key(&self, body: &MintApiKeyRequest) -> Result<MintApiKeyResponse> {
let path = self.namespaced("/keys")?;
self.post_json(&path, body).await
}
pub async fn revoke_key(&self, id: &str) -> Result<RevokeApiKeyResponse> {
let path = self.namespaced(&format!("/keys/{id}"))?;
self.delete_json(&path).await
}
}