ipscanner/resources/
provenance.rs1use serde_json::json;
2
3use crate::types::{
4 AnchoredVerification, Chain, ChainParams, ChainVerification, ExportParams, Jurisdiction,
5 Jurisdictions, ProvenanceCheck, ProvenanceCheckRequest,
6};
7use crate::{Client, Error};
8
9#[derive(Debug, Clone, Copy)]
11pub struct Provenance<'a> {
12 client: &'a Client,
13}
14
15impl<'a> Provenance<'a> {
16 pub(crate) fn new(client: &'a Client) -> Self {
17 Provenance { client }
18 }
19
20 pub async fn check(&self, req: &ProvenanceCheckRequest) -> Result<ProvenanceCheck, Error> {
22 self.client.post("/provenance/check", req).await
23 }
24
25 pub async fn verify(&self) -> Result<ChainVerification, Error> {
27 self.client.get("/provenance/verify", &[]).await
28 }
29
30 pub async fn verify_anchored(&self) -> Result<AnchoredVerification, Error> {
32 self.client.post("/provenance/verify", &json!({})).await
33 }
34
35 pub async fn chain(&self, params: &ChainParams) -> Result<Chain, Error> {
37 let mut q = Vec::new();
38 if let Some(v) = params.limit {
39 q.push(("limit", v.to_string()));
40 }
41 if let Some(v) = params.before {
42 q.push(("before", v.to_string()));
43 }
44 self.client.get("/provenance/chain", &q).await
45 }
46
47 pub async fn jurisdictions(&self) -> Result<Vec<Jurisdiction>, Error> {
49 let res: Jurisdictions = self.client.get("/provenance/jurisdictions", &[]).await?;
50 Ok(res.jurisdictions)
51 }
52
53 pub async fn export(&self, params: &ExportParams) -> Result<String, Error> {
55 let mut q = Vec::new();
56 if let Some(v) = ¶ms.from {
57 q.push(("from", v.clone()));
58 }
59 if let Some(v) = ¶ms.to {
60 q.push(("to", v.clone()));
61 }
62 self.client
63 .get_text("/provenance/export", &q, "text/csv, application/json")
64 .await
65 }
66}