Skip to main content

FastAPI Dapr Helper

FastAPI Dapr Helper is a Python library that facilitates creating subscriptions to Dapr pubsub topics with FastAPI.

This library introduces a decorator named subscribe which, in conjunction with a route generator, allows you to easily setup Dapr pubsub topic subscriptions within your FastAPI application. The package also provides a DaprFastAPI class to manage the subscriptions and generate a subscription route in your FastAPI application.

Installation

pip

pip install fastapi-dapr-helper

Poetry

poetry add fastapi-dapr-helper

Subscribe Decorator

The subscribe decorator is designed to be used in combination with a route generator. It creates subscriptions to Dapr pubsub topics with FastAPI.

from fastapi_dapr_helper.pubsub import subscribe

@subscribe(app=app, path="/test", pubsub="test_pubsub", topic="test_topic")
def test_endpoint():
    return {"message": "test"}

In the above example, the subscribe decorator is used to define a pubsub subscription on the test_endpoint route.

DaprFastAPI Class

The DaprFastAPI class is used to handle Dapr subscriptions and the FastAPI application.

from fastapi_dapr_helper.pubsub import DaprFastAPI

app = FastAPI()
dapr = DaprFastAPI()

dapr.generate_subscribe_route(app)

In this example, the DaprFastAPI class is used to handle subscriptions and to generate a /dapr/subscribe route in the FastAPI app.

Working Examples

Here are some working examples of how to use FastAPI Dapr Helper in your project.

Working Example with FastAPI

import uvicorn
from fastapi import FastAPI
from fastapi_dapr_helper.pubsub import subscribe, DaprFastAPI

app = FastAPI()
dapr = DaprFastAPI()

@subscribe(app=app, path="/test", pubsub="test_pubsub", topic="test_topic")
def test_endpoint():
    return {"message": "test"}

dapr.generate_subscribe_route(app)

uvicorn.run(app, host="0.0.0.0", port=8000)

Output

curl -X 'GET' \
  'http://localhost:8000/dapr/subscribe' \
  -H 'accept: application/json'
[
  {
    "pubsubname": "test_pubsub",
    "topic": "test_topic",
    "route": "/test",
    "metadata": {}
  }
]

Working Example with APIRouter

import uvicorn
from fastapi import FastAPI, APIRouter
from fastapi_dapr_helper.pubsub import subscribe, DaprFastAPI

app = FastAPI()
router = APIRouter()
dapr = DaprFastAPI()

@subscribe(app=router, path="/test", pubsub="test_pubsub", topic="test_topic")
def test_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)

Output

curl -X 'GET' \
  'http://localhost:8000/dapr/subscribe' \
  -H 'accept: application/json'
[
  {
    "pubsubname": "test_pubsub",
    "topic": "test_topic",
    "route": "/api/test",
    "metadata": {}
  }
]

Functionality Overview

The 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 decorator subscribe to embed subscription information into the OpenAPI schema for the route. Then the DaprFastAPI class uses this information to generate a /dapr/subscribe route in the FastAPI app.

The Dapr sidecar will call the /dapr/subscribe route to register the subscriptions.

Contributing

I welcome contributions from the community! Follow the basic Githubflow PR process:

  1. Fork the repo on GitHub.
  2. Clone the project to your own machine.
  3. Commit changes to your own branch.
  4. Push your work back up to your fork.
  5. Submit a Pull Request so that your changes can be reviewed.

Style Guidelines

TLDR

black .

We follow stringent style guidelines to ensure the readability and maintainability of our code. The primary tools used for enforcing these guidelines are flake8 and pylint.

Please see the pyproject.toml file for the specific configuration of these tools.

Testing

Testing 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 use pytest as our primary testing framework. Please familiarize yourself with pytest to 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.

Conclusion

Please 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!

Metadata

Release files for fastapi-dapr-helper 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-dapr-helper 0.1.1
File Size Uploaded Provenance Actions
fastapi-dapr-helper-0.1.1.tar.gz 5.8 kB Without provenance Details for fastapi-dapr-helper-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-dapr-helper 0.1.1
File Interpreter ABI Platform Actions
fastapi_dapr_helper-0.1.1-py3-none-any.whl Python 3 none any Details for fastapi_dapr_helper-0.1.1-py3-none-any.whl

Total release size: 11.1 kB

Release files / fastapi-dapr-helper-0.1.1.tar.gz

Download URL fastapi-dapr-helper-0.1.1.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c4bcd585d701209b27019a933893c726624ba59b10247966b6f19f1649b2fe1f
BLAKE2b-256 checksum
How to use checksums
a9e076b73815b7e741813547c3fee60c549b0a1d532dbf96e2966e1df2e2616b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.1 CPython/3.7.9 Windows/10

Release files / fastapi_dapr_helper-0.1.1-py3-none-any.whl

Download URL fastapi_dapr_helper-0.1.1-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
50e37a62802f0c7b8bba90d5d645a0cde83de82f51f382abdd2f12973ecc42b2
BLAKE2b-256 checksum
How to use checksums
a722dc51975973c359572cf7d816f9b996b5f0139509d861d2f97af83d0920ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.1 CPython/3.7.9 Windows/10

Security for fastapi-dapr-helper 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page