Skip to content

Commit 5abef07

Browse files
remove functions wrapper
1 parent e1faf38 commit 5abef07

File tree

2 files changed

+1
-208
lines changed

2 files changed

+1
-208
lines changed

README.md

Lines changed: 1 addition & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -951,103 +951,4 @@ consumer.destroy()
951951

952952
```python
953953
memphis.is_connected()
954-
```
955-
956-
### Creating a Memphis function
957-
Memphis provides a create_function utility for more easily creatin Memphis Functions.
958-
959-
The user created `event_handler` will be called for every message in the given batch of events. The user's `event_handler` will take in a `msg_payload` as bytes, `msg_headers` as a dict and `inputs` as a dict, and should return a modified version of the payload and headers in the same data types.
960-
961-
The user function should raise an exception if the message processing has failed. If any exception is raised (deliberately or by a failed operation) the message will be sent to the dead letter station.
962-
963-
If the returned modified version of the `msg_payload` or `msg_headers` are returned as `None`, then the message will be skipped and will not be sent to the station or dead letter station.
964-
965-
> Make sure to encode the modified `msg_payload` bytes object with utf-8 encoding!
966-
967-
This example function takes the bytes object `msg_payload` and encodes it into a string so that it may be parsed as JSON.
968-
969-
```python
970-
import json
971-
import base64
972-
from memphis.functions import create_function
973-
974-
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>
975-
return create_function(event, event_handler = event_handler)
976-
977-
def event_handler(msg_payload, msg_headers, inputs):
978-
payload = str(msg_payload, 'utf-8')
979-
as_json = json.loads(payload)
980-
as_json['modified'] = True
981-
982-
return bytes(json.dumps(as_json), encoding='utf-8'), msg_headers
983-
```
984-
985-
If the user would want to have a message that they would want to validate and send to the dead letter station if the validation fails then the user can raise an exception. In the following example, the field `check` is simply a boolean. The following function will send any messages which fail the `check` to the dead letter station.
986-
987-
```python
988-
import json
989-
import base64
990-
from memphis.functions import create_function
991-
992-
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>
993-
return create_function(event, event_handler = event_handler)
994-
995-
def event_handler(msg_payload, msg_headers, inputs):
996-
payload = str(msg_payload, 'utf-8')
997-
as_json = json.loads(payload)
998-
if as_json['check'] == False:
999-
raise Exception("Validation Failed!")
1000-
1001-
return bytes(json.dumps(as_json), encoding='utf-8'), msg_headers
1002-
```
1003-
1004-
If a user would rather just skip the message and not have it be sent to the station or dead letter station, the cuser could instead return `None`, `None`:
1005-
1006-
```python
1007-
import json
1008-
import base64
1009-
from memphis.functions import create_function
1010-
1011-
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>
1012-
return create_function(event, event_handler = event_handler)
1013-
1014-
def event_handler(msg_payload, msg_headers, inputs):
1015-
payload = str(msg_payload, 'utf-8')
1016-
as_json = json.loads(payload)
1017-
if as_json['check'] == False:
1018-
return None, None
1019-
1020-
return bytes(json.dumps(as_json), encoding='utf-8'), msg_headers
1021-
```
1022-
1023-
Lastly, if the user is using another data format like Protocol Buffers, the user may simply decode the `msg_payload` into that format instead of JSON. Assuming we have a .proto definition like this:
1024-
```proto
1025-
syntax = "proto3";
1026-
package protobuf_example;
1027-
1028-
message Message{
1029-
string data_field = 1;
1030-
}
1031-
```
1032-
1033-
We can decode this and get the data_field out like this:
1034-
1035-
```python
1036-
import json
1037-
import base64
1038-
from memphis.functions import create_function
1039-
import message_pb2
1040-
1041-
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>
1042-
return create_function(event, event_handler = event_handler)
1043-
1044-
def event_handler(msg_payload, msg_headers, inputs):
1045-
message = message_pb2.Message()
1046-
message.ParseFromString(base64.b64decode(encoded_str))
1047-
1048-
# Arbitrarily changing the data_field
1049-
message.data_field = "my new data"
1050-
1051-
# SerializeToString returns bytes, which is the type we want
1052-
return message.SerializeToString(), msg_headers
1053-
```
954+
```

memphis/functions.py

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)