This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
84 lines (74 loc) · 2.55 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use hdk::prelude::{
holochain_serial,
holochain_zome_types::zome::{FunctionName, ZomeName},
ExternIO, SerializedBytes,
};
use holochain_conductor_api::ZomeCall;
use holochain_conductor_client::AppWebsocket;
use serde::*;
const WS_URL: &str = "ws://localhost:8888";
const H_APP_ID: &str = "test-app";
const ZOME_NAME: &str = "numbers";
const FN_NAME: &str = "add_ten";
// custom data we want to pass the hApp
#[derive(Serialize, Deserialize, SerializedBytes, Debug, Clone)]
struct ZomeInput {
number: i32,
}
// custom data we want back from the hApp
#[derive(Serialize, Deserialize, SerializedBytes, Debug, Clone)]
pub struct ZomeOutput {
other_number: i32,
}
pub async fn call() -> Result<ZomeOutput, String> {
// connect to a running holochain conductor
// (there needs to be a running holochain conductor!)
let mut app_ws = AppWebsocket::connect(WS_URL.to_string())
.await
.or(Err(String::from("Could not connect to conductor")))?;
let app_info_result = app_ws
.app_info(H_APP_ID.to_string())
.await
.or(Err(String::from("Could not get app info")))?;
let app_info = match app_info_result {
None => return Err(String::from("no app info found")),
Some(app_info) => app_info,
};
let cell_id = app_info.cell_data[0].as_id().to_owned();
let payload = ZomeInput { number: 10 };
// you must encode the payload to standardize it
// for passing to your hApp
let encoded_payload = ExternIO::encode(payload.clone())
.or(Err(String::from("serialization of payload failed")))?;
// define the context of the request
let api_request = ZomeCall {
cell_id: cell_id.clone(),
zome_name: ZomeName::from(String::from(ZOME_NAME)),
fn_name: FunctionName::from(String::from(FN_NAME)),
payload: encoded_payload,
cap_secret: None,
provenance: cell_id.clone().agent_pubkey().to_owned(),
};
// make the request
let encoded_api_response = app_ws
.zome_call(api_request)
.await
.map_err(|e| {
println!("{:?}", e);
e
})
.or(Err(String::from("zome call failed")))?;
// you must decode the payload from
// the standarized format its returned as
let result: ZomeOutput = encoded_api_response
.decode()
.or(Err(String::from("deserialization failed")))?;
Ok(result)
}
#[tokio::main]
async fn main() {
match call().await {
Ok(s) => println!("Result of the call: {:#?}", s),
Err(e) => println!("Got an error: {:?}", e),
};
}