Trait ic_utils::call::AsyncCall

source ·
pub trait AsyncCall<Out>where
    Out: for<'de> ArgumentDecoder<'de> + Send,{
    // Required methods
    fn call<'async_trait>(
        self
    ) -> Pin<Box<dyn Future<Output = Result<RequestId, AgentError>> + Send + 'async_trait>>
       where Self: 'async_trait;
    fn call_and_wait<'async_trait>(
        self
    ) -> Pin<Box<dyn Future<Output = Result<Out, AgentError>> + Send + 'async_trait>>
       where Self: 'async_trait;

    // Provided methods
    fn and_then<Out2, R, AndThen>(
        self,
        and_then: AndThen
    ) -> AndThenAsyncCaller<Out, Out2, Self, R, AndThen>
       where Self: Sized + Send,
             Out2: for<'de> ArgumentDecoder<'de> + Send,
             R: Future<Output = Result<Out2, AgentError>> + Send,
             AndThen: Send + Fn(Out) -> R { ... }
    fn map<Out2, Map>(self, map: Map) -> MappedAsyncCaller<Out, Out2, Self, Map>
       where Self: Sized + Send,
             Out2: for<'de> ArgumentDecoder<'de> + Send,
             Map: Send + Fn(Out) -> Out2 { ... }
}
Expand description

A type that implements asynchronous calls (ie. ‘update’ calls). This can call synchronous and return a RequestId, or it can wait for the result by polling the agent, and return a type.

The return type must be a tuple type that represents all the values the return call should be returning.

Required Methods§

source

fn call<'async_trait>( self ) -> Pin<Box<dyn Future<Output = Result<RequestId, AgentError>> + Send + 'async_trait>>where Self: 'async_trait,

Execute the call, but returns the RequestId. Waiting on the request Id must be managed by the caller using the Agent directly.

Since the return type is encoded in the trait itself, this can lead to types that are not compatible to Out when getting the result from the Request Id. For example, you might hold a AsyncCall<u8>, use call() and poll for the result, and try to deserialize it as a String. This would be caught by Rust type system, but in this case it will be checked at runtime (as Request Id does not have a type associated with it).

source

fn call_and_wait<'async_trait>( self ) -> Pin<Box<dyn Future<Output = Result<Out, AgentError>> + Send + 'async_trait>>where Self: 'async_trait,

Execute the call, and wait for an answer using an exponential-backoff strategy. The return type is encoded in the trait.

Provided Methods§

source

fn and_then<Out2, R, AndThen>( self, and_then: AndThen ) -> AndThenAsyncCaller<Out, Out2, Self, R, AndThen>where Self: Sized + Send, Out2: for<'de> ArgumentDecoder<'de> + Send, R: Future<Output = Result<Out2, AgentError>> + Send, AndThen: Send + Fn(Out) -> R,

Apply a transformation function after the call has been successful. The transformation is applied with the result.

use ic_agent::Agent;
use ic_utils::{Canister, interfaces, call::AsyncCall};
use candid::{Encode, Decode, CandidType, Principal};

async fn create_a_canister() -> Result<Principal, Box<dyn std::error::Error>> {
  let agent = Agent::builder()
    .with_url(URL)
    .with_identity(create_identity())
    .build()?;
  agent.fetch_root_key().await?;
  let management_canister = interfaces::ManagementCanister::create(&agent);
  let management_canister = &management_canister; // needed for `async move`

  // Create a canister, then call the management canister to install a base canister
  // WASM. This is to show how this API would be used, but is probably not a good
  // real use case.
  let (canister_id,) = management_canister
    .create_canister()
    .as_provisional_create_with_amount(None)
    .with_effective_canister_id(effective_id)
    .and_then(|(canister_id,)| {
        let call = management_canister
            .install_code(&canister_id, canister_wasm)
            .build()
            .unwrap();
        async move {
            call.call_and_wait().await?;
            Ok((canister_id,))
        }
    })
    .call_and_wait()
    .await?;

  Ok(canister_id)
}

let canister_id = create_a_canister().await.unwrap();
eprintln!("{}", canister_id);
source

fn map<Out2, Map>(self, map: Map) -> MappedAsyncCaller<Out, Out2, Self, Map>where Self: Sized + Send, Out2: for<'de> ArgumentDecoder<'de> + Send, Map: Send + Fn(Out) -> Out2,

Apply a transformation function after the call has been successful. Equivalent to .and_then(|x| async { map(x) }).

Implementors§

source§

impl<'agent, 'canister: 'agent> AsyncCall<(Principal,)> for CreateCanisterBuilder<'agent, 'canister>

source§

impl<'agent, 'canister: 'agent> AsyncCall<()> for InstallCodeBuilder<'agent, 'canister>

source§

impl<'agent, 'canister: 'agent> AsyncCall<()> for UpdateCanisterBuilder<'agent, 'canister>

source§

impl<'agent, 'canister: 'agent, Out> AsyncCall<Out> for CallForwarder<'agent, 'canister, Out>where Out: for<'de> ArgumentDecoder<'de> + Send + Sync,

source§

impl<'agent, Out> AsyncCall<Out> for AsyncCaller<'agent, Out>where Out: for<'de> ArgumentDecoder<'de> + Send,

source§

impl<Out, Out2, Inner, Map> AsyncCall<Out2> for MappedAsyncCaller<Out, Out2, Inner, Map>where Out: for<'de> ArgumentDecoder<'de> + Send, Out2: for<'de> ArgumentDecoder<'de> + Send, Inner: AsyncCall<Out> + Send, Map: Send + Fn(Out) -> Out2,

source§

impl<Out, Out2, Inner, R, AndThen> AsyncCall<Out2> for AndThenAsyncCaller<Out, Out2, Inner, R, AndThen>where Out: for<'de> ArgumentDecoder<'de> + Send, Out2: for<'de> ArgumentDecoder<'de> + Send, Inner: AsyncCall<Out> + Send, R: Future<Output = Result<Out2, AgentError>> + Send, AndThen: Send + Fn(Out) -> R,