posemesh_domain_http/
reconstruction.rs

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