go_server_rust_sdk/worker/
call.rs

1//! Remote method call functionality for the worker module
2
3use serde_json::Value;
4use crate::scheduler::Client;
5use crate::error::Result;
6
7/// Calls a remote method on the scheduler
8/// 
9/// This function provides a simple interface for making remote method calls
10/// to the scheduler. It handles task execution and result retrieval automatically.
11/// 
12/// # Arguments
13/// 
14/// * `scheduler_url` - The HTTP URL of the scheduler
15/// * `method` - The name of the method to call
16/// * `params` - The parameters to pass to the method
17/// 
18/// # Returns
19/// 
20/// A `Result` containing the method result or an error
21/// 
22/// # Example
23/// 
24/// ```rust
25/// use go_server_rust_sdk::worker::call;
26/// use serde_json::json;
27/// 
28/// # #[tokio::main]
29/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
30/// let result = call(
31///     "http://localhost:8080",
32///     "add",
33///     json!({"a": 1, "b": 2})
34/// ).await?;
35/// 
36/// println!("Result: {}", result);
37/// # Ok(())
38/// # }
39/// ```
40pub async fn call(scheduler_url: &str, method: &str, params: Value) -> Result<Value> {
41    use std::time::Duration;
42    let client = Client::new(scheduler_url.to_string());
43    
44    // Execute the task synchronously (with polling)
45    let result = client.execute_sync(method, params, Duration::from_secs(30)).await?;
46    
47    // Extract the actual result value from the response
48    match result.result {
49        Some(value) => Ok(value),
50        None => Err(crate::error::SdkError::Task("No result returned".to_string())),
51    }
52}
53
54/// Calls a remote method on the scheduler with encryption
55/// 
56/// This function provides a simple interface for making encrypted remote method calls
57/// to the scheduler. It handles encryption, task execution, and result decryption automatically.
58/// 
59/// # Arguments
60/// 
61/// * `scheduler_url` - The HTTP URL of the scheduler
62/// * `method` - The name of the method to call
63/// * `params` - The parameters to pass to the method
64/// * `key` - The encryption key to use
65/// 
66/// # Returns
67/// 
68/// A `Result` containing the method result or an error
69/// 
70/// # Example
71/// 
72/// ```rust
73/// use go_server_rust_sdk::worker::call_encrypted;
74/// use serde_json::json;
75/// 
76/// # #[tokio::main]
77/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
78/// let result = call_encrypted(
79///     "http://localhost:8080",
80///     "add",
81///     json!({"a": 1, "b": 2}),
82///     "my-secret-key"
83/// ).await?;
84/// 
85/// println!("Result: {}", result);
86/// # Ok(())
87/// # }
88/// ```
89#[allow(dead_code)]
90pub async fn call_encrypted(
91    scheduler_url: &str,
92    method: &str,
93    params: Value,
94    key: &str,
95) -> Result<Value> {
96    use std::time::Duration;
97    use rand::Rng;
98    let client = Client::new(scheduler_url.to_string());
99    
100    // Generate a random salt for encryption
101    let salt = rand::thread_rng().gen::<i32>();
102    
103    // Execute the encrypted task synchronously (with polling)
104    let result = client.execute_sync_encrypted(method, key, salt, params, Duration::from_secs(30)).await?;
105    
106    // Extract the actual result value from the response
107    match result.result {
108        Some(value) => Ok(value),
109        None => Err(crate::error::SdkError::Task("No result returned".to_string())),
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116    use serde_json::json;
117
118    // Note: These tests would require a running scheduler server
119    // They are included for documentation purposes
120    
121    #[tokio::test]
122    #[ignore] // Ignore by default since it requires a running server
123    async fn test_call() {
124        let result = call(
125            "http://localhost:8080",
126            "add",
127            json!({"a": 1, "b": 2})
128        ).await;
129        
130        // This would pass if the server is running and has an "add" method
131        assert!(result.is_ok());
132    }
133    
134    #[tokio::test]
135    #[ignore] // Ignore by default since it requires a running server
136    async fn test_call_encrypted() {
137        let result = call_encrypted(
138            "http://localhost:8080",
139            "add",
140            json!({"a": 1, "b": 2}),
141            "test-key"
142        ).await;
143        
144        // This would pass if the server is running and has an "add" method
145        assert!(result.is_ok());
146    }
147}