use crate::models::{V1AgentKey, V1CreateAgentKeyRequest};
use anyhow::{anyhow, Result};
use reqwest::Client;
use tracing::{debug, info};
pub async fn create_agent_key(
base_url: &str,
api_key: &str,
request: V1CreateAgentKeyRequest,
) -> Result<V1AgentKey> {
let client = Client::new();
let url = format!("{}/v1/agent/keys", base_url);
debug!(
"Creating agent key with request: {:?} and key: {}",
request, api_key
);
let response = client
.post(&url)
.header("Authorization", format!("Bearer {}", api_key))
.json(&request)
.send()
.await?;
if !response.status().is_success() {
let status = response.status();
let error_text = response.text().await?;
return Err(anyhow!("API error {}: {}", status.as_u16(), error_text));
}
let agent_key: V1AgentKey = response.json().await?;
info!("Agent key created: {:?}", agent_key);
Ok(agent_key)
}