Skip to content

Commit

Permalink
feat: added Json flag to allow more native type usage (#4)
Browse files Browse the repository at this point in the history
* "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 <idan@memphis.dev>
  • Loading branch information
John-Memphis and idanasulin2706 authored Dec 11, 2023
1 parent 3b235d5 commit 0840b42
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ def event_handler(msg_payload, msg_headers, inputs):
return bytes(json.dumps(as_json), encoding='utf-8'), msg_headers
```

Instead of taking `msg_payload` as a bytes object, the as_dict flag can be used to have the JSON parsed to a dictionary.

```python
import json
import base64
from memphis import create_function

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>
return create_function(event, event_handler = event_handler, as_dict=True)

def event_handler(msg_payload, msg_headers, inputs):
msg_payload['modified'] = True

return msg_payload, msg_headers
```

Memphis Functions support using Async functions through asyncio. When functions are async, set the use_async parameter to true.
```python
import json
Expand Down
12 changes: 11 additions & 1 deletion memphis/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
def create_function(
event,
event_handler: callable,
use_async: bool = False
use_async: bool = False,
as_dict: bool = False
) -> None:
"""
This function creates a Memphis function and processes events with the passed-in event_handler function.
Expand Down Expand Up @@ -46,6 +47,8 @@ def create_function(
The unprocessed message and the exception will be sent to the dead-letter station.
use_async (bool):
When using an async function through asyncio, set this flag to True. This will await the event_handler call instead of calling it directly.
as_dict (bool):
Instead of taking `payload` as a bytes object, the as_dict flag can be used to have the JSON parsed to a dictionary.
Returns:
handler (callable):
Expand Down Expand Up @@ -83,11 +86,18 @@ async def handler(event):
for message in event["messages"]:
try:
payload = base64.b64decode(bytes(message['payload'], encoding='utf-8'))
if as_dict:
payload = str(payload, 'utf-8')
payload = json.loads(payload)

if use_async:
processed_message, processed_headers = await event_handler(payload, message['headers'], event["inputs"])
else:
processed_message, processed_headers = event_handler(payload, message['headers'], event["inputs"])

if as_dict:
processed_message = bytes(json.dumps(processed_message), encoding='utf-8')

if isinstance(processed_message, bytes) and isinstance(processed_headers, dict):
processed_events["messages"].append({
"headers": processed_headers,
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
setup(
name="memphis-functions",
packages=["memphis"],
version="1.0.0",
version="1.0.1",
license="Apache-2.0",
description="A powerful messaging platform for modern developers",
long_description=long_description,
Expand All @@ -17,7 +17,7 @@
author="Memphis.dev",
author_email="team@memphis.dev",
url="https://github.com/memphisdev/memphis-functions.py",
download_url="https://github.com/memphisdev/memphis-functions.py/archive/refs/tags/1.0.0.tar.gz",
download_url="https://github.com/memphisdev/memphis-functions.py/archive/refs/tags/1.0.1.tar.gz",
keywords=["message broker", "devtool", "streaming", "data"],
install_requires=[""],
classifiers=[
Expand Down
2 changes: 1 addition & 1 deletion version-beta.conf
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
1.0.1
2 changes: 1 addition & 1 deletion version.conf
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
1.0.1

0 comments on commit 0840b42

Please sign in to comment.