diff --git a/README.md b/README.md index 5272d8b..3fd17ba 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ An event dispatching/handling library for FastAPI, and Starlette. [![](https://github.com/melvinkcx/fastapi-events/actions/workflows/tests.yml/badge.svg?branch=dev&event=push)](https://github.com/melvinkcx/fastapi-events/actions/workflows/tests.yml) +![PyPI - Downloads](https://img.shields.io/pypi/dw/fastapi-events) Features: @@ -16,6 +17,9 @@ Features: * (__>=0.4.0__) supports event chaining: dispatching events within handlers (thank [@ndopj](https://github.com/ndopj) for contributing to the idea) * (__>=0.7.0__) supports OpenTelemetry: see [this section](#opentelemetry-otel-support) for details +* (__>=0.9.0__) supports dependencies in local handlers: see [this section](#using-dependencies-in-local-handler) for details + +If you use or like this project, please consider giving it a star so it can reach more developers. Thanks =) ## Installation @@ -216,6 +220,42 @@ async def handle_all_events(event: Event): pass ``` +#### Using Dependencies in Local Handler + +> new feature in fastapi-events>=0.9.0 + +Dependencies can now be used with local handler. Sub-dependencies are also supported. + +However, dependencies using generator (with `yield` keyword) is not supported yet. I have the intention to support it in the future. + + +```python +# ex: in handlers.py +from fastapi import Depends + +from fastapi_events.handlers.local import local_handler +from fastapi_events.typing import Event + +async def get_db_conn(): + pass # return a DB conn + + +async def get_db_session( + db_conn=Depends(get_db_conn) +): + pass # return a DB session created from `db_conn` + + + +@local_handler.register(event_name="*") +async def handle_all_events( + event: Event, + db_session=Depends(get_db_session) +): + # use the `db_session` here + pass +``` + ### Piping Events To Remote Queues For larger projects, you might have services dedicated to handling events separately. diff --git a/fastapi_events/__init__.py b/fastapi_events/__init__.py index 377baee..4fcc964 100644 --- a/fastapi_events/__init__.py +++ b/fastapi_events/__init__.py @@ -4,7 +4,7 @@ from fastapi_events.handlers.base import BaseEventHandler -__version__ = "0.8.0" +__version__ = "0.9.0" # handlers keeps track of all handlers registered via EventHandlerASGIMiddleware handler_store: Dict[int, Iterable[BaseEventHandler]] = defaultdict(list)