use std::sync::Arc;
use serde_json::Value;
use crate::error::Error;
use crate::transport::HttpTransport;
use crate::types::{Agent, AgentProfile, Reputation};
pub struct AgentsClient {
transport: Arc<HttpTransport>,
}
impl AgentsClient {
pub fn new(transport: Arc<HttpTransport>) -> Self {
Self { transport }
}
pub async fn register(
&self,
agent_name: &str,
public_key: &str,
capabilities: Option<Vec<String>>,
) -> Result<Agent, Error> {
let mut body = serde_json::json!({
"agentName": agent_name,
"publicKey": public_key,
});
if let Some(caps) = capabilities {
body["capabilities"] = serde_json::json!(caps);
}
let response: Value = self
.transport
.unsigned_request("POST", "/v1/agents/register", None, Some(&body))
.await?;
let data = response
.get("data")
.ok_or_else(|| Error::Http("Missing data in response".to_string()))?;
serde_json::from_value(data.clone()).map_err(Error::from)
}
pub async fn get(&self, agent_id: &str) -> Result<AgentProfile, Error> {
let response: Value = self
.transport
.unsigned_request::<Value>("GET", &format!("/v1/agents/{agent_id}"), None, None::<&()>)
.await?;
let data = response
.get("data")
.ok_or_else(|| Error::Http("Missing data in response".to_string()))?;
serde_json::from_value(data.clone()).map_err(Error::from)
}
pub async fn get_reputation(&self, agent_id: &str) -> Result<Reputation, Error> {
let response: Value = self
.transport
.unsigned_request::<Value>(
"GET",
&format!("/v1/agents/{agent_id}/reputation"),
None,
None::<&()>,
)
.await?;
let data = response
.get("data")
.ok_or_else(|| Error::Http("Missing data in response".to_string()))?;
serde_json::from_value(data.clone()).map_err(Error::from)
}
}