Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: SyncCall/AsyncCall enhancements #539

Merged
merged 11 commits into from
May 30, 2024
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

* Changed the SyncCall and AsyncCall traits to use an associated type for their output instead of a generic parameter.
* Call builders now generally implement `IntoFuture`, allowing `.call_and_wait().await` to be shortened to `.await`.

## [0.35.0] - 2024-05-10

* Added a limit to the concurrent requests an agent will make at once. This should make server-side ratelimiting much rarer to encounter, even when sending a high volume of requests (for example, a large `ic_utils::ManagementCanister::install` call).
Expand Down
19 changes: 17 additions & 2 deletions ic-agent/src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use std::{
collections::HashMap,
convert::TryFrom,
fmt,
future::Future,
future::{Future, IntoFuture},
pin::Pin,
sync::{Arc, Mutex, RwLock},
task::{Context, Poll},
Expand Down Expand Up @@ -234,7 +234,6 @@ pub enum PollResult {
/// let response = agent.update(&management_canister_id, "provisional_create_canister_with_cycles")
/// .with_effective_canister_id(effective_canister_id)
/// .with_arg(Encode!(&Argument { amount: None })?)
/// .call_and_wait()
/// .await?;
///
/// let result = Decode!(response.as_slice(), CreateCanisterResult)?;
Expand Down Expand Up @@ -1614,6 +1613,14 @@ impl<'agent> QueryBuilder<'agent> {
}
}

impl<'agent> IntoFuture for QueryBuilder<'agent> {
type IntoFuture = AgentFuture<'agent, Vec<u8>>;
type Output = Result<Vec<u8>, AgentError>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(self.call())
}
}

/// An in-flight canister update call. Useful primarily as a `Future`.
pub struct UpdateCall<'agent> {
agent: &'agent Agent,
Expand Down Expand Up @@ -1769,6 +1776,14 @@ impl<'agent> UpdateBuilder<'agent> {
}
}

impl<'agent> IntoFuture for UpdateBuilder<'agent> {
type IntoFuture = AgentFuture<'agent, Vec<u8>>;
type Output = Result<Vec<u8>, AgentError>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(self.call_and_wait())
}
}

#[cfg(all(test, feature = "reqwest", not(target_family = "wasm")))]
mod offline_tests {
use super::*;
Expand Down
1 change: 0 additions & 1 deletion ic-agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
//! let response = agent.update(&management_canister_id, "provisional_create_canister_with_cycles")
//! .with_effective_canister_id(effective_canister_id)
//! .with_arg(Encode!(&Argument { amount: None})?)
//! .call_and_wait()
//! .await?;
//!
//! let result = Decode!(response.as_slice(), CreateCanisterResult)?;
Expand Down
Loading