From 5fee428b91129c1f2b918984eb0454feee6db013 Mon Sep 17 00:00:00 2001 From: Xinzhao Xu Date: Tue, 14 May 2024 15:57:13 +0800 Subject: [PATCH] Add json method for Response --- Cargo.toml | 9 +++++++++ src/response.rs | 15 +++++++++++++++ tests/program/Cargo.toml | 3 ++- tests/program/src/lib.rs | 17 +++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 6e50997..08083c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/src/response.rs b/src/response.rs index 03b2b7c..8cf5bda 100644 --- a/src/response.rs +++ b/src/response.rs @@ -1,4 +1,8 @@ use anyhow::{anyhow, Result}; +#[cfg(feature = "json")] +use serde::de::DeserializeOwned; +#[cfg(feature = "json")] +use serde_json; use std::collections::HashMap; use wasi::http::types::{IncomingResponse, StatusCode}; use wasi::io::streams::StreamError; @@ -57,4 +61,15 @@ impl Response { pub fn body(&self) -> &Vec { &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(&self) -> Result { + Ok(serde_json::from_slice(&self.body)?) + } } diff --git a/tests/program/Cargo.toml b/tests/program/Cargo.toml index a61b73d..0e3daeb 100644 --- a/tests/program/Cargo.toml +++ b/tests/program/Cargo.toml @@ -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"] } diff --git a/tests/program/src/lib.rs b/tests/program/src/lib.rs index 5457c2c..3fad9c7 100644 --- a/tests/program/src/lib.rs +++ b/tests/program/src/lib.rs @@ -2,6 +2,7 @@ mod bindings; use bindings::exports::wasi::cli::run::Guest; +use serde::Deserialize; use std::time::Duration; use wasi_http_client::Client; @@ -9,6 +10,13 @@ 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 @@ -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::().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")