Skip to main content

confium_log_monitor/
client.rs

1//! HTTP client for the transparency log server.
2
3use anyhow::{Context, Result};
4use serde::Deserialize;
5
6#[derive(Debug, Clone, Deserialize)]
7pub struct TreeHead {
8    pub tree_size: u64,
9    pub root: String,
10    pub timestamp: String,
11}
12
13#[derive(Debug, Clone, Deserialize)]
14pub struct ConsistencyProof {
15    pub old_size: u64,
16    pub new_size: u64,
17    pub new_root: String,
18    pub proof: Vec<String>,
19}
20
21pub struct LogClient {
22    base_url: String,
23    http: reqwest::Client,
24}
25
26impl LogClient {
27    pub fn new(base_url: String) -> Self {
28        Self {
29            base_url: base_url.trim_end_matches('/').to_string(),
30            http: reqwest::Client::builder()
31                .timeout(std::time::Duration::from_secs(10))
32                .build()
33                .expect("reqwest client"),
34        }
35    }
36
37    pub async fn fetch_head(&self) -> Result<TreeHead> {
38        let url = format!("{}/v1/head", self.base_url);
39        let head = self
40            .http
41            .get(&url)
42            .send()
43            .await?
44            .error_for_status()?
45            .json::<TreeHead>()
46            .await
47            .context("decoding /v1/head response")?;
48        Ok(head)
49    }
50
51    pub async fn fetch_consistency(&self, old_size: u64) -> Result<ConsistencyProof> {
52        let url = format!("{}/v1/consistency/{}", self.base_url, old_size);
53        let proof = self
54            .http
55            .get(&url)
56            .send()
57            .await?
58            .error_for_status()?
59            .json::<ConsistencyProof>()
60            .await
61            .context("decoding /v1/consistency response")?;
62        Ok(proof)
63    }
64}