rpc/
rpc.rs

1use blockless_sdk::rpc::RpcClient;
2use serde::{Deserialize, Serialize};
3
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    let mut client = RpcClient::new();
6
7    // Example 1: Simple ping
8    println!("=== Example 1: Simple Ping ===");
9    match client.ping() {
10        Ok(response) => println!("Ping response: {}", response),
11        Err(e) => println!("Ping error: {}", e),
12    }
13
14    // Example 2: Echo with different data types
15    println!("\n=== Example 2: Echo Examples ===");
16
17    // Echo string
18    match client.echo("Hello, World!".to_string()) {
19        Ok(response) => println!("Echo string: {}", response),
20        Err(e) => println!("Echo error: {}", e),
21    }
22
23    // Echo number
24    match client.echo(42) {
25        Ok(response) => println!("Echo number: {}", response),
26        Err(e) => println!("Echo error: {}", e),
27    }
28
29    // Echo complex object
30    #[derive(Serialize, Deserialize, Debug)]
31    struct Person {
32        name: String,
33        age: u32,
34    }
35
36    let person = Person {
37        name: "Alice".to_string(),
38        age: 30,
39    };
40
41    match client.echo(person) {
42        Ok(response) => println!("Echo person: {:?}", response),
43        Err(e) => println!("Echo error: {}", e),
44    }
45
46    // Example 3: Get version
47    println!("\n=== Example 3: Get Version ===");
48    match client.version() {
49        Ok(version) => {
50            println!("Version info:");
51            for (key, value) in version {
52                println!("  {}: {}", key, value);
53            }
54        }
55        Err(e) => println!("Version error: {}", e),
56    }
57
58    // Example 4: Generic call with custom types
59    println!("\n=== Example 4: Generic Call ===");
60
61    #[derive(Serialize, Deserialize, Debug)]
62    struct CustomRequest {
63        message: String,
64        count: u32,
65    }
66
67    #[derive(Serialize, Deserialize, Debug)]
68    struct CustomResponse {
69        processed: String,
70        timestamp: u64,
71    }
72
73    let request = CustomRequest {
74        message: "Test message".to_string(),
75        count: 5,
76    };
77
78    // This would fail since "custom.process" doesn't exist in our test implementation
79    match client.call::<CustomRequest, CustomResponse>("custom.process", Some(request)) {
80        Ok(response) => {
81            if let Some(result) = response.result {
82                println!("Custom response: {:?}", result);
83            } else if let Some(error) = response.error {
84                println!("Custom error: {} (code: {})", error.message, error.code);
85            }
86        }
87        Err(e) => println!("Custom call error: {}", e),
88    }
89
90    // Example 5: Error handling
91    println!("\n=== Example 5: Error Handling ===");
92
93    // Try calling a non-existent method
94    match client.call::<(), String>("nonexistent.method", None) {
95        Ok(response) => {
96            if let Some(error) = response.error {
97                println!("Expected error: {} (code: {})", error.message, error.code);
98            }
99        }
100        Err(e) => println!("Call error: {}", e),
101    }
102
103    println!("\nAll examples completed!");
104    Ok(())
105}