Firstly, awesome project! I appreciate it being developed and shared.
Tested with v0.7.1 and Python 3.10.4 on Ubuntu 22.04.1 LTS.
I believe the solution is to add data_type argument to self.publish_func() since the default value is "json" for the publish methods:
|
await self.publish_func(reply_to, response) |
Test code and output below:
import json
import traceback
from panini import app as panini_app
app = panini_app.App(
service_name="quickstart-app",
host="127.0.0.1",
port=4222,
)
message = {
"key4": "value1",
"key7": 2,
"key3": 3.024444412342342342,
"key1": [1, 2, 3, 4],
"key6": {"1": 1, "2": 2, "3": 3, "4": 4, "5": 5},
"key5": {"subkey1": "1", "subkey2": 2, "3": 3, "4": 4, "5": 5},
"key2": None,
}
@app.task()
async def publish_string():
some_string = json.dumps(message, sort_keys=True)
try:
print(f"*** sending request for json")
# default data_type is "json"
response = await app.request(
subject="some.publish.subject.json",
message=message,
timeout=5
)
print(f"response expecting json: type: {type(response)}")
except BaseException as exc:
print(f"****** got exception: {exc}")
try:
print(f"*** sending request for str")
response = await app.request(
subject="some.publish.subject.str",
message=some_string,
data_type=str,
timeout=5
)
print(f"response expecting str: type: {type(response)}")
except BaseException as exc:
print(f"****** got exception: {exc}")
try:
print(f"*** sending request for bytes")
response = await app.request(
subject="some.publish.subject.bytes",
message=some_string.encode(),
data_type=bytes,
timeout=5
)
print(f"response expecting bytes: type: {type(response)}")
except BaseException as exc:
print(f"****** got exception: {exc}")
# default data_type is "json"
@app.listen("some.publish.subject.json")
async def receive_dict(msg):
print(f"request expecting json: type: {type(msg.data)}")
return {"some": "response"}
@app.listen("some.publish.subject.str", data_type=str)
async def receive_string(msg):
print(f"request expecting str: type: {type(msg.data)}")
return '{"some": "response"}'
@app.listen("some.publish.subject.bytes", data_type=bytes)
async def receive_bytes(msg):
print(f"request expecting bytes: type: {type(msg.data)}")
return b'{"some": "response"}'
if __name__ == "__main__":
app.start()
*** sending request for json
request expecting json: type: <class 'dict'>
response expecting json: type: <class 'dict'>
*** sending request for str
request expecting str: type: <class 'str'>
****** got exception: nats: timeout
*** sending request for bytes
request expecting bytes: type: <class 'bytes'>
****** got exception: nats: timeout
^C
...
panini.exceptions.DataTypeError: Expected dict or list but got <class 'str'>
...
panini.exceptions.DataTypeError: Expected dict or list but got <class 'bytes'>
Firstly, awesome project! I appreciate it being developed and shared.
Tested with v0.7.1 and Python 3.10.4 on Ubuntu 22.04.1 LTS.
I believe the solution is to add
data_typeargument toself.publish_func()since the default value is"json"for the publish methods:panini/panini/managers/nats_client.py
Line 394 in b60b5b2
Test code and output below: