Is there a way to "log" query calls?

I’m trying to implement simple http functionality, using the http_request method, in rust.

It works great for query calls, but eventually I’ll need to support mutable requests, and / or I’ll need to log the calls. I can’t seem to find anything that works.

I’ve tried calling an update function from the query, like so:

async fn testz() {
    ic_cdk::println!("IN");

    let call_result: Result<(String,), _> = ic_cdk::api::call::call(
        ic_cdk::export::Principal::from_text("rrkah-fqaaa-aaaaa-aaaaq-cai").unwrap(),
        "greetUpdate",
        ("TEST",),
    )
    .await;

    let v = match call_result {
        Ok(it) => it.0,
        Err(e) => e.1,
    };

    ic_cdk::println!("OUT {}", v);
}

… and call it like so:

ic_cdk::block_on(testz());

If i call this function from an update call, it works. If I call it from a query function, it doesn’t work. Am I doing something wrong, or is it just not something that’s supported right now? Are there any plans of supporting a case like this?

I also tried annotating the http_request function as an update function:

#[update(name = "http_request")]
fn http_request(request: HttpRequest) -> HttpResponse {
    ic_cdk::println!("{:?}", request);

… but then I get the following error when calling the endpoint:

Details: ReplicaError { reject_code: 3, reject_message: IC0302: Canister rrkah-fqaaa-aaaaa-aaaaq-cai has no query method \'http_request\' }

Any ideas?

That is very much in the definition of a query call: a query is faster because it doesn’t have to go through consensus. But the price is that it cannot make any permanent state changes or make other calls that might.

So you can’t have your cake and eat it too. You have to pick between either fast, or persistent.

Right, and that’s part of the question: how do I do that, for a generic http request? For the moment the only implementation I could find is for query calls based on exposing http_request. And that works fine. I guess I’m trying to find anequivalent for an update call.

I made a PR that would allow that: feat: infer update call based on HTTP request method by paulyoung · Pull Request #195 · dfinity/agent-rs · GitHub

I plan to make more changes but was waiting to hear back about the proposal process that was mentioned.

2 Likes