fastapi-event

license pypi pyversions


fastapi-event is event dispatcher for FastAPI framework.

Installation

pip3 install fastapi-event

Usage

Make Event

from fastapi_event import BaseEvent


class TestEvent(BaseEvent):
    async def run(self, parameter=None):
        ...

Inherit BaseEvent and override run() method.

Parameter(optional)

from pydantic import BaseModel


class TestEventParameter(BaseModel):
    id: str
    pw: str

In case of need parameter, you have to inherit BaseModel and set fields.

Middleware

from fastapi import FastAPI
from fastapi_event import EventHandlerMiddleware

app = FastAPI()
app.add_middleware(EventHandlerMiddleware)

EventListener

from fastapi_event import EventListener


@EventListener()
async def test():
    ...

Set @EventListener() decorator on the function that emits the event.

@EventListener(run_at_once=True)

If you pass run_at_once=True, it will execute through asyncio.gather().

Otherwise, they are executed in the order they were stored.

Store event

from fastapi_event import EventListener, event_handler


@EventListener()
async def test():
    await event_handler.store(
        event=TestEvent,
        parameter=TestParameter(id="hide", pw="hide"),  # Optional
    )

Store your event to handler via store() method. (parameter is optional)

An event will be emitted after the function has finished executing.

FastAPI Event

Event dispatcher for FastAPI

FastAPI Event Info

⭐ Stars 15
🔗 Source Code github.com
🕒 Last Update 6 months ago
🕒 Created 7 months ago
🐞 Open Issues 0
➗ Star-Issue Ratio Infinity
😎 Author teamhide