canic_core/api/
rpc.rs

1use crate::{
2    cdk::{candid::CandidType, types::Principal},
3    dto::{
4        error::Error,
5        rpc::{
6            CreateCanisterParent, CreateCanisterResponse, Request, Response,
7            UpgradeCanisterResponse,
8        },
9    },
10    ids::CanisterRole,
11    workflow::rpc::request::{RpcRequestWorkflow, handler::RootResponseWorkflow},
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(request: Request) -> Result<Response, Error> {
58        RootResponseWorkflow::response(request)
59            .await
60            .map_err(Error::from)
61    }
62}