Skip to content

Commit

Permalink
Runnable API for URL query params (#196)
Browse files Browse the repository at this point in the history
* update Cargo.lock

* add runnable API for url query params

* Update Grain bindings

Co-authored-by: Oscar Spencer <oscar@grain-lang.org>
  • Loading branch information
cohix and ospencer authored Jan 13, 2022
1 parent ebdc67c commit 3ffcf59
Show file tree
Hide file tree
Showing 42 changed files with 492 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/grain/suborbital/env.gr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export enum FieldType {
FieldTypeHeader,
FieldTypeParams,
FieldTypeState,
FieldTypeQuery,
}

import foreign wasm return_result: (WasmI32, WasmI32, WasmI32) -> Void as witx_bindgen_returnResult from "env"
Expand Down Expand Up @@ -217,6 +218,9 @@ export let rec requestGetField: (FieldType, String, Int32) -> Int32 = (fieldType
FieldTypeState => {
result0_0 = 4n
},
FieldTypeQuery => {
result0_0 = 5n
},
}
let vec1 = key
let ptr1 = WasmI32.add(WasmI32.fromGrain(vec1), 8n)
Expand Down
Binary file added api/grain/suborbital/env.gr.wasm
Binary file not shown.
Binary file added api/grain/suborbital/ffi.gr.wasm
Binary file not shown.
5 changes: 5 additions & 0 deletions api/grain/suborbital/request.gr
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ export let urlParam = key => {
export let state = key => {
getField(Env.FieldTypeState, key)
}

export let queryParam = key => {
let result = getField(Env.FieldTypeQuery, key)
Bytes.toString(Result.unwrap(result))
}
4 changes: 4 additions & 0 deletions api/rust/core/src/req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ pub fn state_raw(key: &str) -> Option<Vec<u8>> {
get_field(FieldType::State.into(), key)
}

pub fn query_param(key: &str) -> String {
get_field(FieldType::Query.into(), key).map_or("".into(), util::to_string)
}

/// Executes the request via FFI
///
/// Then retreives the result from the host and returns it
Expand Down
2 changes: 2 additions & 0 deletions api/rust/core/src/req/field_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub enum FieldType {
Header,
Params,
State,
Query,
}

impl From<FieldType> for i32 {
Expand All @@ -14,6 +15,7 @@ impl From<FieldType> for i32 {
FieldType::Header => 2,
FieldType::Params => 3,
FieldType::State => 4,
FieldType::Query => 5,
}
}
}
5 changes: 5 additions & 0 deletions api/swift/Sources/suborbital/lib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ let fieldTypeBody = Int32(1)
let fieldTypeHeader = Int32(2)
let fieldTypeParams = Int32(3)
let fieldTypeState = Int32(4)
let fieldTypeQuery = Int32(5)

public func ReqMethod() -> String {
return requestGetField(fieldType: fieldTypeMeta, key: "method")
Expand Down Expand Up @@ -171,6 +172,10 @@ public func ReqParam(key: String) -> String {
return requestGetField(fieldType: fieldTypeParams, key: key)
}

public func ReqQuery(key: String) -> String {
return requestGetField(fieldType: fieldTypeQuery, key: key)
}

public func State(key: String) -> String {
return requestGetField(fieldType: fieldTypeState, key: key)
}
Expand Down
5 changes: 5 additions & 0 deletions api/tinygo/runnable/req/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
FieldHeader
FieldParams
FieldState
FieldQuery
)

func getField(fieldType FieldType, key string) []byte {
Expand Down Expand Up @@ -98,3 +99,7 @@ func SetState(key, value string) error {
_, err := setField(FieldState, key, value)
return err
}

func QueryParam(key string) string {
return string(getField(FieldQuery, key))
}
9 changes: 9 additions & 0 deletions rcap/requesthandler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rcap

import (
"net/url"
"strings"

"github.com/pkg/errors"
Expand All @@ -13,6 +14,7 @@ const (
RequestFieldTypeHeader = int32(2)
RequestFieldTypeParams = int32(3)
RequestFieldTypeState = int32(4)
RequestFieldTypeQuery = int32(5)
)

var (
Expand Down Expand Up @@ -108,6 +110,13 @@ func (r *requestHandler) GetField(fieldType int32, key string) ([]byte, error) {
} else {
return nil, ErrKeyNotFound
}
case RequestFieldTypeQuery:
url, err := url.Parse(r.req.URL)
if err != nil {
return nil, errors.Wrap(err, "failed to url.Parse")
}

val = url.Query().Get(key)
default:
return nil, errors.Wrapf(ErrInvalidFieldType, "module requested field type %d", fieldType)
}
Expand Down
Binary file modified rwasm/testdata/as-fetch/as-fetch.wasm
Binary file not shown.
Binary file modified rwasm/testdata/as-get/as-get.wasm
Binary file not shown.
Binary file modified rwasm/testdata/as-graphql/as-graphql.wasm
Binary file not shown.
Binary file modified rwasm/testdata/as-req/as-req.wasm
Binary file not shown.
Binary file modified rwasm/testdata/fetch-swift/fetch-swift.wasm
Binary file not shown.
Binary file modified rwasm/testdata/fetch/fetch.wasm
Binary file not shown.
Binary file modified rwasm/testdata/get-static-swift/get-static-swift.wasm
Binary file not shown.
Binary file modified rwasm/testdata/hello-swift/hello-swift.wasm
Binary file not shown.
Binary file modified rwasm/testdata/legacy/legacy.wasm
Binary file not shown.
Binary file modified rwasm/testdata/return-err/return-err.wasm
Binary file not shown.
Binary file modified rwasm/testdata/rs-dbtest/rs-dbtest.wasm
Binary file not shown.
Binary file modified rwasm/testdata/rs-graphql/rs-graphql.wasm
Binary file not shown.
Binary file modified rwasm/testdata/runnables.wasm.zip
Binary file not shown.
6 changes: 6 additions & 0 deletions rwasm/testdata/rust-urlquery/.runnable.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: rust-urlquery
namespace: default
lang: rust
version: ""
apiVersion: 0.13.1
fqfnUri: ""
60 changes: 60 additions & 0 deletions rwasm/testdata/rust-urlquery/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions rwasm/testdata/rust-urlquery/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "rust-urlquery"
version = "0.1.0"
authors = ["Suborbital Runnable <info@suborbital.dev>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
suborbital = { path = "../../../api/rust/core" }
Binary file added rwasm/testdata/rust-urlquery/rust-urlquery.wasm
Binary file not shown.
21 changes: 21 additions & 0 deletions rwasm/testdata/rust-urlquery/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use suborbital::runnable::*;
use suborbital::req;

struct RustUrlquery{}

impl Runnable for RustUrlquery {
fn run(&self, _: Vec<u8>) -> Result<Vec<u8>, RunErr> {
let query = req::query_param("message");

Ok(String::from(format!("hello {}", query)).as_bytes().to_vec())
}
}


// initialize the runner, do not edit below //
static RUNNABLE: &RustUrlquery = &RustUrlquery{};

#[no_mangle]
pub extern fn _start() {
use_runnable(RUNNABLE);
}
Binary file modified rwasm/testdata/swift-get/swift-get.wasm
Binary file not shown.
Binary file modified rwasm/testdata/swift-log/swift-log.wasm
Binary file not shown.
Binary file modified rwasm/testdata/swift-set/swift-set.wasm
Binary file not shown.
Binary file modified rwasm/testdata/tinygo-cache/tinygo-cache.wasm
Binary file not shown.
Binary file modified rwasm/testdata/tinygo-hello-echo/tinygo-hello-echo.wasm
Binary file not shown.
Binary file modified rwasm/testdata/tinygo-http-get/tinygo-http-get.wasm
Binary file not shown.
Binary file modified rwasm/testdata/tinygo-log/tinygo-log.wasm
Binary file not shown.
Binary file modified rwasm/testdata/tinygo-req/tinygo-req.wasm
Binary file not shown.
6 changes: 6 additions & 0 deletions rwasm/testdata/tinygo-urlquery/.runnable.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: tinygo-urlquery
namespace: default
lang: tinygo
version: ""
apiVersion: 0.13.1
fqfnUri: ""
7 changes: 7 additions & 0 deletions rwasm/testdata/tinygo-urlquery/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module vendor.suborbital.network/tinygo-urlquery

replace github.com/suborbital/reactr => ../../../

require github.com/suborbital/reactr v0.13.0

go 1.17
Loading

0 comments on commit 3ffcf59

Please sign in to comment.