holochain_client_core/lib.rs
1//!
2//! Implements ConductorClient, a struct that implement RpcChannel and can therefore be used by the
3//! parity jsonrpc-core-client to produce a client instance that can make calls over any transport.
4//!
5//! WARNING! - A limitation of the client macro in jsonrpc-core-client is preventing this from working properly
6//! The expanded and fixed macro is currently in the lib.rs. Once the problem is fixed this will become the lib and
7//! the macro exansion can work as intended.
8//!
9//! It also exports a connect_ws function which is a helper to construct websocket connection
10//!
11//! Basic Example
12//! ```
13//! use holochain_client_core::{
14//! connect_ws, Future,
15//! };
16//! use url::Url;
17//!
18//! fn main() {
19//! let client_url = Url::parse("ws://localhost:1234").unwrap();
20//! let client = connect_ws(&client_url).wait().unwrap(); // connect returns a future
21//!
22//! let result = client.zome_call( // calling a function also returns a future
23//! "instance_id".into(),
24//! "zome".into(),
25//! "fn_name".into(),
26//! serde_json::Value::Null,
27//! ).wait().unwrap();
28//!
29//! println!("{}", result)
30//! }
31//! ```
32//!
33//! Example using different transport (http)
34//!
35//! ```
36//! use holochain_client_core::{
37//! connect_ws, Future, ConductorClient,
38//! }
39//! use jsonrpc_core_client::transports::http;
40//! use url::Url;
41//!
42//! fn main() {
43//! let client_url = Url::parse("http://localhost:1234").unwrap();
44//! let client = http::connect<ConductorClient>(&client_url).wait().unwrap(); // connect returns a future
45//!
46//! let result = client.zome_call( // calling a function also returns a future
47//! "instance_id".into(),
48//! "zome".into(),
49//! "fn_name".into(),
50//! serde_json::Value::Null,
51//! ).wait().unwrap();
52//!
53//! println!("{}", result)
54//! }
55//! ```
56//!
57use std::prelude::v1::*;
58pub use jsonrpc_core::futures::future::Future;
59use jsonrpc_core_client::{transports::ws::connect, RpcError};
60
61use url::Url;
62pub fn connect_ws(url: &Url) -> impl Future<Item = ConductorClient, Error = RpcError> {
63 connect::<ConductorClient>(url)
64}
65mod rpc_impl_conductor_rpc {
66 use super::*;
67 use jsonrpc_core as _jsonrpc_core;
68 /// The generated client module.
69 pub mod gen_client {
70 use super::*;
71
72 use _jsonrpc_core::serde_json::{self};
73 use _jsonrpc_core::{
74 Params,
75 };
76 use _jsonrpc_core_client::{
77 RpcChannel, RpcError, RawClient,
78 };
79 use jsonrpc_core_client as _jsonrpc_core_client;
80 /// The Client.
81 pub struct Client {
82 inner: RawClient,
83 }
84 #[automatically_derived]
85 #[allow(unused_qualifications)]
86 impl ::core::clone::Clone for Client {
87 #[inline]
88 fn clone(&self) -> Client {
89 match *self {
90 Client {
91 inner: ref __self_0_0,
92 } => Client {
93 inner: ::core::clone::Clone::clone(&(*__self_0_0)),
94 },
95 }
96 }
97 }
98 impl Client {
99 /// Creates a new `Client`.
100 pub fn new(sender: RpcChannel) -> Self {
101 Client {
102 inner: sender.into(),
103 }
104 }
105 pub fn call(
106 &self,
107 instance_id: String,
108 zome: String,
109 function: String,
110 args: serde_json::Value,
111 ) -> impl Future<Item = serde_json::Value, Error = RpcError> {
112 let mut map = serde_json::Map::new();
113
114 map.insert("instance_id".into(), instance_id.into());
115 map.insert("zome".into(), zome.into());
116 map.insert("function".into(), function.into());
117 map.insert("args".into(), args.into());
118
119 let args_map = Params::Map(map);
120 self.inner.call_method("call", args_map.into())
121 }
122 }
123 impl From<RpcChannel> for Client {
124 fn from(channel: RpcChannel) -> Self {
125 Client::new(channel.into())
126 }
127 }
128 }
129}
130pub use self::rpc_impl_conductor_rpc::gen_client;
131/// A ConductorClient is a struct that implement RpcChannel and can therefore be used by the
132/// parity jsonrpc-core-client to produce a client instance that can make calls
133pub use gen_client::Client as ConductorClient;