Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from wacker-dev/json
Browse files Browse the repository at this point in the history
Add json method for Response
  • Loading branch information
iawia002 authored May 14, 2024
2 parents 26a9322 + 66798f0 commit ecaaeee
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ keywords = ["webassembly", "wasm", "wasi"]
repository = "https://github.com/wacker-dev/wasi-http-client"
license = "Apache-2.0"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
anyhow = "1.0.83"
wasi = "0.13.0"
url = "2.5.0"
serde = { version = "1.0.201", optional = true }
serde_json = { version = "1.0.117", optional = true }

[features]
json = ["dep:serde", "dep:serde_json"]
13 changes: 13 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anyhow::{anyhow, Result};
#[cfg(feature = "json")]
use serde::de::DeserializeOwned;
use std::collections::HashMap;
use wasi::http::types::{IncomingResponse, StatusCode};
use wasi::io::streams::StreamError;
Expand Down Expand Up @@ -57,4 +59,15 @@ impl Response {
pub fn body(&self) -> &Vec<u8> {
&self.body
}

/// Deserialize the response body as JSON.
///
/// # Optional
///
/// This requires the `json` feature enabled.
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub fn json<T: DeserializeOwned>(&self) -> Result<T> {
Ok(serde_json::from_slice(&self.body)?)
}
}
3 changes: 2 additions & 1 deletion tests/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package = "component:wasi-http-client-test-program"
"wasi:random" = { path = "wit/deps/random" }

[dependencies]
wasi-http-client = { path = "../../../wasi-http-client" }
wasi-http-client = { path = "../../../wasi-http-client", features = ["json"] }

wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
serde = { version = "1.0.201", features = ["derive"] }
17 changes: 17 additions & 0 deletions tests/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
mod bindings;

use bindings::exports::wasi::cli::run::Guest;
use serde::Deserialize;
use std::time::Duration;
use wasi_http_client::Client;

struct Component;

bindings::export!(Component with_types_in bindings);

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct Data {
origin: String,
url: String,
}

impl Guest for Component {
fn run() -> Result<(), ()> {
// get with query
Expand All @@ -22,6 +30,15 @@ impl Guest for Component {
String::from_utf8_lossy(resp.body())
);

// get with json response
let resp = Client::new().get("https://httpbin.org/get").send().unwrap();
let json_data = resp.json::<Data>().unwrap();
println!(
"GET https://httpbin.org/get, status code: {}, body:\n{:?}\n",
resp.status(),
json_data,
);

// post with json data
let resp = Client::new()
.post("https://httpbin.org/post")
Expand Down

0 comments on commit ecaaeee

Please sign in to comment.