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}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