Skip to main content

flux_verify_api/plato/
client.rs

1use reqwest::Client;
2use serde_json::json;
3
4/// PLATO client for submitting verification tiles.
5#[derive(Debug, Clone)]
6pub struct PlatoClient {
7    base_url: String,
8    token: Option<String>,
9    client: Client,
10}
11
12impl PlatoClient {
13    pub fn new(base_url: String, token: Option<String>) -> Self {
14        Self {
15            base_url,
16            token,
17            client: Client::new(),
18        }
19    }
20
21    /// Submit a verification result to PLATO.
22    pub async fn submit(
23        &self,
24        proof_hash: &str,
25        verdict: &str,
26        claim: &str,
27    ) -> Result<String, String> {
28        let url = format!("{}/tiles/submit", self.base_url);
29
30        let body = json!({
31            "proof_hash": proof_hash,
32            "verdict": verdict,
33            "claim": claim,
34        });
35
36        let mut req = self.client.post(&url).json(&body);
37        if let Some(ref token) = self.token {
38            req = req.bearer_auth(token);
39        }
40
41        let resp = req
42            .send()
43            .await
44            .map_err(|e| format!("PLATO request failed: {}", e))?;
45
46        if resp.status().is_success() {
47            Ok(format!("submitted-{}", proof_hash.len()))
48        } else {
49            Err(format!("PLATO returned status {}", resp.status()))
50        }
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_plato_client_construction() {
60        let client = PlatoClient::new("http://localhost:9999".into(), Some("test-token".into()));
61        assert_eq!(client.base_url, "http://localhost:9999");
62        assert_eq!(client.token, Some("test-token".into()));
63    }
64
65    #[test]
66    fn test_plato_client_no_token() {
67        let client = PlatoClient::new("http://localhost:9999".into(), None);
68        assert!(client.token.is_none());
69    }
70}