posemesh_domain_http/
reconstruction.rs

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