posemesh_domain_http/
reconstruction.rs1use 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 response = Client::new()
44 .post(format!("{}/api/v1/domains/{}/process", domain_server_url, domain_id))
45 .bearer_auth(access_token)
46 .header("posemesh-client-id", client_id)
47 .json(&request)
48 .send()
49 .await?;
50
51 if response.status().is_success() {
52 Ok(response)
53 } else {
54 let status = response.status();
55 let text = response
56 .text()
57 .await
58 .unwrap_or_else(|_| "Unknown error".to_string());
59 Err(format!("Failed to process domain. Status: {} - {}", status, text).into())
60 }
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn test_job_request_structure() {
69 let json = r#"{"data_ids":["test-id-1", "test-id-2"], "server_url": "https://example.com"}"#;
70 assert!(json.contains("test-id-1"));
71 assert!(json.contains("https://example.com"));
72
73 let deserialized: JobRequest = serde_json::from_str(&json).unwrap();
74 assert_eq!(deserialized.data_ids.len(), 2);
75 assert_eq!(deserialized.processing_type, "local_and_global_refinement");
76 assert_eq!(deserialized.server_api_key, "aukilabs123");
77 assert_eq!(deserialized.server_url, "https://example.com")}
78}