From 3d0227d0a59316ce78bfa4c388fa884b012d6525 Mon Sep 17 00:00:00 2001 From: Xinzhao Xu Date: Mon, 13 May 2024 10:13:19 +0800 Subject: [PATCH] Add more docs --- .github/workflows/ci.yml | 2 ++ README.md | 15 ++++++++++++++- examples/README.md | 34 ++++++++++++++++++++++++++++++++++ src/lib.rs | 20 ++++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 examples/README.md diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 47747e9..0ed6256 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,3 +20,5 @@ jobs: run: cargo fmt --all -- --check - name: cargo clippy run: cargo clippy --all-targets --all-features -- -D warnings + - name: cargo test + run: cargo test diff --git a/README.md b/README.md index 5a7fe43..ce4b289 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,15 @@ # wasi-http-client -HTTP client library for WASI. + +HTTP client library for [WASI Preview 2](https://github.com/WebAssembly/WASI/tree/main/preview2), +making it easier to send http(s) requests in WASI components. + +```rust +let resp = Client::new() + .post("https://httpbin.org/post") + .body("hello".as_bytes()) + .connect_timeout(Duration::from_secs(5)) + .send() + .unwrap(); + +println!("status code: {}", resp.status()); +``` diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..5cc2111 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,34 @@ +# Examples + +See https://github.com/wacker-dev/wasi-examples/tree/main/http-client for a real-world example. +After compilation, you can use [wasmtime](https://github.com/bytecodealliance/wasmtime) to run it: + +``` +$ wasmtime -S http target/wasm32-wasi/debug/http_client.wasm + +status code: 200 +content-type: application/json +content-length: 297 +access-control-allow-credentials: true +server: gunicorn/19.9.0 +date: Sat, 11 May 2024 09:46:38 GMT +access-control-allow-origin: * +body: +{ + "args": {}, + "data": "hello", + "files": {}, + "form": {}, + "headers": { + "Content-Length": "5", + "Host": "httpbin.org", + "X-Amzn-Trace-Id": "Root=1-663f3e7e-6e3f84f87a20aef56c58a344" + }, + "json": null, + "origin": "……", + "url": "https://httpbin.org/post" +} +``` + +There are specific steps for compilation in the [README](https://github.com/wacker-dev/wasi-examples/blob/main/http-client/README.md), +and the main logic of the sample program is located at [lib.rs](https://github.com/wacker-dev/wasi-examples/blob/main/http-client/src/lib.rs#L14-L19). diff --git a/src/lib.rs b/src/lib.rs index e2380b3..7976612 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,23 @@ +//! # wasi-http-client +//! +//! `wasi_http_client` is an HTTP client library for [WASI Preview 2](https://github.com/WebAssembly/WASI/tree/main/preview2), +//! making it easier to send http(s) requests in WASI components. +//! +//! ``` +//! # use std::time::Duration; +//! # use wasi_http_client::Client; +//! # fn run() { +//! let resp = Client::new() +//! .post("https://httpbin.org/post") +//! .body("hello".as_bytes()) +//! .connect_timeout(Duration::from_secs(5)) +//! .send() +//! .unwrap(); +//! +//! println!("status code: {}", resp.status()); +//! # } +//! ``` + mod client; mod request; mod response;