standard-plugin-sdk 0.1.0

Write Standard Code plugins in Rust: wasm components against standard:plugin@2.0.0
Documentation
//! 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;
        }
    }
}