-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DataTypeError raised when listener's return type is not JSON #238
Comments
Hello Rob |
Do we have a next release date in mind? Thank you! |
Oh, sorry @roblight I missed your last message. |
@roblight, Panini v0.8.0a1 available via pip. This will still take a few weeks before the release of v0.8.0. In the meantime, I suggest you try to solve your problem with v0.8.0a1. I think the solution is a bit more global than you would expect. So, update with the "data_type" parameter that is associated with this issue:
subject = "a.b.c"
message = {"param1": "value1"}
response: bytes = app.request(subject=subject, message=message, response_data_type=bytes)
Let's update your code according to the above:
Let me know if you have any questions about it. |
Also, other updates that are not directly related to the issue, but may be useful for solving the problem with "data_type":
from panini.exceptions import MessageSchemaError
from panini import app as panini_app
app = panini_app.App(
service_name="test_serializer_callable",
host="127.0.0.1",
port=4222,
)
def callable_validator(**message):
if type(message) is not dict:
raise MessageSchemaError("type(data) is not dict")
if "data" not in message:
raise MessageSchemaError("'data' not in message")
if type(message["data"]) is not int:
raise MessageSchemaError("type(message['data']) is not int")
if message["data"] < 0:
raise MessageSchemaError(f"Value of field 'data' is {message['data']} that negative")
message["data"] += 1
return message
@app.listen("test_validator.foo", data_type=callable_validator)
async def publish(msg):
return {"success": True}
@app.listen("test_validator.foo-with-error-cb", data_type=callable_validator)
async def publish(msg):
return {"success": True}
@app.listen("test_validator.check")
async def check(msg):
try:
message = callable_validator(**msg.data)
except MessageSchemaError:
return {"success": False}
return {"success": True}
if __name__ == "__main__":
app.start()
|
Hi @artas728 thank you so much! I plan to review the changes and give feedback ASAP. |
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 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:
The text was updated successfully, but these errors were encountered: