isp_sdk/icsp/
icsp_did.rs

1use candid::{Nat, Principal};
2use ic_cdk::api::call::CallResult;
3use ic_cdk::export::candid::{CandidType, Deserialize};
4
5#[derive(CandidType, Deserialize, Debug)]
6pub struct BucketStatusExt {
7    pub used_memory: Nat,
8    pub canister_id: Principal,
9}
10
11#[derive(CandidType, Deserialize, Debug)]
12pub struct Buckets {
13    pub live_buckets: Vec<BucketStatusExt>,
14    pub dead_buckets: Vec<BucketStatusExt>,
15}
16
17#[derive(CandidType, Deserialize, Debug)]
18pub struct FileBufExt {
19    pub bucket_id: Principal,
20    pub total_index: Nat,
21    pub wrote_page: Vec<bool>,
22    pub file_type: String,
23    pub is_http_open: bool,
24    pub total_size: u64,
25    pub received: Nat,
26}
27
28#[derive(CandidType, Deserialize, Debug)]
29pub struct StoreArgs {
30    pub key: String,
31    pub value: Vec<u8>,
32    pub total_index: Nat,
33    pub file_type: String,
34    pub is_http_open: bool,
35    pub total_size: u64,
36    pub index: Nat,
37}
38
39#[derive(CandidType, Deserialize, Debug)]
40struct OtherFile {
41    pub file_location: FileLocation,
42    pub file_key: String,
43    pub file_url: String,
44    pub file_type: String,
45}
46
47#[derive(CandidType, Deserialize, Debug)]
48enum FileLocation {
49    IPFS,
50    Arweave,
51}
52
53type icsp = candid::Service;
54
55struct SERVICE(Principal);
56impl SERVICE {
57    pub async fn get_cycle_balance(&self) -> CallResult<(Nat,)> {
58        ic_cdk::call(self.0, "getCycleBalance", ()).await
59    }
60
61    pub async fn get_file_info(&self, file_key: String) -> CallResult<(Option<FileBufExt>,)> {
62        ic_cdk::call(self.0, "getFileInfo", (file_key,)).await
63    }
64
65    pub async fn get_all_ic_file_key(&self) -> CallResult<(Vec<String>,)> {
66        ic_cdk::call(self.0, "getAllIcFileKey", ()).await
67    }
68
69    pub async fn get_all_ipfs_file_key(&self) -> CallResult<(Vec<String>,)> {
70        ic_cdk::call(self.0, "getAllIpfsFileKey", ()).await
71    }
72
73    pub async fn get_all_arweave_file_key(&self) -> CallResult<(Vec<String>,)> {
74        ic_cdk::call(self.0, "getAllArFileKey", ()).await
75    }
76
77    pub async fn record_file(&self, other_file: OtherFile) -> CallResult<()> {
78        ic_cdk::call(self.0, "recordFile", (other_file,)).await
79    }
80
81    pub async fn delete(&self, arg0: String) -> CallResult<()> {
82        ic_cdk::call(self.0, "delete", (arg0,)).await
83    }
84
85    pub async fn init(&self) -> CallResult<(BucketStatusExt,)> {
86        ic_cdk::call(self.0, "init", ()).await
87    }
88
89    pub async fn get_other_file(
90        &self,
91        file_key: String,
92        file_location: FileLocation,
93    ) -> CallResult<(Option<OtherFile>,)> {
94        ic_cdk::call(self.0, "getOtherFile", (file_key, file_location)).await
95    }
96
97    pub async fn add_admin(&self, new_admin: Principal) -> CallResult<()> {
98        ic_cdk::call(self.0, "addAdmin", (new_admin,)).await
99    }
100
101    pub async fn delete_admin(&self, old_admin: Principal) -> CallResult<()> {
102        ic_cdk::call(self.0, "deleteAdmin", (old_admin,)).await
103    }
104
105    pub async fn get_admins(&self) -> CallResult<(Vec<Principal>,)> {
106        ic_cdk::call(self.0, "getAdmins", ()).await
107    }
108
109    pub async fn get_bucket_of_file(&self, key: String) -> CallResult<(Option<Principal>,)> {
110        ic_cdk::call(self.0, "getBucketOfFile", (key,)).await
111    }
112
113    pub async fn get_buckets(&self) -> CallResult<(Option<Buckets>,)> {
114        ic_cdk::call(self.0, "getBuckets", ()).await
115    }
116
117    pub async fn store(&self, args: StoreArgs) -> CallResult<()> {
118        ic_cdk::call(self.0, "store", (args,)).await
119    }
120}