fastapi-mqtt
fastapi-mqtt copied to clipboard
Using Pydantic models for messages
Are there any plans to make this library more like FastAPI with regard to tightly defined models and being more opinionated about the default way to do things? If so, where would we start?
I'm thinking maybe:
- Moving
on_connect
,on_message
and so on to an advanced example rather than the main one - Adding a PayloadModel to the the subscribe decorator.
- Add a decorator (@topics?) for outbound traffic, with the model as a hint in the payload field
- Adding an error decorator, that is called when an exception is raised when constructing
- A decorator for a Last Will and Testament function
- A router class so the code can be nicely split up into different files, like in FastAPI
- Integrate support for encoding/decoding Pydantic models to lightweight transport formats rather than json (bencoding, bson, protobuf, cson, asn.1 or cap'n proto?)
But I'm a noob here. Would there be any love for the models stuff? And where's best to start?
you can split your code in multiple files as you do in fastapi https://fastapi.tiangolo.com/tutorial/bigger-applications/ you need to make functions for publishing and subscribing the event
I want to tightly define types of messages sent to and from topics using pydantic basemodels.
I want to tightly define types of messages sent to and from topics using pydantic basemodels.
Can you show what exactly you want?
Not sure, something like this maybe?
class MyItem(BaseModel):
value: int
@mq.subscribe("topic")
async def topic(item: MyItem):
print(f"item has a value of {item.value}")