@@ -37,14 +37,26 @@ def query_endpoint(self, canister_id, data):
37
37
ret = self .client .query (canister_id , data )
38
38
return cbor2 .loads (ret )
39
39
40
+ async def query_endpoint_async (self , canister_id , data ):
41
+ ret = await self .client .query_async (canister_id , data )
42
+ return cbor2 .loads (ret )
43
+
40
44
def call_endpoint (self , canister_id , request_id , data ):
41
45
self .client .call (canister_id , request_id , data )
42
46
return request_id
43
47
48
+ async def call_endpoint_async (self , canister_id , request_id , data ):
49
+ await self .client .call_async (canister_id , request_id , data )
50
+ return request_id
51
+
44
52
def read_state_endpoint (self , canister_id , data ):
45
53
result = self .client .read_state (canister_id , data )
46
54
return result
47
55
56
+ async def read_state_endpoint_async (self , canister_id , data ):
57
+ result = await self .client .read_state_async (canister_id , data )
58
+ return result
59
+
48
60
def query_raw (self , canister_id , method_name , arg , return_type = None , effective_canister_id = None ):
49
61
req = {
50
62
'request_type' : "query" ,
@@ -61,6 +73,22 @@ def query_raw(self, canister_id, method_name, arg, return_type = None, effective
61
73
elif result ['status' ] == 'rejected' :
62
74
return result ['reject_message' ]
63
75
76
+ async def query_raw_async (self , canister_id , method_name , arg , return_type = None , effective_canister_id = None ):
77
+ req = {
78
+ 'request_type' : "query" ,
79
+ 'sender' : self .identity .sender ().bytes ,
80
+ 'canister_id' : Principal .from_str (canister_id ).bytes if isinstance (canister_id , str ) else canister_id .bytes ,
81
+ 'method_name' : method_name ,
82
+ 'arg' : arg ,
83
+ 'ingress_expiry' : self .get_expiry_date ()
84
+ }
85
+ _ , data = sign_request (req , self .identity )
86
+ result = await self .query_endpoint_async (canister_id if effective_canister_id is None else effective_canister_id , data )
87
+ if result ['status' ] == 'replied' :
88
+ return decode (result ['reply' ]['arg' ], return_type )
89
+ elif result ['status' ] == 'rejected' :
90
+ return result ['reject_message' ]
91
+
64
92
def update_raw (self , canister_id , method_name , arg , return_type = None , effective_canister_id = None , ** kwargs ):
65
93
req = {
66
94
'request_type' : "call" ,
@@ -82,7 +110,26 @@ def update_raw(self, canister_id, method_name, arg, return_type = None, effectiv
82
110
else :
83
111
raise Exception ('Timeout to poll result, current status: ' + str (status ))
84
112
85
-
113
+ async def update_raw_async (self , canister_id , method_name , arg , return_type = None , effective_canister_id = None , ** kwargs ):
114
+ req = {
115
+ 'request_type' : "call" ,
116
+ 'sender' : self .identity .sender ().bytes ,
117
+ 'canister_id' : Principal .from_str (canister_id ).bytes if isinstance (canister_id , str ) else canister_id .bytes ,
118
+ 'method_name' : method_name ,
119
+ 'arg' : arg ,
120
+ 'ingress_expiry' : self .get_expiry_date ()
121
+ }
122
+ req_id , data = sign_request (req , self .identity )
123
+ eid = canister_id if effective_canister_id is None else effective_canister_id
124
+ _ = await self .call_endpoint_async (eid , req_id , data )
125
+ # print('update.req_id:', req_id.hex())
126
+ status , result = await self .poll_async (eid , req_id , ** kwargs )
127
+ if status == 'rejected' :
128
+ raise Exception ('Rejected: ' + result .decode ())
129
+ elif status == 'replied' :
130
+ return decode (result , return_type )
131
+ else :
132
+ raise Exception ('Timeout to poll result, current status: ' + str (status ))
86
133
87
134
def read_state_raw (self , canister_id , paths ):
88
135
req = {
@@ -101,6 +148,23 @@ def read_state_raw(self, canister_id, paths):
101
148
cert = cbor2 .loads (d ['certificate' ])
102
149
return cert
103
150
151
+ async def read_state_raw_async (self , canister_id , paths ):
152
+ req = {
153
+ 'request_type' : 'read_state' ,
154
+ 'sender' : self .identity .sender ().bytes ,
155
+ 'paths' : paths ,
156
+ 'ingress_expiry' : self .get_expiry_date (),
157
+ }
158
+ _ , data = sign_request (req , self .identity )
159
+ ret = await self .read_state_endpoint_async (canister_id , data )
160
+ if ret == b'Invalid path requested.' :
161
+ raise ValueError ('Invalid path requested!' )
162
+ elif ret == b'Could not parse body as read request: invalid type: byte array, expected a sequence' :
163
+ raise ValueError ('Could not parse body as read request: invalid type: byte array, expected a sequence' )
164
+ d = cbor2 .loads (ret )
165
+ cert = cbor2 .loads (d ['certificate' ])
166
+ return cert
167
+
104
168
def request_status_raw (self , canister_id , req_id ):
105
169
paths = [
106
170
['request_status' .encode (), req_id ],
@@ -112,6 +176,17 @@ def request_status_raw(self, canister_id, req_id):
112
176
else :
113
177
return status .decode (), cert
114
178
179
+ async def request_status_raw_async (self , canister_id , req_id ):
180
+ paths = [
181
+ ['request_status' .encode (), req_id ],
182
+ ]
183
+ cert = await self .read_state_raw_async (canister_id , paths )
184
+ status = lookup (['request_status' .encode (), req_id , 'status' .encode ()], cert )
185
+ if (status == None ):
186
+ return status , cert
187
+ else :
188
+ return status .decode (), cert
189
+
115
190
def poll (self , canister_id , req_id , delay = 1 , timeout = float ('inf' )):
116
191
status = None
117
192
for _ in wait (delay , timeout ):
@@ -129,3 +204,21 @@ def poll(self, canister_id, req_id, delay=1, timeout=float('inf')):
129
204
return status , msg
130
205
else :
131
206
return status , _
207
+
208
+ async def poll_async (self , canister_id , req_id , delay = 1 , timeout = float ('inf' )):
209
+ status = None
210
+ for _ in wait (delay , timeout ):
211
+ status , cert = await self .request_status_raw_async (canister_id , req_id )
212
+ if status == 'replied' or status == 'done' or status == 'rejected' :
213
+ break
214
+
215
+ if status == 'replied' :
216
+ path = ['request_status' .encode (), req_id , 'reply' .encode ()]
217
+ res = lookup (path , cert )
218
+ return status , res
219
+ elif status == 'rejected' :
220
+ path = ['request_status' .encode (), req_id , 'reject_message' .encode ()]
221
+ msg = lookup (path , cert )
222
+ return status , msg
223
+ else :
224
+ return status , _
0 commit comments