@@ -3,12 +3,12 @@ use std::pin::Pin;
33
44use did:: rpc:: request:: RpcRequest ;
55use did:: rpc:: response:: RpcResponse ;
6- use ic_cdk:: api:: management_canister:: http_request:: {
7- self , CanisterHttpRequestArgument , HttpHeader , HttpMethod , TransformContext ,
6+ use ic_exports:: ic_cdk;
7+ use ic_exports:: ic_cdk:: management_canister:: {
8+ self , HttpHeader , HttpMethod , HttpRequestArgs , TransformContext ,
89} ;
910#[ cfg( feature = "sanitize-http-outcall" ) ]
10- use ic_cdk:: api:: management_canister:: http_request:: { HttpResponse , TransformArgs } ;
11- use ic_exports:: ic_cdk;
11+ use ic_exports:: ic_cdk:: management_canister:: { HttpRequestResult , TransformArgs } ;
1212
1313use crate :: { Client , JsonRpcError , JsonRpcResult } ;
1414
@@ -69,10 +69,15 @@ impl HttpOutcallClient {
6969 /// Only available with Cargo feature `sanitize-http-outcall`.
7070 #[ cfg( feature = "sanitize-http-outcall" ) ]
7171 pub fn sanitized ( mut self ) -> Self {
72- self . transform_context = Some ( TransformContext :: from_name (
73- "sanitize_http_response" . into ( ) ,
74- vec ! [ ] ,
75- ) ) ;
72+ use ic_exports:: ic_cdk:: management_canister:: TransformFunc ;
73+
74+ self . transform_context = Some ( TransformContext {
75+ function : TransformFunc ( candid:: Func {
76+ method : "sanitize_http_response" . to_string ( ) ,
77+ principal : ic_exports:: ic_cdk:: api:: canister_self ( ) ,
78+ } ) ,
79+ context : vec ! [ ] ,
80+ } ) ;
7681 self
7782 }
7883
@@ -91,7 +96,7 @@ impl HttpOutcallClient {
9196
9297#[ cfg( feature = "sanitize-http-outcall" ) ]
9398#[ ic_cdk:: query]
94- fn sanitize_http_response ( raw_response : TransformArgs ) -> HttpResponse {
99+ fn sanitize_http_response ( raw_response : TransformArgs ) -> HttpRequestResult {
95100 const USE_HEADERS : & [ & str ] = & [ "content-encoding" , "content-length" , "content-type" , "host" ] ;
96101 let TransformArgs { mut response, .. } = raw_response;
97102 response
@@ -133,7 +138,7 @@ impl Client for HttpOutcallClient {
133138 log:: trace!( "Making http request to {url} with headers: {headers:?}" ) ;
134139 log:: trace!( "Request body is: {}" , String :: from_utf8_lossy( & body) ) ;
135140
136- let request = CanisterHttpRequestArgument {
141+ let request = HttpRequestArgs {
137142 url,
138143 max_response_bytes,
139144 method : HttpMethod :: POST ,
@@ -142,23 +147,17 @@ impl Client for HttpOutcallClient {
142147 transform,
143148 } ;
144149
145- let cost = http_request_required_cycles ( & request) ;
150+ let cost = management_canister :: cost_http_request ( & request) ;
146151
147- let cycles_available = ic_exports:: ic_cdk:: api:: canister_balance128 ( ) ;
152+ let cycles_available = ic_exports:: ic_cdk:: api:: canister_cycle_balance ( ) ;
148153 if cycles_available < cost {
149154 return Err ( JsonRpcError :: InsufficientCycles {
150155 available : cycles_available,
151156 cost,
152157 } ) ;
153158 }
154159
155- let http_response = http_request:: http_request ( request, cost)
156- . await
157- . map ( |( res, ) | res)
158- . map_err ( |( r, m) | JsonRpcError :: CanisterCall {
159- rejection_code : r,
160- message : m,
161- } ) ?;
160+ let http_response = management_canister:: http_request ( & request) . await ?;
162161
163162 log:: trace!(
164163 "CanisterClient - Response from http_outcall'. Response: {} {:?}. Body: {}" ,
@@ -176,23 +175,6 @@ impl Client for HttpOutcallClient {
176175 }
177176}
178177
179- // Calculate cycles for http_request
180- // NOTE:
181- // https://github.com/dfinity/cdk-rs/blob/710a6cdcc3eb03d2392df1dfd5f047dff9deee80/examples/management_canister/src/caller/lib.rs#L7-L19
182- pub fn http_request_required_cycles ( arg : & CanisterHttpRequestArgument ) -> u128 {
183- let max_response_bytes = match arg. max_response_bytes {
184- Some ( ref n) => * n as u128 ,
185- None => 2 * 1024 * 1024u128 , // default 2MiB
186- } ;
187- let arg_raw = candid:: utils:: encode_args ( ( arg, ) ) . expect ( "Failed to encode arguments." ) ;
188- // The fee is for a 13-node subnet to demonstrate a typical usage.
189- ( 3_000_000u128
190- + 60_000u128 * 13
191- + ( arg_raw. len ( ) as u128 + "http_request" . len ( ) as u128 ) * 400
192- + max_response_bytes * 800 )
193- * 13
194- }
195-
196178#[ cfg( test) ]
197179#[ cfg( feature = "sanitize-http-outcall" ) ]
198180mod tests {
@@ -203,7 +185,7 @@ mod tests {
203185 #[ test]
204186 fn sanitize_http_response_removes_extra_headers ( ) {
205187 let transform_args = TransformArgs {
206- response : HttpResponse {
188+ response : HttpRequestResult {
207189 status : 200u128 . into ( ) ,
208190 headers : vec ! [
209191 HttpHeader {
@@ -228,7 +210,7 @@ mod tests {
228210 context : vec ! [ ] ,
229211 } ;
230212
231- let sanitized: HttpResponse = sanitize_http_response ( transform_args) ;
213+ let sanitized: HttpRequestResult = sanitize_http_response ( transform_args) ;
232214 assert_eq ! ( sanitized. headers. len( ) , 3 ) ;
233215 assert_eq ! ( sanitized. status, Nat :: from( 200u128 ) ) ;
234216 assert ! (
0 commit comments