use serde_json::{json, Value};
use crate::error::Result;
use crate::http::HttpClient;
pub struct Memories<'a> {
http: &'a HttpClient,
}
impl<'a> Memories<'a> {
pub(crate) fn new(http: &'a HttpClient) -> Self {
Self { http }
}
pub async fn list(&self) -> Result<Vec<Value>> {
let resp = self.http._get("/memories", None).await?;
match resp {
Value::Object(mut map) => Ok(map
.remove("memories")
.and_then(|v| match v {
Value::Array(a) => Some(a),
_ => None,
})
.unwrap_or_default()),
Value::Array(a) => Ok(a),
_ => Ok(Vec::new()),
}
}
pub async fn create(&self, name: &str, description: &str) -> Result<Value> {
let body = json!({ "name": name, "description": description });
self.http._post("/memories", Some(&body)).await
}
pub async fn get(&self, memory_id: &str) -> Result<Value> {
let path = format!("/memories/{memory_id}");
self.http._get(&path, None).await
}
pub async fn delete(&self, memory_id: &str) -> Result<()> {
let path = format!("/memories/{memory_id}");
self.http._delete(&path).await.map(|_| ())
}
pub async fn stats(&self, memory_id: &str) -> Result<Value> {
let path = format!("/memories/{memory_id}/stats");
self.http._get(&path, None).await
}
pub async fn stats_history(&self, memory_id: &str, filters: Option<&Value>) -> Result<Value> {
let path = format!("/memories/{memory_id}/stats/history");
self.http._get(&path, filters).await
}
}