Skip to content

Commit

Permalink
Merge pull request #1 from teamhide/develop
Browse files Browse the repository at this point in the history
Fix some codes
  • Loading branch information
teamhide authored Dec 16, 2021
2 parents 54c5568 + f593008 commit e870658
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
11 changes: 11 additions & 0 deletions fastapi_event/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from fastapi_event.base import BaseEvent
from fastapi_event.handler import event_handler
from fastapi_event.listener import EventListener
from fastapi_event.middleware import EventHandlerMiddleware

__all__ = [
"event_handler",
"BaseEvent",
"EventListener",
"EventHandlerMiddleware",
]
9 changes: 7 additions & 2 deletions fastapi_event/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
class InvalidEventTypeException(Exception):
def __init__(self):
super().__init__("event must inherit BaseEvent")
super().__init__("Event must inherit BaseEvent")


class InvalidParameterTypeException(Exception):
def __init__(self):
super().__init__("parameter must inherit BaseModel")
super().__init__("Parameter must inherit BaseModel")


class EmptyContextException(Exception):
def __init__(self):
super().__init__("Event context is empty. check if middleware configured well")
22 changes: 11 additions & 11 deletions fastapi_event/handler.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from contextvars import ContextVar
from typing import Type, Dict, Union, Optional
from typing import Type, Dict, Union, Optional, NoReturn

from pydantic import BaseModel

from fastapi_event.base import BaseEvent
from fastapi_event.exceptions import (
InvalidEventTypeException,
InvalidParameterTypeException,
EmptyContextException,
)

_handler_context: ContextVar[Optional, "EventHandler"] = ContextVar(
"_handler", default=None,
"_handler_context", default=None,
)


Expand Down Expand Up @@ -40,20 +41,19 @@ async def publish(self) -> None:
class EventHandlerMeta(type):
async def store(
self, event: Type[BaseEvent], parameter: BaseModel = None,
):
) -> None:
handler = self._get_event_handler()
await handler.store(event=event, parameter=parameter)

async def publish(self):
async def publish(self) -> None:
handler = self._get_event_handler()
await handler.publish()

def _get_event_handler(self) -> EventHandler:
handler = _handler_context.get()
if not handler:
raise

return handler
def _get_event_handler(self) -> Union[EventHandler, NoReturn]:
try:
return _handler_context.get()
except LookupError:
raise EmptyContextException


class EventHandlerDelegator(metaclass=EventHandlerMeta):
Expand All @@ -68,4 +68,4 @@ def __exit__(self, exc_type, exc_value, traceback):
_handler_context.reset(self.token)


event_handler: EventHandlerMeta = EventHandlerDelegator
event_handler: Type[EventHandlerDelegator] = EventHandlerDelegator

0 comments on commit e870658

Please sign in to comment.