canic_core/api/ic/
mod.rs

1pub mod call;
2pub mod http;
3pub mod ledger;
4pub mod mgmt;
5pub mod network;
6pub mod signature;
7
8// fine to use externally
9pub use crate::ops::ic::IcOps;
10
11use crate::{api::prelude::*, cdk::candid::CandidType, ops::ic::call::CallOps};
12use serde::de::DeserializeOwned;
13
14///
15/// Call
16///
17/// Public IC call façade.
18///
19
20pub struct Call;
21
22impl Call {
23    #[must_use]
24    pub fn bounded_wait(canister_id: impl Into<Principal>, method: &str) -> CallBuilder {
25        CallBuilder {
26            inner: CallOps::bounded_wait(canister_id, method),
27        }
28    }
29
30    #[must_use]
31    pub fn unbounded_wait(canister_id: impl Into<Principal>, method: &str) -> CallBuilder {
32        CallBuilder {
33            inner: CallOps::unbounded_wait(canister_id, method),
34        }
35    }
36}
37
38///
39/// CallBuilder (api)
40///
41
42pub struct CallBuilder {
43    inner: crate::ops::ic::call::CallBuilder,
44}
45
46impl CallBuilder {
47    pub fn try_with_arg<A: CandidType>(self, arg: A) -> Result<Self, PublicError> {
48        let inner = self.inner.try_with_arg(arg)?;
49
50        Ok(Self { inner })
51    }
52
53    #[must_use]
54    pub fn with_cycles(self, cycles: u128) -> Self {
55        Self {
56            inner: self.inner.with_cycles(cycles),
57        }
58    }
59
60    pub async fn execute(self) -> Result<CallResult, PublicError> {
61        let inner = self.inner.execute().await?;
62
63        Ok(CallResult { inner })
64    }
65}
66
67///
68/// CallResult (api)
69///
70/// Public, stable result wrapper.
71///
72
73pub struct CallResult {
74    inner: crate::ops::ic::call::CallResult,
75}
76
77impl CallResult {
78    /// Decode the candid response.
79    pub fn candid<R>(&self) -> Result<R, PublicError>
80    where
81        R: CandidType + DeserializeOwned,
82    {
83        self.inner.candid().map_err(PublicError::from)
84    }
85}