How to call IC from the rust agent package

Hey, I would like to call the IC from a flutter app.
So far, I managed to run rust code and import the ic_agent module. I tried to make a query call but didn’t receive any answer. This is how I tried to make the call:

pub async fn query_call(canister_id: &str, method_name: &str) -> String {
    // create random identity for agent
    let rng = ring::rand::SystemRandom::new();
    let key_pair = ring::signature::Ed25519KeyPair::generate_pkcs8(&rng)
        .expect("Could not generate a key pair.");
    let identity = BasicIdentity::from_key_pair(
ring::signature::Ed25519KeyPair::from_pkcs8(key_pair.as_ref())
            .expect("Could not read the key pair."),
    );

    // initialize agent and canister_id
    let url = format!("/api/v2/canister/{}/query", canister_id);
    let agent = ic_agent::Agent::builder()
        .with_identity(identity)
        .with_url(url)
        .build()
        .unwrap();
    let canister_id_principal = Principal::from_text(canister_id).unwrap();

    // use QueryBuilder to make query call
    let query_builder = QueryBuilder::new(&agent, canister_id_principal, String::from(method_name));
    let response = query_builder.call().await;
    match response {
        Ok(reply) => return String::from_utf8_lossy(&reply).to_string(),
        Err(AgentError::ReplicaError {
        reject_code,
        reject_message,
        }) => return format!("Error {} occured with message: {}", reject_code, reject_message),
        Err(_) => return "Error occured other than ReplicaError!".to_string(),
    }
}

What did I do wrong? Is QueryBuilder the right object for this?

1 Like

The URL just need to be root. Don’t use the fully qualified path. Rest seems okay.

1 Like

Ok cool, thanks! I’ll try it when I get back on Tuesday :+1:t2:

It still doesn’t return anything…
The root url would look something like this right?

let url = format!("https://{}.ic0", canister_id);

Or should I add the canister id somewhere else?

Try with ic0.app for contacting sodium. But yes essentially.

Alright, switching to tokio 1.2.0 instead of 0.2.0 did the trick :+1:t2:

With the ic0.app ending I can reach the replica, but it returns a 404 error.
Any idea why this happens?
The canister and function work when called by the dfx command line tool.

This might be the reason