invoance 0.2.0

Official Rust SDK for the Invoance compliance API
Documentation
//! Traces resource — `client.traces()`.

use std::sync::Arc;

use serde_json::{json, Map, Value};

use crate::error::Error;
use crate::http::{HttpTransport, QueryParams};
use crate::models::traces::*;

/// Handle for the `/traces` endpoints.
#[derive(Clone)]
pub struct TracesResource {
    t: Arc<HttpTransport>,
}

impl TracesResource {
    pub(crate) fn new(t: Arc<HttpTransport>) -> Self {
        Self { t }
    }

    /// `POST /traces` — create a new trace.
    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
    }

    /// `GET /traces` — paginated trace listing.
    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
    }

    /// `GET /traces/{trace_id}` — get trace detail with paginated events.
    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
    }

    /// `DELETE /traces/{trace_id}` — delete an empty open trace.
    pub async fn delete(&self, trace_id: &str) -> Result<DeleteTraceResponse, Error> {
        self.t.delete(&format!("/traces/{trace_id}")).await
    }

    /// `POST /traces/{trace_id}/seal` — seal a trace (async, returns 202).
    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
    }

    /// `GET /traces/{trace_id}/proof` — export proof bundle as JSON.
    pub async fn proof(&self, trace_id: &str) -> Result<TraceProofBundle, Error> {
        self.t.get(&format!("/traces/{trace_id}/proof"), None).await
    }

    /// `GET /traces/{trace_id}/proof/pdf` — download proof bundle as PDF bytes.
    pub async fn proof_pdf(&self, trace_id: &str) -> Result<Vec<u8>, Error> {
        self.t
            .get_bytes(&format!("/traces/{trace_id}/proof/pdf"))
            .await
    }
}