use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::client::Client;
use crate::error::BoxxkiteError;
#[derive(Debug, Clone, Default, Serialize)]
pub struct CreateSecretOptions {
#[serde(skip_serializing_if = "Option::is_none")]
trust_tier: Option<String>,
}
impl CreateSecretOptions {
pub fn new() -> Self {
Self::default()
}
pub fn trust_tier(mut self, trust_tier: impl Into<String>) -> Self {
self.trust_tier = Some(trust_tier.into());
self
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct Secret {
pub id: String,
pub name: String,
pub allowed_hosts: Vec<String>,
pub trust_tier: Option<String>,
pub created_at: String,
pub last_used_at: Option<String>,
}
impl Client {
pub async fn create_secret(
&self,
name: &str,
value: &str,
allowed_hosts: &[String],
options: CreateSecretOptions,
) -> Result<Secret, BoxxkiteError> {
#[derive(Serialize)]
struct Body<'a> {
name: &'a str,
value: &'a str,
allowed_hosts: &'a [String],
#[serde(skip_serializing_if = "Option::is_none")]
trust_tier: Option<&'a str>,
}
let body = Body {
name,
value,
allowed_hosts,
trust_tier: options.trust_tier.as_deref(),
};
let builder = self.request(Method::POST, "/v1/secrets").json(&body);
self.send(builder).await
}
pub async fn list_secrets(&self) -> Result<Vec<Secret>, BoxxkiteError> {
let builder = self.request(Method::GET, "/v1/secrets");
self.send_or_default(builder).await
}
pub async fn delete_secret(&self, secret_id: &str) -> Result<(), BoxxkiteError> {
let builder = self.request(Method::DELETE, &format!("/v1/secrets/{secret_id}"));
self.send_no_content(builder).await
}
}