Expand description
HTTP: over wasi:http in a component (the wasi feature), and through
the test host natively.
An HTTP client over wasi:http (wasi feature): any method, request
headers and body, a timeout, the response’s status, headers and body,
and a cap on how much body is read. Grant: fetch:<host> (or
fetch:<scheme>://<host>[:<port>], network.full, machine.full);
the host refuses any other host before a connection is made, and the
plugin sees Error::GrantDenied { grant: "fetch:<host>" }.
Natively (cargo test) requests go to the test host
(crate::testing::MockHost::http), which checks the same grants,
records every request and answers from the responses the test queued.
ⓘ
//! A daemon plugin that polls an HTTPS endpoint every five minutes and
//! publishes the latest body as a live message. Needs the SDK's `wasi`
//! feature (the WASI HTTP imports) and the grant `fetch:api.github.com`.
//! The request names its headers, a timeout and a body cap; the response
//! carries its status and headers.
#![no_std]
use standard_plugin::daemon::{Context, DaemonPlugin};
#[standard_plugin::daemon]
struct Status;
impl DaemonPlugin for Status {
fn activate(_cx: &Context) -> Self {
Self
}
async fn run(&self, cx: Context) {
loop {
#[cfg(target_arch = "wasm32")]
{
use standard_plugin::daemon::http::Request;
let response = Request::get("https://api.github.com/zen")
.header("accept", "text/plain")
.header("user-agent", "standard-plugin-example")
.timeout_ms(10_000)
.max_body(4 * 1024)
.send()
.await;
if let Ok(response) = response
&& response.ok()
&& let Ok(text) = response.text()
{
let _ = cx.live().publish("status.zen", text);
}
}
cx.sleep(5 * 60_000).await;
}
}
}Structs§
- Request
- A request. Build it with
Request::get,Request::postorRequest::new, thenRequest::sendit. - Response
- A response: status, headers (names lowercase, in order) and body.
Constants§
- DEFAULT_
MAX_ BODY - The most body a response reads when the request names no cap.
- DEFAULT_
TIMEOUT_ MS - How long a request may take when it names no timeout: headers and the whole body.
Functions§
- get
GET urlwith the default timeout and cap.