Trait ic_utils::call::AsyncCall [−][src]
pub trait AsyncCall<Out> where
Out: for<'de> ArgumentDecoder<'de> + Send, {
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, W>(
self,
waiter: W
) -> Pin<Box<dyn Future<Output = Result<Out, AgentError>> + Send + 'async_trait>>
where
W: Waiter,
W: 'async_trait,
Self: 'async_trait;
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
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 [O] when getting the result from the Request Id.
For example, you might hold a AsyncCallcall()
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).
fn call_and_wait<'async_trait, W>(
self,
waiter: W
) -> Pin<Box<dyn Future<Output = Result<Out, AgentError>> + Send + 'async_trait>> where
W: Waiter,
W: 'async_trait,
Self: 'async_trait,
fn call_and_wait<'async_trait, W>(
self,
waiter: W
) -> Pin<Box<dyn Future<Output = Result<Out, AgentError>> + Send + 'async_trait>> where
W: Waiter,
W: 'async_trait,
Self: 'async_trait,
Execute the call, and wait for an answer using a Waiter strategy. The return type is encoded in the 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 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, Principal};
use ic_utils::{Canister, interfaces};
use candid::{Encode, Decode, CandidType};
async fn create_a_canister() -> Result<Principal, Box<dyn std::error::Error>> {
let agent = Agent::builder()
.with_url(URL)
.with_identity(create_identity())
.build()?;
let management_canister = Canister::builder()
.with_agent(&agent)
.with_canister_id("aaaaa-aa")
.with_interface(interfaces::ManagementCanister)
.build()?;
let waiter = garcon::Delay::builder()
.throttle(std::time::Duration::from_millis(500))
.timeout(std::time::Duration::from_secs(60 * 5))
.build();
// 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()
.and_then(|(canister_id,)| async {
management_canister
.install_code(&canister_id, canister_wasm)
.build()
.call_and_wait(waiter)
.await
})
.call_and_wait(waiter.clone())
.await?;
let result = Decode!(response.as_slice(), CreateCanisterResult)?;
let canister_id: Principal = Principal::from_text(&result.canister_id.to_text())?;
Ok(canister_id)
}
let canister_id = create_a_canister().await.unwrap();
eprintln!("{}", canister_id);