Skip to content

Commit 0840b42

Browse files
feat: added Json flag to allow more native type usage (#4)
* "Added as_json flag" * "When as_json is given, allows the return of a javascript object instead of a uint8 array" * "Updated Readme, changed as_json to as_dict" * fixes * version --------- Co-authored-by: idanasulinStrech <[email protected]>
1 parent 3b235d5 commit 0840b42

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ def event_handler(msg_payload, msg_headers, inputs):
7676
return bytes(json.dumps(as_json), encoding='utf-8'), msg_headers
7777
```
7878

79+
Instead of taking `msg_payload` as a bytes object, the as_dict flag can be used to have the JSON parsed to a dictionary.
80+
81+
```python
82+
import json
83+
import base64
84+
from memphis import create_function
85+
86+
def handler(event, context): # The name of this file and this function should match the handler field in the memphis.yaml file in the following format <file name>.<function name>
87+
return create_function(event, event_handler = event_handler, as_dict=True)
88+
89+
def event_handler(msg_payload, msg_headers, inputs):
90+
msg_payload['modified'] = True
91+
92+
return msg_payload, msg_headers
93+
```
94+
7995
Memphis Functions support using Async functions through asyncio. When functions are async, set the use_async parameter to true.
8096
```python
8197
import json

memphis/functions.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
def create_function(
66
event,
77
event_handler: callable,
8-
use_async: bool = False
8+
use_async: bool = False,
9+
as_dict: bool = False
910
) -> None:
1011
"""
1112
This function creates a Memphis function and processes events with the passed-in event_handler function.
@@ -46,6 +47,8 @@ def create_function(
4647
The unprocessed message and the exception will be sent to the dead-letter station.
4748
use_async (bool):
4849
When using an async function through asyncio, set this flag to True. This will await the event_handler call instead of calling it directly.
50+
as_dict (bool):
51+
Instead of taking `payload` as a bytes object, the as_dict flag can be used to have the JSON parsed to a dictionary.
4952
5053
Returns:
5154
handler (callable):
@@ -83,11 +86,18 @@ async def handler(event):
8386
for message in event["messages"]:
8487
try:
8588
payload = base64.b64decode(bytes(message['payload'], encoding='utf-8'))
89+
if as_dict:
90+
payload = str(payload, 'utf-8')
91+
payload = json.loads(payload)
92+
8693
if use_async:
8794
processed_message, processed_headers = await event_handler(payload, message['headers'], event["inputs"])
8895
else:
8996
processed_message, processed_headers = event_handler(payload, message['headers'], event["inputs"])
9097

98+
if as_dict:
99+
processed_message = bytes(json.dumps(processed_message), encoding='utf-8')
100+
91101
if isinstance(processed_message, bytes) and isinstance(processed_headers, dict):
92102
processed_events["messages"].append({
93103
"headers": processed_headers,

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
setup(
99
name="memphis-functions",
1010
packages=["memphis"],
11-
version="1.0.0",
11+
version="1.0.1",
1212
license="Apache-2.0",
1313
description="A powerful messaging platform for modern developers",
1414
long_description=long_description,
@@ -17,7 +17,7 @@
1717
author="Memphis.dev",
1818
author_email="[email protected]",
1919
url="https://github.com/memphisdev/memphis-functions.py",
20-
download_url="https://github.com/memphisdev/memphis-functions.py/archive/refs/tags/1.0.0.tar.gz",
20+
download_url="https://github.com/memphisdev/memphis-functions.py/archive/refs/tags/1.0.1.tar.gz",
2121
keywords=["message broker", "devtool", "streaming", "data"],
2222
install_requires=[""],
2323
classifiers=[

version-beta.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.0
1+
1.0.1

version.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.0
1+
1.0.1

0 commit comments

Comments
 (0)