pub enum Client {
Ws(WsClient),
Http(HttpClient),
}Expand description
Unified client that can be either WebSocket or HTTP
Variants§
Ws(WsClient)
Http(HttpClient)
Implementations§
Source§impl Client
impl Client
Sourcepub fn ws<A: ToSocketAddrs>(addr: A) -> Result<WsClient, Box<dyn Error>>
pub fn ws<A: ToSocketAddrs>(addr: A) -> Result<WsClient, Box<dyn Error>>
Create a WebSocket client
Examples found in repository?
examples/blocking_ws.rs (line 9)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7 // Connect to ReifyDB server - various ways to specify address:
8 // Using tuple (address, port):
9 let client = Client::ws(("127.0.0.1", 8090))?;
10
11 // Create a blocking session with authentication
12 let mut session = client.blocking_session(Some("mysecrettoken".to_string()))?;
13
14 // Execute a command to create a table
15 let command_result =
16 session.command("CREATE NAMESPACE test; CREATE TABLE test.users { id: INT4, name: UTF8 }", None)?;
17 println!("Command executed: {} frames returned", command_result.frames.len());
18
19 // Execute a query
20 let query_result = session.query("MAP { x: 42, y: 'hello' }", None)?;
21
22 println!("Query executed: {} frames returned", query_result.frames.len());
23
24 // Print first frame if available
25 if let Some(frame) = query_result.frames.first() {
26 println!("First frame:\n{}", frame);
27 }
28
29 Ok(())
30}More examples
examples/callback_ws.rs (line 10)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9 // Connect to ReifyDB server
10 let client = Client::ws(("127.0.0.1", 8090))?;
11
12 // Create a callback session with authentication
13 let session = client.callback_session(Some("mysecrettoken".to_string()))?;
14
15 // Execute a command to create a table
16 let command_id = session.command(
17 "CREATE NAMESPACE test; CREATE TABLE test.users { id: INT4, name: UTF8 }",
18 None,
19 |result| match result {
20 Ok(data) => println!("Command executed: {} frames returned", data.frames.len()),
21 Err(e) => println!("Command failed: {}", e),
22 },
23 )?;
24 println!("Command sent with ID: {}", command_id);
25
26 // Execute a query
27 let query_id = session.query("MAP { x: 42, y: 'hello' }", None, |result| {
28 match result {
29 Ok(data) => {
30 println!("Query executed: {} frames returned", data.frames.len());
31 // Print first frame if available
32 if let Some(frame) = data.frames.first() {
33 println!("First frame:\n{}", frame);
34 }
35 }
36 Err(e) => println!("Query failed: {}", e),
37 }
38 })?;
39 println!("Query sent with ID: {}", query_id);
40
41 // Wait for callbacks to complete
42 thread::sleep(Duration::from_millis(500));
43
44 Ok(())
45}examples/channel_ws.rs (line 10)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9 // Connect to ReifyDB server
10 let client = Client::ws(("127.0.0.1", 8090))?;
11
12 // Create a channel session with authentication
13 let (session, receiver) = client.channel_session(Some("mysecrettoken".to_string()))?;
14
15 // Consume authentication response
16 if let Ok(msg) = receiver.recv_timeout(Duration::from_millis(100)) {
17 if let Ok(ChannelResponse::Auth {
18 request_id,
19 }) = msg.response
20 {
21 println!("Authenticated with ID: {}", request_id);
22 }
23 }
24
25 // Execute a command to create a table
26 let command_id =
27 session.command("CREATE NAMESPACE test; CREATE TABLE test.users { id: INT4, name: UTF8 }", None)?;
28 println!("Command sent with ID: {}", command_id);
29
30 // Execute a query
31 let query_id = session.query("MAP { x: 42, y: 'hello' }", None)?;
32 println!("Query sent with ID: {}", query_id);
33
34 // Receive responses
35 let mut received = 0;
36 while received < 2 {
37 match receiver.recv_timeout(Duration::from_secs(1)) {
38 Ok(msg) => {
39 match msg.response {
40 Ok(ChannelResponse::Command {
41 request_id,
42 result,
43 }) => {
44 println!(
45 "Command {} executed: {} frames returned",
46 request_id,
47 result.frames.len()
48 );
49 received += 1;
50 }
51 Ok(ChannelResponse::Query {
52 request_id,
53 result,
54 }) => {
55 println!(
56 "Query {} executed: {} frames returned",
57 request_id,
58 result.frames.len()
59 );
60 // Print first frame if
61 // available
62 if let Some(frame) = result.frames.first() {
63 println!("First frame:\n{}", frame);
64 }
65 received += 1;
66 }
67 Ok(ChannelResponse::Auth {
68 ..
69 }) => {
70 // Already handled above
71 }
72 Err(e) => {
73 println!("Request {} failed: {}", msg.request_id, e);
74 received += 1;
75 }
76 }
77 }
78 Err(_) => {
79 println!("Timeout waiting for responses");
80 break;
81 }
82 }
83 }
84
85 Ok(())
86}examples/test_remote_sort.rs (line 8)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5 println!("Connecting to ws://192.168.100.52:8090/\n");
6
7 // Connect to the remote server
8 let client = Client::ws(("192.168.100.52", 8090))?;
9
10 // Try with auth token from environment or use "root"
11 let token = std::env::var("REIFYDB_TOKEN").unwrap_or_else(|_| "root".to_string());
12 println!(
13 "Using auth token: {}\n",
14 if token == "root" {
15 "root (default)"
16 } else {
17 &token
18 }
19 );
20
21 // Create a blocking session with auth token
22 let mut session = client.blocking_session(Some(token))?;
23
24 println!("Connected successfully!\n");
25
26 // Test ASC
27 println!("=== TEST 1: ASC (should show smallest first) ===");
28 println!("Query:");
29 println!("from system.table_storage_stats");
30 println!("sort total_bytes asc\n");
31
32 let query_asc = "from system.table_storage_stats\nsort total_bytes asc";
33 let result_asc = session.query(query_asc, None)?;
34
35 if let Some(frame) = result_asc.frames.first() {
36 if let Some(total_bytes_col) = frame.columns.iter().find(|c| c.name == "total_bytes") {
37 let mut values: Vec<u64> = Vec::new();
38 for i in 0..total_bytes_col.data.len() {
39 let val = total_bytes_col.data.as_string(i).parse::<u64>().unwrap_or(0);
40 values.push(val);
41 }
42 println!("ASC Results: {:?}", values);
43 println!("First value (should be smallest): {}", values[0]);
44 println!("Last value (should be largest): {}\n", values[values.len() - 1]);
45 }
46 }
47
48 // Test DESC
49 println!("=== TEST 2: DESC (should show largest first) ===");
50 println!("Query:");
51 println!("from system.table_storage_stats");
52 println!("sort total_bytes desc\n");
53
54 let query = "from system.table_storage_stats\nsort total_bytes desc";
55 let result = session.query(query, None)?;
56
57 println!("Query executed: {} frames returned\n", result.frames.len());
58
59 // Print the results
60 if let Some(frame) = result.frames.first() {
61 println!("Frame output:");
62 println!("{}\n", frame);
63
64 // Also analyze the data
65 if let Some(total_bytes_col) = frame.columns.iter().find(|c| c.name == "total_bytes") {
66 println!("=== Analyzing total_bytes column ===");
67 let mut values: Vec<u64> = Vec::new();
68 for i in 0..total_bytes_col.data.len() {
69 let val = total_bytes_col.data.as_string(i).parse::<u64>().unwrap_or(0);
70 values.push(val);
71 println!("Row {}: {} bytes", i, val);
72 }
73
74 println!("\nValues in order: {:?}", values);
75
76 // Check if sorted correctly (DESC = largest first)
77 let mut is_desc_sorted = true;
78 for i in 1..values.len() {
79 if values[i - 1] < values[i] {
80 is_desc_sorted = false;
81 println!(
82 "\n⚠️ SORTING ERROR at position {}: {} < {}",
83 i,
84 values[i - 1],
85 values[i]
86 );
87 }
88 }
89
90 if is_desc_sorted {
91 println!("\n✅ Correctly sorted in DESCENDING order (largest first)");
92 } else {
93 println!("\n❌ NOT correctly sorted in descending order!");
94 println!(" Expected: Largest value first, decreasing values");
95 println!(" Got: {:?}", values);
96 }
97 }
98 }
99
100 Ok(())
101}Sourcepub fn ws_from_url(url: &str) -> Result<WsClient, Box<dyn Error>>
pub fn ws_from_url(url: &str) -> Result<WsClient, Box<dyn Error>>
Create a WebSocket client from URL
Sourcepub fn http<A: ToSocketAddrs>(addr: A) -> Result<HttpClient, Box<dyn Error>>
pub fn http<A: ToSocketAddrs>(addr: A) -> Result<HttpClient, Box<dyn Error>>
Create an HTTP client
Examples found in repository?
examples/blocking_http.rs (line 7)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7 let client = Client::http(("127.0.0.1", 8090))?;
8
9 // Create a blocking session with authentication
10 let mut session = client.blocking_session(Some("mysecrettoken".to_string()))?;
11
12 // Execute a command to create a table
13 let command_result =
14 session.command("CREATE NAMESPACE test; CREATE TABLE test.users { id: INT4, name: UTF8 }", None)?;
15 println!("Command executed: {} frames returned", command_result.frames.len());
16
17 // Execute a query
18 let query_result = session.query("MAP { x: 42, y: 'hello' }", None)?;
19
20 println!("Query executed: {} frames returned", query_result.frames.len());
21
22 // Print first frame if available
23 if let Some(frame) = query_result.frames.first() {
24 println!("First frame:\n{}", frame);
25 }
26
27 Ok(())
28}Sourcepub fn http_from_url(url: &str) -> Result<HttpClient, Box<dyn Error>>
pub fn http_from_url(url: &str) -> Result<HttpClient, Box<dyn Error>>
Create an HTTP client from URL
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Client
impl RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more