Skip to content

Commit

Permalink
Fix functions context issue (#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
oeway authored Jun 4, 2024
1 parent 27cd403 commit a9bb9f3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
10 changes: 4 additions & 6 deletions docs/serverless-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ It takes two arguments:
- `query_string`: a string (instead of bytes) with the query string
- `raw_path`: a string (instead of bytes) with the raw path
- `headers`: a dictionary (instead of an iterable) with the headers
- `body`: the request body (bytes or arrayBuffer), will be None if empty
* `context`: Contains user and environment related information. Only available if `require_context` is set to True when registering the service.

- `body`: the request body (bytes or arrayBuffer), will be None if empty
- `context`: Contains user and environment related information.

To register the functions, call `api.register_service` with `type="functions"`.

Expand All @@ -27,16 +26,15 @@ await api.register_service({
"type": "functions",
"config": {
"visibility": "public",
"require_context": true
},
"hello-world": async function(event, context) {
"hello-world": async function(event) {
return {
status: 200,
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({message: "Hello World"})
};
},
"index": async function(event, context) {
"index": async function(event) {
return {
status: 200,
body: "Home page"
Expand Down
10 changes: 6 additions & 4 deletions hypha/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
scope["body"] = body or None
func = self.service[func_name]
try:
if self.service.config.get("require_context"):
authorization = scope["headers"].get("authorization")
authorization = scope["headers"].get("authorization")
if authorization:
user_info = parse_token(authorization, allow_anonymouse=True)
result = await func(scope, context={"user": user_info.model_dump()})
user_info = user_info.model_dump()
else:
result = await func(scope)
user_info = None
scope['context'] = {"user": user_info, "_rkwargs": True}
result = await func(scope)
headers = Headers(headers=result.get("headers"))
body = result.get("body")
status = result.get("status", 200)
Expand Down
7 changes: 3 additions & 4 deletions tests/testFunctionsPlugin.imjoy.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@
"type": "functions",
"config": {
"visibility": "public",
"require_context": true
},
"hello-world": async function(event, context) {
"hello-world": async function(event) {
return {
status: 200,
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({message: "Hello World", context})
body: JSON.stringify({message: "Hello World", context: event.context})
};
},
"index": async function(event, context) {
"index": async function(event) {
return {
status: 200,
body: `Home page`
Expand Down

0 comments on commit a9bb9f3

Please sign in to comment.