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§
sourcefn call<'async_trait>(
self
) -> Pin<Box<dyn Future<Output = Result<RequestId, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
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 [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).
sourcefn call_and_wait<'async_trait>(
self
) -> Pin<Box<dyn Future<Output = Result<Out, 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,
Execute the call, and wait for an answer using a [Waiter] strategy. The return type is encoded in the trait.
Provided Methods§
sourcefn 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.
# // This test is ignored because it requires an ic to be running. We run these
# // in the ic-ref workflow.
use ic_agent::{Agent, Principal};
use ic_utils::{Canister, interfaces};
use candid::{Encode, Decode, CandidType};
# let canister_wasm = b"\0asm\x01\0\0\0";
# fn create_identity() -> impl ic_agent::Identity {
# let rng = ring::rand::SystemRandom::new();
# let key_pair = ring::signature::Ed25519KeyPair::generate_pkcs8(&rng)
# .expect("Could not generate a key pair.");
#
# ic_agent::BasicIdentity::from_key_pair(
# ring::signature::Ed25519KeyPair::from_pkcs8(key_pair.as_ref())
# .expect("Could not read the key pair."),
# )
# }
#
# const URL: &'static str = concat!("http://localhost:", env!("IC_REF_PORT"));
#
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()?;
// 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()
.await
})
.call_and_wait()
.await?;
let result = Decode!(response.as_slice(), CreateCanisterResult)?;
let canister_id: Principal = Principal::from_text(&result.canister_id.to_text())?;
Ok(canister_id)
}
# let mut runtime = tokio::runtime::Runtime::new().unwrap();
# runtime.block_on(async {
let canister_id = create_a_canister().await.unwrap();
eprintln!("{}", canister_id);
# });
sourcefn 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,
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) }).