use std::sync::Arc;
use serde_json::{json, Map, Value};
use crate::error::Error;
use crate::http::{HttpTransport, QueryParams};
use crate::models::traces::*;
#[derive(Clone)]
pub struct TracesResource {
t: Arc<HttpTransport>,
}
impl TracesResource {
pub(crate) fn new(t: Arc<HttpTransport>) -> Self {
Self { t }
}
pub async fn create(&self, params: CreateTraceParams) -> Result<CreateTraceResponse, Error> {
let mut body = Map::new();
body.insert("label".into(), json!(params.label));
if let Some(m) = params.metadata {
body.insert("metadata".into(), Value::Object(m));
}
self.t
.post("/traces", Some(&Value::Object(body)), None)
.await
}
pub async fn list(&self, params: ListTracesParams) -> Result<ListTracesResponse, Error> {
let q: QueryParams = vec![
("page", params.page.map(|v| v.to_string())),
("limit", params.limit.map(|v| v.to_string())),
("status", params.status),
];
self.t.get("/traces", Some(q)).await
}
pub async fn get(&self, trace_id: &str, params: GetTraceParams) -> Result<TraceDetail, Error> {
let q: QueryParams = vec![
("event_page", params.event_page.map(|v| v.to_string())),
("event_limit", params.event_limit.map(|v| v.to_string())),
];
self.t.get(&format!("/traces/{trace_id}"), Some(q)).await
}
pub async fn delete(&self, trace_id: &str) -> Result<DeleteTraceResponse, Error> {
self.t.delete(&format!("/traces/{trace_id}")).await
}
pub async fn seal(&self, trace_id: &str) -> Result<SealTraceResponse, Error> {
let body = json!({});
self.t
.post(&format!("/traces/{trace_id}/seal"), Some(&body), None)
.await
}
pub async fn proof(&self, trace_id: &str) -> Result<TraceProofBundle, Error> {
self.t.get(&format!("/traces/{trace_id}/proof"), None).await
}
pub async fn proof_pdf(&self, trace_id: &str) -> Result<Vec<u8>, Error> {
self.t
.get_bytes(&format!("/traces/{trace_id}/proof/pdf"))
.await
}
}