Skip to content

Commit

Permalink
feat!: SyncCall/AsyncCall enhancements (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspofford-dfinity authored May 30, 2024
1 parent 1a16e17 commit 608a3f4
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 158 deletions.
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

0 comments on commit 608a3f4

Please sign in to comment.