Skip to main content

auths_infra_http/
claim_client.rs

1//! Auths registry platform claim submission HTTP implementation.
2
3use std::future::Future;
4
5use serde::Deserialize;
6
7use auths_core::ports::platform::{ClaimResponse, PlatformError, RegistryClaimClient};
8
9use crate::default_http_client;
10use crate::error::{map_reqwest_error, map_status_error};
11
12#[derive(Deserialize)]
13struct ServerClaimResponse {
14    platform: String,
15    namespace: String,
16    did: String,
17}
18
19/// HTTP implementation that submits platform identity claims to the auths registry.
20///
21/// Usage:
22/// ```ignore
23/// let client = HttpRegistryClaimClient::new();
24/// let response = client.submit_claim(registry_url, &did, &proof_url).await?;
25/// println!("{}", response.message);
26/// ```
27pub struct HttpRegistryClaimClient {
28    client: reqwest::Client,
29}
30
31impl HttpRegistryClaimClient {
32    /// Create a new client with a default HTTP client.
33    pub fn new() -> Self {
34        Self {
35            client: default_http_client(),
36        }
37    }
38}
39
40impl Default for HttpRegistryClaimClient {
41    fn default() -> Self {
42        Self::new()
43    }
44}
45
46impl RegistryClaimClient for HttpRegistryClaimClient {
47    fn submit_claim(
48        &self,
49        registry_url: &str,
50        did: &str,
51        proof_url: &str,
52    ) -> impl Future<Output = Result<ClaimResponse, PlatformError>> + Send {
53        let client = self.client.clone();
54        let url = format!(
55            "{}/v1/identities/{}/claims",
56            registry_url.trim_end_matches('/'),
57            did
58        );
59        let proof_url = proof_url.to_string();
60        async move {
61            let resp = client
62                .post(&url)
63                .header("Content-Type", "application/json")
64                .json(&serde_json::json!({ "proof_url": proof_url }))
65                .send()
66                .await
67                .map_err(|e| PlatformError::Network(map_reqwest_error(e, &url)))?;
68
69            let status = resp.status();
70
71            if !status.is_success() {
72                return Err(PlatformError::Network(map_status_error(
73                    status.as_u16(),
74                    &url,
75                )));
76            }
77
78            let server: ServerClaimResponse =
79                resp.json().await.map_err(|e| PlatformError::Platform {
80                    message: format!("failed to parse claim response: {e}"),
81                })?;
82
83            Ok(ClaimResponse {
84                message: format!(
85                    "Platform claim indexed: {} @{} -> {}",
86                    server.platform, server.namespace, server.did
87                ),
88            })
89        }
90    }
91}