sabuhish / fastapi-mqtt

fastapi-mqtt is extension for MQTT protocol
https://sabuhish.github.io/fastapi-mqtt/
MIT License
267 stars 42 forks source link

Using Pydantic models for messages #51

Open bitplane opened 1 year ago

bitplane commented 1 year ago

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:

But I'm a noob here. Would there be any love for the models stuff? And where's best to start?

kalmastenitin commented 1 year ago

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

bitplane commented 1 year ago

I want to tightly define types of messages sent to and from topics using pydantic basemodels.

kalmastenitin commented 1 year ago

I want to tightly define types of messages sent to and from topics using pydantic basemodels.

Can you show what exactly you want?

bitplane commented 1 year ago

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}")