Skip to main content

posemesh_domain_http/
reconstruction.rs

1use reqwest::{Client, Response};
2use serde::{Deserialize, Serialize};
3
4use crate::errors::{AukiErrorResponse, DomainError};
5
6#[derive(Debug, Serialize, Deserialize)]
7pub struct JobRequest {
8    pub data_ids: Vec<String>,
9    #[serde(default = "default_processing_type")]
10    pub processing_type: String,
11    #[serde(default = "default_api_key")]
12    pub server_api_key: String,
13    pub server_url: String,
14}
15
16fn default_processing_type() -> String {
17    "local_and_global_refinement".to_string()
18}
19
20fn default_api_key() -> String {
21    "aukilabs123".to_string()
22}
23
24impl Default for JobRequest {
25    fn default() -> Self {
26        Self {
27            data_ids: vec![],
28            processing_type: default_processing_type(),
29            server_api_key: default_api_key(),
30            server_url: "".to_string(),
31        }
32    }
33}
34
35pub async fn forward_job_request_v1(
36    domain_server_url: &str,
37    client_id: &str,
38    access_token: &str,
39    domain_id: &str,
40    request: &JobRequest,
41) -> Result<Response, DomainError> {
42    let response = Client::new()
43        .post(format!(
44            "{}/api/v1/domains/{}/process",
45            domain_server_url, domain_id
46        ))
47        .bearer_auth(access_token)
48        .header("posemesh-client-id", client_id)
49        .json(&request)
50        .send()
51        .await?;
52
53    if response.status().is_success() {
54        Ok(response)
55    } else {
56        let status = response.status();
57        let text = response
58            .text()
59            .await
60            .unwrap_or_else(|_| "Unknown error".to_string());
61        Err(AukiErrorResponse {
62            status,
63            error: format!("Failed to process domain. {}", text),
64        }
65        .into())
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn test_job_request_structure() {
75        let json =
76            r#"{"data_ids":["test-id-1", "test-id-2"], "server_url": "https://example.com"}"#;
77        assert!(json.contains("test-id-1"));
78        assert!(json.contains("https://example.com"));
79
80        let deserialized: JobRequest = serde_json::from_str(json).unwrap();
81        assert_eq!(deserialized.data_ids.len(), 2);
82        assert_eq!(deserialized.processing_type, "local_and_global_refinement");
83        assert_eq!(deserialized.server_api_key, "aukilabs123");
84        assert_eq!(deserialized.server_url, "https://example.com")
85    }
86}