zbus_lib/client/
mod.rs

1use std::thread;
2use std::time::Duration;
3
4// pub use wsocket::{WsClient, Instruct, WsClientHandler};
5use serde::de::DeserializeOwned;
6use serde::Deserialize;
7use serde_json::Value;
8
9pub use component::WsRpcClient;
10
11use crate::{
12    err::ZbusErr,
13    message::{Request, Response},
14};
15use crate::err::ZbusResult;
16
17mod component;
18
19// mod rpc;
20
21// mod wsocket;
22
23pub trait RpcClient {
24    fn invoke(&self, req: Request) -> ZbusResult<Value> {
25        self.deliver(req).and_then(|resp| {
26            if resp.status() == 200 {
27                Ok(resp.body())
28            } else {
29                Err(ZbusErr::err(resp.body().to_string()))
30            }
31        })
32    }
33    // fn invoke<T: for<'de> Deserialize<'de>> (&self, req: Request) -> ZbusResult<T> {
34    //     self.deliver(req).and_then(|resp| {
35    //         if resp.status() == 200 {
36    //             serde_json::from_value(resp.body()).map_err(|e|From::from(e))
37    //         } else {
38    //             Err(ZbusErr::err(resp.body().to_string()))
39    //         }
40    //     })
41    // }
42    fn deliver(&self, req: Request) -> ZbusResult<Response>;
43}
44
45
46pub trait WebsocketClient {
47    fn connect(&self) -> bool;
48    fn reconnect(&mut self) -> bool;
49    fn message<S: Into<String>>(&self, id: S) -> ws::Message;
50    fn is_connected(&self) -> bool;
51    fn close(&self) -> bool;
52    fn send(&self, msg: ws::Message) -> bool;
53}
54
55
56// #[test]
57// fn client() -> OkResult {
58//     let rpc_client = WsRpcClient::connect("ws://127.0.0.1:1555");
59//     for _ in 0..20 {
60//         let ws_conn = rpc_client.handler();
61//         thread::spawn(move || {
62//             let req = Request::builder()
63//                 .url("/test")
64//                 .body(Value::String("iddasfa".into()))
65//                 .id("dsafasafda")
66//                 .build();
67//             ws_conn.deliver(req);
68//         });
69//     }
70//     thread::sleep(Duration::from_secs(20));
71//     Ok(())
72// }