Skip to content
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

Using Pydantic models for messages #51

Open
bitplane opened this issue Mar 2, 2023 · 4 comments
Open

Using Pydantic models for messages #51

bitplane opened this issue Mar 2, 2023 · 4 comments

Comments

@bitplane
Copy link

bitplane commented Mar 2, 2023

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?

@kalmastenitin
Copy link

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
Copy link
Author

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

@kalmastenitin
Copy link

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

Can you show what exactly you want?

@bitplane
Copy link
Author

bitplane commented Mar 31, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants