Skip to content

Commit 146e274

Browse files
feat: update WIT with new guest function web helper. add example function. (#19)
1 parent 07c0e39 commit 146e274

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

momento-functions-host/src/web_extensions.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@
55
use momento_functions_wit::function_web::momento::functions::web_function_support;
66

77
/// Returns the headers for the web function, if any are present.
8+
/// This consumes the headers and takes ownership of the value; multiple calls after will always
9+
/// yield `None`.
810
pub fn headers() -> Vec<(String, String)> {
911
web_function_support::headers()
1012
.into_iter()
1113
.map(|web_function_support::Header { name, value }| (name, value))
1214
.collect()
1315
}
16+
17+
/// Returns the metadata within the caller's token, if present.
18+
/// This consumes the metadata and takes ownership of the value; multiple calls after will always
19+
/// yield `None`.
20+
pub fn token_metadata() -> Option<String> {
21+
web_function_support::token_metadata()
22+
}

momento-functions-wit/wit/guest-function-web.wit

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
interface guest-function-web {
33
use web-function-support.{header};
4-
4+
55
post: func(payload: list<u8>) -> response;
66

77
record response {
@@ -12,8 +12,12 @@ interface guest-function-web {
1212
}
1313

1414
interface web-function-support {
15+
/// Returns a list of headers used in the function. Can only be called once, consumes ownership.
1516
headers: func() -> list<header>;
17+
/// Returns a list of query parameters used in the function. Can only be called once, consumes ownership.
1618
query-parameters: func() -> list<query-parameter>;
19+
/// Returns the metadata provided in the caller's token, if present. Can only be called once, consumes ownership.
20+
token-metadata: func() -> option<string>;
1721

1822
record header {
1923
name: string,

momento-functions/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ crate-type = ["cdylib"]
7070
name = "web-function-json-greeter"
7171
crate-type = ["cdylib"]
7272

73+
[[example]]
74+
name = "web-function-token-metadata"
75+
crate-type = ["cdylib"]
76+
7377
[[example]]
7478
name = "environment-variables"
7579
crate-type = ["cdylib"]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use momento_functions_host::encoding::Json;
2+
3+
#[derive(serde::Serialize)]
4+
struct Response {
5+
message: String,
6+
}
7+
8+
momento_functions::post!(token_metadata);
9+
/// Using the provided host support, this function returns the caller's provided metadata within the `token_id` field
10+
/// of the Momento key. When calling `GenerateApiToken` or `GenerateDisposableToken`, you can provide
11+
/// data serialized as a `String` in the `token_id` field.
12+
fn token_metadata(_: Vec<u8>) -> Json<Response> {
13+
let maybe_token_metadata = momento_functions_host::web_extensions::token_metadata();
14+
if let Some(metadata) = maybe_token_metadata {
15+
Json(Response {
16+
message: format!("Token metadata provided: {}", metadata),
17+
})
18+
} else {
19+
Json(Response {
20+
message: "No metadata provided, try invoking with a Momento key that was generated with a populated 'token_id' field".to_string(),
21+
})
22+
}
23+
}

0 commit comments

Comments
 (0)