Skip to content

Commit e652060

Browse files
WIP: Fetching API data through WASM
1 parent 0ed74e0 commit e652060

File tree

16 files changed

+492
-35
lines changed

16 files changed

+492
-35
lines changed

examples/simple-url-call/Cargo.lock

Lines changed: 263 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/simple-url-call/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ edition = "2018"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10+
serde_json = "1.0"
11+
serde = { version = "1.0", features = ["derive"] }
12+
jsonpath-rust = "0.1.0"
Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
use std::str;
2+
use serde::Deserialize;
3+
use serde_json::{ json, from_str };
4+
use std::collections::HashMap;
5+
16
mod raw;
27

38
pub 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+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern "C" {
2-
pub fn call(call_data_offset: *const u8, call_data_length: i32);
2+
pub fn http_call(call_data_offset: *const u8, call_data_length: i32);
33
pub fn call_result_size() -> u32;
4-
pub fn call_result_copy(result_offset: i32);
4+
pub fn call_result_copy(result_offset: *const u8);
55
pub fn gas_used() -> i64;
66
}

0 commit comments

Comments
 (0)