Package: fastapi-dapr-helper
FastAPI Dapr HelperFastAPI Dapr Helper is a Python library that facilitates creating subscriptions to Dapr pubsub topics with FastAPI.This library introduces a decorator namedsubscribewhich, in conjunction with a route generator, allows you to easily setup Dapr pubsub topic subscriptions within your FastAPI application. The package also provides aDaprFastAPIclass to manage the subscriptions and generate a subscription route in your FastAPI application.Installationpippipinstallfastapi-dapr-helperPoetrypoetryaddfastapi-dapr-helperSubscribe DecoratorThesubscribedecorator is designed to be used in combination with a route generator. It creates subscriptions to Dapr pubsub topics with FastAPI.fromfastapi_dapr_helper.pubsubimportsubscribe@subscribe(app=app,path="/test",pubsub="test_pubsub",topic="test_topic")deftest_endpoint():return{"message":"test"}In the above example, thesubscribedecorator is used to define a pubsub subscription on thetest_endpointroute.DaprFastAPI ClassTheDaprFastAPIclass is used to handle Dapr subscriptions and the FastAPI application.fromfastapi_dapr_helper.pubsubimportDaprFastAPIapp=FastAPI()dapr=DaprFastAPI()dapr.generate_subscribe_route(app)In this example, theDaprFastAPIclass is used to handle subscriptions and to generate a/dapr/subscriberoute in the FastAPI app.Working ExamplesHere are some working examples of how to use FastAPI Dapr Helper in your project.Working Example with FastAPIimportuvicornfromfastapiimportFastAPIfromfastapi_dapr_helper.pubsubimportsubscribe,DaprFastAPIapp=FastAPI()dapr=DaprFastAPI()@subscribe(app=app,path="/test",pubsub="test_pubsub",topic="test_topic")deftest_endpoint():return{"message":"test"}dapr.generate_subscribe_route(app)uvicorn.run(app,host="0.0.0.0",port=8000)Outputcurl-X'GET'\'http://localhost:8000/dapr/subscribe'\-H'accept: application/json'[{"pubsubname":"test_pubsub","topic":"test_topic","route":"/test","metadata":{}}]Working Example with APIRouterimportuvicornfromfastapiimportFastAPI,APIRouterfromfastapi_dapr_helper.pubsubimportsubscribe,DaprFastAPIapp=FastAPI()router=APIRouter()dapr=DaprFastAPI()@subscribe(app=router,path="/test",pubsub="test_pubsub",topic="test_topic")deftest_endpoint():return{"message":"test"}app.include_router(router,prefix="/api")dapr.generate_subscribe_route(app)uvicorn.run(app,host="0.0.0.0",port=8000)Outputcurl-X'GET'\'http://localhost:8000/dapr/subscribe'\-H'accept: application/json'[{"pubsubname":"test_pubsub","topic":"test_topic","route":"/api/test","metadata":{}}]Functionality OverviewThe main function of FastAPI Dapr Helper is to make it easier to create subscriptions to Dapr pubsub topics within FastAPI applications.
It achieves this by using the decoratorsubscribeto embed subscription information into the OpenAPI schema for the route.
Then theDaprFastAPIclass uses this information to generate a/dapr/subscriberoute in the FastAPI app.The Dapr sidecar will call the/dapr/subscriberoute to register the subscriptions.ContributingI welcome contributions from the community!
Follow the basic Githubflow PR process:Fork the repo on GitHub.Clone the project to your own machine.Commit changes to your own branch.Push your work back up to your fork.Submit a Pull Request so that your changes can be reviewed.Style GuidelinesTLDRblack.We follow stringent style guidelines to ensure the readability and maintainability of our code.
The primary tools used for enforcing these guidelines areflake8andpylint.Please see the pyproject.toml file for the specific configuration of these tools.TestingTesting is a vital part of our development process.
Not only must all tests pass before a change can be merged,
but any new sections of code must also include corresponding tests.
This allows us to ensure the stability and reliability of our codebase.If you are adding a new feature or fixing a bug,
please include detailed tests to demonstrate the correctness of your changes.
These tests will be automatically run when you submit your pull request.
Any pull request that causes existing tests to fail cannot be merged.To facilitate the testing process, we usepytestas our primary testing framework.
Please familiarize yourself withpytestto write effective tests for your contributions.Remember, good tests are as important as the code itself.
They ensure the functionality of the code and prevent regressions.
Thank you for helping maintain the quality of the codebase.ConclusionPlease ensure that your code conforms to our style guidelines before submitting a PR.
If you have any questions, feel free to open an issue for clarification.Thank you for your contribution!
Happy coding!