Skip to content

Commit

Permalink
Merge pull request #12 from blocklessnetwork/fix/http-open-post-req-body
Browse files Browse the repository at this point in the history
fix: HttpOpen post request data
  • Loading branch information
Joinhack authored Jun 20, 2024
2 parents 9157e0b + 27f10a9 commit 0dbdb5d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "blockless-sdk"
version = "0.1.3"
authors = ["Join.G"]
authors = ["Join.G", "Zeeshan.S"]
description = "blockless runtime sdk"
keywords = ["blockless", "sdk"]
readme = "README.md"
Expand All @@ -10,7 +10,7 @@ license = "MIT/Apache-2.0"
repository = "https://github.com/blocklessnetwork/sdk-rust"

[dependencies]
json = "0.12"
json = { version = "0.12", default-features = false }

[dev-dependencies]
serde = { version = "1.0", features = ["derive"] }
Expand Down
22 changes: 15 additions & 7 deletions examples/coingecko_oracle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use blockless_sdk::*;
use serde::Serialize;
use serde_json::json;
use blockless_sdk::*;
use std::collections::HashMap;

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

// perform http request
let http_opts = HttpOptions::new("GET", 30, 10);
let http_res = BlocklessHttp::open(
format!("https://api.coingecko.com/api/v3/simple/price?ids={}&vs_currencies=usd", coin_id).as_str(),
&http_opts
).unwrap();
format!(
"https://api.coingecko.com/api/v3/simple/price?ids={}&vs_currencies=usd",
coin_id
)
.as_str(),
&http_opts,
)
.unwrap();
let body = http_res.get_all_body().unwrap(); // e.g. {"bitcoin":{"usd":67675}}

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

// parse the json response; extrac usd price
let json: serde_json::Result<HashMap<String, HashMap<String, f64>>> = serde_json::from_slice(&body);
let json: serde_json::Result<HashMap<String, HashMap<String, f64>>> =
serde_json::from_slice(&body);
let Ok(data) = json else {
eprintln!("Failed to parse JSON");
return;
Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ impl std::fmt::Display for CGIErrorKind {
CGIErrorKind::ExecError => write!(f, "CGI Exec Error."),
CGIErrorKind::ReadError => write!(f, "Read Error."),
CGIErrorKind::NoCommandError => write!(f, "No CGI Command Error."),
}
}
}
}

impl std::error::Error for CGIErrorKind {}
impl std::error::Error for CGIErrorKind {}
12 changes: 6 additions & 6 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ use std::cmp::Ordering;
use crate::{error::HttpErrorKind, http_host::*};
use json::JsonValue;

pub type Hanlde = u32;
pub type Handle = u32;

pub type CodeStatus = u32;

pub struct BlocklessHttp {
inner: Hanlde,
inner: Handle,
code: CodeStatus,
}

pub struct HttpOptions {
method: String,
connect_timeout: u32,
read_timeout: u32,
body: Option<String>,
pub method: String,
pub connect_timeout: u32,
pub read_timeout: u32,
pub body: Option<String>,
}

impl HttpOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn read_env_vars(buf: &mut [u8]) -> std::io::Result<u32> {
let mut len = 0;
let errno = unsafe { env_var_read(buf.as_mut_ptr(), buf.len() as _, &mut len) };
if errno == 0 {
return Ok(len);
return Ok(len);
}
let err = std::io::Error::from_raw_os_error(errno as i32);
Err(err)
Expand Down
15 changes: 8 additions & 7 deletions src/memory_host.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#[link(wasm_import_module = "blockless_memory")]
extern "C" {
#[link_name = "memory_read"]
pub(crate) fn memory_read(buf: *mut u8, len: u32, num: *mut u32) -> u32;
#[link_name = "env_var_read"]
pub(crate) fn env_var_read(buf: *mut u8, len: u32, num: *mut u32) -> u32;
}

#[link(wasm_import_module = "blockless_memory")]
extern "C" {
#[link_name = "memory_read"]
pub(crate) fn memory_read(buf: *mut u8, len: u32, num: *mut u32) -> u32;
#[link_name = "env_var_read"]
pub(crate) fn env_var_read(buf: *mut u8, len: u32, num: *mut u32) -> u32;
}
5 changes: 1 addition & 4 deletions src/socket.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
socket_host::*,
SocketErrorKind
};
use crate::{socket_host::*, SocketErrorKind};

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

0 comments on commit 0dbdb5d

Please sign in to comment.