Skip to content

Commit 0dbdb5d

Browse files
authored
Merge pull request #12 from blocklessnetwork/fix/http-open-post-req-body
fix: HttpOpen post request data
2 parents 9157e0b + 27f10a9 commit 0dbdb5d

File tree

7 files changed

+35
-29
lines changed

7 files changed

+35
-29
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "blockless-sdk"
33
version = "0.1.3"
4-
authors = ["Join.G"]
4+
authors = ["Join.G", "Zeeshan.S"]
55
description = "blockless runtime sdk"
66
keywords = ["blockless", "sdk"]
77
readme = "README.md"
@@ -10,7 +10,7 @@ license = "MIT/Apache-2.0"
1010
repository = "https://github.com/blocklessnetwork/sdk-rust"
1111

1212
[dependencies]
13-
json = "0.12"
13+
json = { version = "0.12", default-features = false }
1414

1515
[dev-dependencies]
1616
serde = { version = "1.0", features = ["derive"] }

examples/coingecko_oracle.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::collections::HashMap;
1+
use blockless_sdk::*;
22
use serde::Serialize;
33
use serde_json::json;
4-
use blockless_sdk::*;
4+
use std::collections::HashMap;
55

66
#[derive(Debug, Serialize)]
77
struct CoinPrice {
@@ -14,20 +14,28 @@ fn main() {
1414
// read coin id from stdin
1515
let mut buf = [0; 1024];
1616
let len = read_stdin(&mut buf).unwrap();
17-
let coin_id = std::str::from_utf8(&buf[..len as usize]).unwrap_or_default().trim();
17+
let coin_id = std::str::from_utf8(&buf[..len as usize])
18+
.unwrap_or_default()
19+
.trim();
1820

1921
// perform http request
2022
let http_opts = HttpOptions::new("GET", 30, 10);
2123
let http_res = BlocklessHttp::open(
22-
format!("https://api.coingecko.com/api/v3/simple/price?ids={}&vs_currencies=usd", coin_id).as_str(),
23-
&http_opts
24-
).unwrap();
24+
format!(
25+
"https://api.coingecko.com/api/v3/simple/price?ids={}&vs_currencies=usd",
26+
coin_id
27+
)
28+
.as_str(),
29+
&http_opts,
30+
)
31+
.unwrap();
2532
let body = http_res.get_all_body().unwrap(); // e.g. {"bitcoin":{"usd":67675}}
2633

2734
// println!("{}", String::from_utf8(body.clone()).unwrap());
2835

2936
// parse the json response; extrac usd price
30-
let json: serde_json::Result<HashMap<String, HashMap<String, f64>>> = serde_json::from_slice(&body);
37+
let json: serde_json::Result<HashMap<String, HashMap<String, f64>>> =
38+
serde_json::from_slice(&body);
3139
let Ok(data) = json else {
3240
eprintln!("Failed to parse JSON");
3341
return;

src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ impl std::fmt::Display for CGIErrorKind {
100100
CGIErrorKind::ExecError => write!(f, "CGI Exec Error."),
101101
CGIErrorKind::ReadError => write!(f, "Read Error."),
102102
CGIErrorKind::NoCommandError => write!(f, "No CGI Command Error."),
103-
}
103+
}
104104
}
105105
}
106106

107-
impl std::error::Error for CGIErrorKind {}
107+
impl std::error::Error for CGIErrorKind {}

src/http.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ use std::cmp::Ordering;
33
use crate::{error::HttpErrorKind, http_host::*};
44
use json::JsonValue;
55

6-
pub type Hanlde = u32;
6+
pub type Handle = u32;
77

88
pub type CodeStatus = u32;
99

1010
pub struct BlocklessHttp {
11-
inner: Hanlde,
11+
inner: Handle,
1212
code: CodeStatus,
1313
}
1414

1515
pub struct HttpOptions {
16-
method: String,
17-
connect_timeout: u32,
18-
read_timeout: u32,
19-
body: Option<String>,
16+
pub method: String,
17+
pub connect_timeout: u32,
18+
pub read_timeout: u32,
19+
pub body: Option<String>,
2020
}
2121

2222
impl HttpOptions {

src/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn read_env_vars(buf: &mut [u8]) -> std::io::Result<u32> {
1414
let mut len = 0;
1515
let errno = unsafe { env_var_read(buf.as_mut_ptr(), buf.len() as _, &mut len) };
1616
if errno == 0 {
17-
return Ok(len);
17+
return Ok(len);
1818
}
1919
let err = std::io::Error::from_raw_os_error(errno as i32);
2020
Err(err)

src/memory_host.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#[link(wasm_import_module = "blockless_memory")]
2-
extern "C" {
3-
#[link_name = "memory_read"]
4-
pub(crate) fn memory_read(buf: *mut u8, len: u32, num: *mut u32) -> u32;
5-
#[link_name = "env_var_read"]
6-
pub(crate) fn env_var_read(buf: *mut u8, len: u32, num: *mut u32) -> u32;
7-
}
1+
2+
#[link(wasm_import_module = "blockless_memory")]
3+
extern "C" {
4+
#[link_name = "memory_read"]
5+
pub(crate) fn memory_read(buf: *mut u8, len: u32, num: *mut u32) -> u32;
6+
#[link_name = "env_var_read"]
7+
pub(crate) fn env_var_read(buf: *mut u8, len: u32, num: *mut u32) -> u32;
8+
}

src/socket.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::{
2-
socket_host::*,
3-
SocketErrorKind
4-
};
1+
use crate::{socket_host::*, SocketErrorKind};
52

63
pub fn create_tcp_bind_socket(addr: &str) -> Result<u32, SocketErrorKind> {
74
unsafe {

0 commit comments

Comments
 (0)