Skip to main content

canic_core/api/
rpc.rs

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