Skip to main content

canic_core/api/rpc/
mod.rs

1mod capability;
2
3use crate::{
4    cdk::{candid::CandidType, types::Principal},
5    dto::{
6        capability::{RootCapabilityEnvelopeV1, RootCapabilityResponseV1},
7        error::Error,
8        rpc::{CreateCanisterParent, CreateCanisterResponse, UpgradeCanisterResponse},
9    },
10    ids::CanisterRole,
11    workflow::rpc::request::RpcRequestWorkflow,
12};
13
14///
15/// RpcApi
16///
17/// Public, user-callable wrappers for Canic's internal RPC workflows.
18///
19/// These functions:
20/// - form part of the public API surface
21/// - are safe to call from downstream canister `lib.rs` code
22/// - return [`Error`] suitable for IC boundaries
23///
24/// Internally, they delegate to workflow-level RPC implementations,
25/// preserving the layering:
26///
27///   user canister -> api -> workflow -> ops -> infra
28///
29/// Workflow returns internal [`InternalError`]; conversion to [`Error`]
30/// happens exclusively at this API boundary.
31///
32
33pub struct RpcApi;
34
35impl RpcApi {
36    pub async fn create_canister_request<A>(
37        canister_role: &CanisterRole,
38        parent: CreateCanisterParent,
39        extra: Option<A>,
40    ) -> Result<CreateCanisterResponse, Error>
41    where
42        A: CandidType + Send + Sync,
43    {
44        RpcRequestWorkflow::create_canister_request(canister_role, parent, extra)
45            .await
46            .map_err(Error::from)
47    }
48
49    pub async fn upgrade_canister_request(
50        canister_pid: Principal,
51    ) -> Result<UpgradeCanisterResponse, Error> {
52        RpcRequestWorkflow::upgrade_canister_request(canister_pid)
53            .await
54            .map_err(Error::from)
55    }
56
57    pub async fn response_capability_v1(
58        envelope: RootCapabilityEnvelopeV1,
59    ) -> Result<RootCapabilityResponseV1, Error> {
60        capability::response_capability_v1(envelope).await
61    }
62}