canic_core/api/
rpc.rs

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