Skip to content

Commit

Permalink
chore: extend http example, update docs and test (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice authored Jan 4, 2024
1 parent 2b512f9 commit 64feb1c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
TEST=$(extism call zig-out/bin/basic-example.wasm http_get --allow-host '*')
echo $TEST
echo $TEST | grep '"userId": 1'
echo $TEST | grep 'user=1'
TEST=$(extism call zig-out/bin/basic-example.wasm greet --config user=Benjamin)
echo $TEST
Expand Down
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ extism call ./zig-out/bin/my-plugin.wasm log_stuff --log-level=debug
## HTTP

Sometimes it is useful to let a plug-in [make HTTP calls](https://pkg.go.dev/github.com/extism/go-pdk#HTTPRequest.Send). [See this example](example/http.go)
Sometimes it is useful to let a plug-in [make HTTP calls]. [see: Extism HTTP library](src/http.zig)

```zig
const http = extism_pdk.http;
Expand All @@ -290,8 +290,37 @@ export fn http_get() i32 {
const res = plugin.request(req, null) catch unreachable;
defer res.deinit();
if (res.status != 200) {
plugin.setError("request failed");
return @as(i32, res.status);
}
// get the bytes for the response body
const body = res.body(allocator) catch unreachable;
// => { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }
const Todo = struct {
userId: u32,
id: u32,
title: []const u8,
completed: bool,
};
const todo = std.json.parseFromSlice(Todo, allocator, body, .{}) catch |err| {
plugin.setError(std.fmt.allocPrint(allocator, "parse error: {any}", .{err}) catch unreachable);
return 1;
};
defer todo.deinit();
// format a string with the todo data
const t = todo.value;
const tmpl = "[id={d}] '{s}' by user={d} is complete: {any}\n";
const args = .{ t.id, t.title, t.userId, t.completed };
const output = std.fmt.allocPrint(allocator, tmpl, args) catch unreachable;
// allocate space for the output data
const outMem = plugin.allocateBytes(output);
// `outputMemory` provides a zero-copy way to write plugin data back to the host
plugin.outputMemory(res.memory);
plugin.outputMemory(outMem);
return 0;
}
Expand Down
29 changes: 27 additions & 2 deletions examples/basic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,34 @@ export fn http_get() i32 {
const res = plugin.request(req, null) catch unreachable;
defer res.deinit();

if (res.status != 200) {
plugin.setError("request failed");
return @as(i32, res.status);
}

// get the bytes for the res body
const body = res.body(allocator) catch unreachable;
// => { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }
const Todo = struct {
userId: u32,
id: u32,
title: []const u8,
completed: bool,
};
const todo = std.json.parseFromSlice(Todo, allocator, body, .{}) catch |err| {
plugin.setError(std.fmt.allocPrint(allocator, "parse error: {any}", .{err}) catch unreachable);
return 1;
};
defer todo.deinit();

const t = todo.value;
const tmpl = "[id={d}] '{s}' by user={d} is complete: {any}\n";
const args = .{ t.id, t.title, t.userId, t.completed };
const output = std.fmt.allocPrint(allocator, tmpl, args) catch unreachable;
const outMem = plugin.allocateBytes(output);

// `outputMemory` provides a zero-copy way to write plugin data back to the host
plugin.outputMemory(res.memory);
plugin.outputMemory(outMem);

return 0;
}
Expand Down Expand Up @@ -104,7 +130,6 @@ export fn add() i32 {

const params = std.json.parseFromSlice(Add, allocator, input, std.json.ParseOptions{}) catch unreachable;
const sum = Sum{ .sum = params.value.a + params.value.b };

const output = std.json.stringifyAlloc(allocator, sum, std.json.StringifyOptions{}) catch unreachable;
plugin.output(output);
return 0;
Expand Down

0 comments on commit 64feb1c

Please sign in to comment.