Giter Site home page Giter Site logo

teamhide / fastapi-event Goto Github PK

View Code? Open in Web Editor NEW
42.0 2.0 2.0 28 KB

Event dispatcher for FastAPI

License: Apache License 2.0

Python 99.69% Shell 0.31%
fastapi event-driven eventbus event-emitter eventstore eventdispatcher fastapi-event asyncio

fastapi-event's Introduction

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.

fastapi-event's People

Contributors

teamhide avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

fastapi-event's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.