1+ use std:: str;
2+ use serde:: Deserialize ;
3+ use serde_json:: { json, from_str } ;
4+ use std:: collections:: HashMap ;
5+
16mod raw;
27
38pub fn get_used_gas ( ) -> i64 {
@@ -8,12 +13,73 @@ pub fn get_call_result_size() -> u32 {
813 unsafe { raw:: call_result_size ( ) }
914}
1015
11- pub fn call ( ) {
12- let call_data = "This is some call data" ;
16+ pub struct HttpCallOptions {
17+ method : Option < String > ,
18+ body : Option < String > ,
19+ headers : Option < HashMap < String , String > > ,
20+ }
21+
22+ #[ derive( Deserialize ) ]
23+ struct RawCallResult {
24+ status : i32 ,
25+ result : String ,
26+ error : bool ,
27+ }
28+
29+ #[ derive( Debug ) ]
30+ pub struct CallResult {
31+ pub status : i32 ,
32+ pub result : String ,
33+ }
34+
35+ #[ derive( Debug ) ]
36+ pub struct CallResultError {
37+ pub error : String ,
38+ pub status : i32 ,
39+ }
40+
41+ pub fn http_call ( url : & str , options : Option < HttpCallOptions > ) -> Result < CallResult , CallResultError > {
42+ let unwrapped_options = options. unwrap_or ( HttpCallOptions {
43+ method : Some ( "GET" . to_string ( ) ) ,
44+ body : None ,
45+ headers : None ,
46+ } ) ;
47+
48+ let call_data = json ! ( {
49+ "url" : url,
50+ "type" : "http" ,
51+ "options" : {
52+ "headers" : unwrapped_options. headers,
53+ "body" : unwrapped_options. body,
54+ "method" : unwrapped_options. method. unwrap_or( "GET" . to_string( ) ) ,
55+ }
56+ } ) . to_string ( ) ;
1357
1458 unsafe {
15- raw:: call ( call_data. as_ptr ( ) , call_data. len ( ) as i32 ) ;
59+ raw:: http_call ( call_data. as_ptr ( ) , call_data. len ( ) as i32 ) ;
1660 }
1761
18- let mut result_data: Vec < u8 > = Vec :: with_capacity ( get_call_result_size ( ) as usize ) ;
19- }
62+ let mut result_data: Vec < u8 > = Vec :: new ( ) ;
63+ result_data. resize ( get_call_result_size ( ) as usize , 0 ) ;
64+
65+ unsafe {
66+ raw:: call_result_copy ( result_data. as_ptr ( ) )
67+ }
68+
69+ let result = str:: from_utf8 ( & result_data) ;
70+ let unwrapped_result = result. unwrap_or ( "ERR_PARSING_RESULT" ) ;
71+
72+ let parsed_result: RawCallResult = from_str ( unwrapped_result) . unwrap ( ) ;
73+
74+ if parsed_result. error {
75+ return Err ( CallResultError {
76+ error : parsed_result. result ,
77+ status : parsed_result. status ,
78+ } ) ;
79+ }
80+
81+ return Ok ( CallResult {
82+ status : parsed_result. status ,
83+ result : parsed_result. result ,
84+ } ) ;
85+ }
0 commit comments