extension_api

Crates.ioextension_api
lib.rsextension_api
version
sourcesrc
created_at2024-12-01 12:57:18.875214
updated_at2024-12-08 07:44:44.191435
description自定义wasm plugin 扩展api
homepage
repositoryhttps://github.com/workoss/wasm-extension
max_upload_size
id1467424
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
workoss (ossworker)

documentation

https://docs.rs/extension_api

README

extension_api

使用

依赖 extension_api

add this to your Cargo.toml:

[dependencies]
extension_api = "0.0.4"

实现 api

cargo create --lib demo_plugin

src/lib.rs

use extension_api::{
    register_plugin,
    wit::{
        get_settings,
        workoss::extension::{
            extension_http::{fetch, HttpRequest},
            platform::current_platform,
        },
        PluginInput, PluginOutput,
    },
    Plugin,
};

struct DemoPlugin;

impl Plugin for DemoPlugin {
    fn new() -> Self
    where
        Self: Sized,
    {
        Self
    }

    fn run_plugin(&self, input: PluginInput) -> Result<PluginOutput, String> {
        let (os, arch) = current_platform()?;
        println!("host os:{os:?},arch:{arch:?}");
        let input = String::from_utf8(input.body.unwrap()).unwrap();
        println!("demo plugin input:{:#?}", input);
        let settings = get_settings(Some("plugin_key"))?;
        println!("settings: {:#?}", settings);
        let req = HttpRequest::builder()
            .method(extension_api::wit::workoss::extension::http_client::HttpMethod::Get)
            .url("https://www.baidu.com")
            .build()?;
        let resp = fetch(&req).map_err(|e| format!("{e:?}"))?;
        let body = String::from_utf8(resp.body).unwrap();
        println!("resp body:{:?}", &body);

        Ok(PluginOutput {
            body: Some(body.into_bytes()),
            mime_type: extension_api::wit::MimeType::Json,
        })
    }
}

register_plugin!(DemoPlugin);

Commit count: 0

cargo fmt