use capsula_api_types::{UploadResponse, VaultExistsResponse, VaultInfo, VaultsResponse};
use reqwest::blocking::multipart;
use serde_json::Value as JsonValue;
use std::path::Path;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ClientError {
#[error("HTTP request failed: {0}")]
Request(#[from] reqwest::Error),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("JSON serialization error: {0}")]
Json(#[from] serde_json::Error),
#[error("Server returned error: {0}")]
ServerError(String),
}
pub type Result<T> = std::result::Result<T, ClientError>;
#[derive(Debug, Clone)]
pub struct CapsulaClient {
base_url: String,
client: reqwest::blocking::Client,
}
impl CapsulaClient {
pub fn new(base_url: impl Into<String>) -> Self {
Self {
base_url: base_url.into(),
client: reqwest::blocking::Client::new(),
}
}
pub fn list_vaults(&self) -> Result<Vec<VaultInfo>> {
let url = format!("{}/api/v1/vaults", self.base_url);
let response = self.client.get(&url).send()?;
if !response.status().is_success() {
return Err(ClientError::ServerError(format!(
"Failed to list vaults: {}",
response.status()
)));
}
let vaults_response: VaultsResponse = response.json()?;
Ok(vaults_response.vaults)
}
pub fn vault_exists(&self, vault_name: &str) -> Result<Option<VaultInfo>> {
let url = format!("{}/api/v1/vaults/{}", self.base_url, vault_name);
let response = self.client.get(&url).send()?;
if !response.status().is_success() {
return Err(ClientError::ServerError(format!(
"Failed to check vault: {}",
response.status()
)));
}
let vault_response: VaultExistsResponse = response.json()?;
Ok(vault_response.vault)
}
pub fn upload_run(
&self,
run_id: &str,
files: &[(impl AsRef<Path>, impl AsRef<Path>)],
pre_run_hooks: Option<Vec<JsonValue>>,
post_run_hooks: Option<Vec<JsonValue>>,
) -> Result<UploadResponse> {
let url = format!("{}/api/v1/upload", self.base_url);
let mut form = multipart::Form::new().text("run_id", run_id.to_string());
if let Some(hooks) = pre_run_hooks {
let hooks_json = serde_json::to_string(&hooks)?;
form = form.text("pre_run", hooks_json);
}
if let Some(hooks) = post_run_hooks {
let hooks_json = serde_json::to_string(&hooks)?;
form = form.text("post_run", hooks_json);
}
for (local_path, relative_path) in files {
let local_path = local_path.as_ref();
let relative_path = relative_path.as_ref();
let content = std::fs::read(local_path)?;
form = form.text("path", relative_path.to_string_lossy().to_string());
let file_name = local_path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("file");
let part = multipart::Part::bytes(content).file_name(file_name.to_string());
form = form.part("file", part);
}
let response = self.client.post(&url).multipart(form).send()?;
if !response.status().is_success() {
return Err(ClientError::ServerError(format!(
"Upload failed: {}",
response.status()
)));
}
let upload_response: UploadResponse = response.json()?;
Ok(upload_response)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_client_creation() {
let client = CapsulaClient::new("http://localhost:8500");
assert_eq!(client.base_url, "http://localhost:8500");
}
}