Skip to main content

fastapi-event

license pypi pyversions Downloads


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.

from fastapi_event import BaseEvent


class FirstEvent(BaseEvent):
    ORDER = 1  # HERE(Optional)
    
    async def run(self, parameter=None):
        ...


class SecondEvent(BaseEvent):
    ORDER = 2  # HERE(Optional)
    
    async def run(self, parameter=None):
        ...

If you want to determine the order between events, specify ORDER in your event.

Then, regardless of the order in which the events are stored, they will be executed in the order specified in ORDER variable.

However, ORDER does not work when run_at_once=True.

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=False)

If you pass run_at_once=False, it will execute in the order in which store() is called. (or according to the ORDER variable defined in the event)

Otherwise, it will execute through asyncio.gather() to run at once.

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.

Release files for fastapi-event 0.1.3

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-event 0.1.3
File Size Uploaded
fastapi-event-0.1.3.tar.gz 6.3 kB Details

Release files / fastapi-event-0.1.3.tar.gz

Download URL fastapi-event-0.1.3.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
6412100a0f86516aa8638dcdc8dacbadaeec723cbf2e2ebf14adb5f18afe9b44
BLAKE2b-256 checksum
How to use checksums
764e4ce60167f5014b43fdfa813b2edc4f110c82741a58d1807a261cbf825f5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.1.3 This release

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

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