use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use super::{ManagementClient, Result};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PresetEnvelope {
pub id: String,
pub name: String,
#[serde(default)]
pub guardrail: Option<serde_json::Value>,
#[serde(default)]
pub budget: Option<serde_json::Value>,
#[serde(default)]
pub rate_limit: Option<serde_json::Value>,
#[serde(default)]
pub disabled_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PresetListResponse {
pub data: Vec<PresetEnvelope>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct CreatePresetRequest {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub guardrail: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub budget: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub rate_limit: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct UpdatePresetRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub guardrail: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub budget: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub rate_limit: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "is_false")]
pub clear_guardrail: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub clear_budget: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub clear_rate_limit: bool,
}
fn is_false(b: &bool) -> bool {
!*b
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeletePresetResponse {
pub deleted: bool,
}
impl ManagementClient {
pub async fn list_presets(&self) -> Result<PresetListResponse> {
let path = self.namespaced("/presets")?;
self.get_json(&path).await
}
pub async fn get_preset(&self, id: &str) -> Result<PresetEnvelope> {
let path = self.namespaced(&format!("/presets/{id}"))?;
self.get_json(&path).await
}
pub async fn create_preset(&self, body: &CreatePresetRequest) -> Result<PresetEnvelope> {
let path = self.namespaced("/presets")?;
self.post_json(&path, body).await
}
pub async fn update_preset(
&self,
id: &str,
body: &UpdatePresetRequest,
) -> Result<PresetEnvelope> {
let path = self.namespaced(&format!("/presets/{id}"))?;
self.put_json(&path, body).await
}
pub async fn delete_preset(&self, id: &str) -> Result<DeletePresetResponse> {
let path = self.namespaced(&format!("/presets/{id}"))?;
self.delete_json(&path).await
}
}