Skip to main content

bity_ic_storage_canister_c2c/
lib.rs

1use bity_ic_storage_canister_api::cancel_upload;
2use bity_ic_storage_canister_api::finalize_upload;
3use bity_ic_storage_canister_api::get_storage_size;
4use bity_ic_storage_canister_api::get_stored_files_size_bytes;
5use bity_ic_storage_canister_api::init_reupload;
6use bity_ic_storage_canister_api::init_upload;
7use bity_ic_storage_canister_api::remove_file;
8use bity_ic_storage_canister_api::store_chunk;
9
10pub async fn get_storage_size(
11    canister_id: candid::Principal,
12    args: get_storage_size::Args,
13) -> Result<get_storage_size::Response, String> {
14    let response = ic_cdk::call::Call::unbounded_wait(canister_id, "get_storage_size")
15        .with_arg(args)
16        .await
17        .map_err(|e| format!("Call failed: {:?}", e))?;
18
19    response
20        .candid::<get_storage_size::Response>()
21        .map_err(|e| format!("Failed to decode response: {:?}", e))
22}
23
24pub async fn get_stored_files_size_bytes(
25    canister_id: candid::Principal,
26    args: get_stored_files_size_bytes::Args,
27) -> Result<get_stored_files_size_bytes::Response, String> {
28    let response = ic_cdk::call::Call::unbounded_wait(canister_id, "get_stored_files_size_bytes")
29        .with_arg(args)
30        .await
31        .map_err(|e| format!("Call failed: {:?}", e))?;
32
33    response
34        .candid::<get_stored_files_size_bytes::Response>()
35        .map_err(|e| format!("Failed to decode response: {:?}", e))
36}
37
38pub async fn init_upload(
39    canister_id: candid::Principal,
40    args: init_upload::Args,
41) -> Result<init_upload::Response, String> {
42    let response = ic_cdk::call::Call::unbounded_wait(canister_id, "init_upload")
43        .with_arg(args)
44        .await
45        .map_err(|e| format!("Call failed: {:?}", e))?;
46
47    response
48        .candid::<init_upload::Response>()
49        .map_err(|e| format!("Failed to decode response: {:?}", e))
50}
51
52pub async fn init_reupload(
53    canister_id: candid::Principal,
54    args: init_reupload::Args,
55) -> Result<init_reupload::Response, String> {
56    let response = ic_cdk::call::Call::unbounded_wait(canister_id, "init_reupload")
57        .with_arg(args)
58        .await
59        .map_err(|e| format!("Call failed: {:?}", e))?;
60
61    response
62        .candid::<init_reupload::Response>()
63        .map_err(|e| format!("Failed to decode response: {:?}", e))
64}
65
66pub async fn store_chunk(
67    canister_id: candid::Principal,
68    args: store_chunk::Args,
69) -> Result<store_chunk::Response, String> {
70    let response = ic_cdk::call::Call::unbounded_wait(canister_id, "store_chunk")
71        .with_arg(args)
72        .await
73        .map_err(|e| format!("Call failed: {:?}", e))?;
74
75    response
76        .candid::<store_chunk::Response>()
77        .map_err(|e| format!("Failed to decode response: {:?}", e))
78}
79
80pub async fn finalize_upload(
81    canister_id: candid::Principal,
82    args: finalize_upload::Args,
83) -> Result<finalize_upload::Response, String> {
84    let response = ic_cdk::call::Call::unbounded_wait(canister_id, "finalize_upload")
85        .with_arg(args)
86        .await
87        .map_err(|e| format!("Call failed: {:?}", e))?;
88
89    response
90        .candid::<finalize_upload::Response>()
91        .map_err(|e| format!("Failed to decode response: {:?}", e))
92}
93
94pub async fn cancel_upload(
95    canister_id: candid::Principal,
96    args: cancel_upload::Args,
97) -> Result<cancel_upload::Response, String> {
98    let response = ic_cdk::call::Call::unbounded_wait(canister_id, "cancel_upload")
99        .with_arg(args)
100        .await
101        .map_err(|e| format!("Call failed: {:?}", e))?;
102
103    response
104        .candid::<cancel_upload::Response>()
105        .map_err(|e| format!("Failed to decode response: {:?}", e))
106}
107
108pub async fn remove_file(
109    canister_id: candid::Principal,
110    args: remove_file::Args,
111) -> Result<remove_file::Response, String> {
112    let response = ic_cdk::call::Call::unbounded_wait(canister_id, "remove_file")
113        .with_arg(args)
114        .await
115        .map_err(|e| format!("Call failed: {:?}", e))?;
116
117    response
118        .candid::<remove_file::Response>()
119        .map_err(|e| format!("Failed to decode response: {:?}", e))
120}