reifydb_client/
client.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the MIT, see license.md file
3
4use std::net::ToSocketAddrs;
5
6use crate::{HttpClient, ws::client::WsClient};
7
8/// Unified client that can be either WebSocket or HTTP
9#[derive(Clone)]
10pub enum Client {
11	Ws(WsClient),
12	Http(HttpClient),
13}
14
15impl Client {
16	/// Create a WebSocket client
17	pub fn ws<A: ToSocketAddrs>(addr: A) -> Result<WsClient, Box<dyn std::error::Error>> {
18		WsClient::new(addr)
19	}
20
21	/// Create a WebSocket client from URL
22	pub fn ws_from_url(url: &str) -> Result<WsClient, Box<dyn std::error::Error>> {
23		WsClient::from_url(url)
24	}
25
26	/// Create an HTTP client
27	pub fn http<A: ToSocketAddrs>(addr: A) -> Result<HttpClient, Box<dyn std::error::Error>> {
28		HttpClient::new(addr)
29	}
30
31	/// Create an HTTP client from URL
32	pub fn http_from_url(url: &str) -> Result<HttpClient, Box<dyn std::error::Error>> {
33		HttpClient::from_url(url)
34	}
35
36	/// Close the client connection
37	pub fn close(self) -> Result<(), Box<dyn std::error::Error>> {
38		match self {
39			Client::Ws(ws) => ws.close(),
40			Client::Http(http) => http.close(),
41		}
42	}
43}