[][src]Function hdk::call

pub fn call<S: Into<String>>(
    zome_name: S,
    cap_name: S,
    fn_name: S,
    fn_args: Value
) -> ZomeApiResult<String>

Call an exposed function from another zome. Arguments for the called function are passed as serde_json::Value. Returns the value that's returned by the given function as a json str.

Examples

In order to utilize call, you must have at least two separate Zomes. Here are two Zome examples, where one performs a call into the other.

This first one, is the one that is called into, with the Zome name summer.

#[macro_use]
extern crate hdk;
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate serde_json;

fn handle_sum(num1: u32, num2: u32) -> serde_json::Value {
    let sum = num1 + num2;
    return json!({"sum": format!("{}",sum)});
}

define_zome! {
    entries: []

    genesis: || {
        Ok(())
    }

    functions: {
        main (Public) {
            sum: {
                inputs: |num1: u32, num2: u32|,
                outputs: |sum: serde_json::Value|,
                handler: handle_sum
            }
        }
    }
}

This second one, is the one that performs the call into the summer Zome.

#[macro_use]
extern crate hdk;
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate serde_json;

use hdk::holochain_core_types::hash::HashString;

fn handle_check_sum(num1: u32, num2: u32) -> serde_json::Value {
    #[derive(Serialize)]
    struct SumInput {
        num1: u32,
        num2: u32,
    };
    let call_input = SumInput {
        num1: num1,
        num2: num2,
    };
    let maybe_result = hdk::call(
        "summer",
        "main",
        "sum",
        serde_json::to_value(call_input).unwrap()
    );
    match maybe_result {
        Ok(result) => serde_json::from_str(&result).unwrap(),
        Err(hdk_error) => hdk_error.to_json(),
    }
}

define_zome! {
    entries: []

    genesis: || {
        Ok(())
    }

    functions: {
        main (Public) {
            check_sum: {
                inputs: |num1: u32, num2: u32|,
                outputs: |sum: serde_json::Value|,
                handler: handle_check_sum
            }
        }
    }
}