Skip to content

Commit

Permalink
Merge pull request #136 from baoj2010/main
Browse files Browse the repository at this point in the history
Fix the bug of HANDLER is None
  • Loading branch information
JoshYuJump authored Jul 2, 2022
2 parents 97bdfb3 + 3ca945d commit eaec9e7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bali/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from bali.decorators import event_handler, init_handler
from bali.resources import Resource, ModelResource

__version__ = '3.2.1'
__version__ = '3.2.2'


class Schema(BaseModel):
Expand Down
8 changes: 5 additions & 3 deletions bali/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def event_handler(event_type):
# find queue by event_type
def decorator(func):
@functools.wraps(func)
def wrapper(self, body, message):
def wrapper(handler, body, message):
try:
if isinstance(body, str):
body = json.loads(body)
Expand All @@ -175,12 +175,14 @@ def wrapper(self, body, message):
):
event = param.annotation(**body)
break
res = func(self, event or body)
if not handler:
handler = HANDLER
res = func(handler, event or body)
message.ack()
return res
except:
logger.error(traceback.format_exc())
callback = functools.partial(wrapper, HANDLER)
callback = functools.partial(wrapper, None)
register_callback(event_type, callback)
return wrapper

Expand Down
10 changes: 9 additions & 1 deletion tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from kombu import Connection, Exchange, Queue

from bali.core import _settings
from bali.decorators import event_handler
from bali.decorators import event_handler, init_handler
from bali.events import Event, dispatch, handle

amqp_uri = os.getenv('AMQP_SERVER_ADDRESS', default='amqp://127.0.0.1:5672')
Expand Down Expand Up @@ -38,12 +38,20 @@ class TestHandle:
@event_handler(event_type='test0')
def call_test0(self, event: Event):
handle_test_handler_test0(event)
self.instance_fun()
return event

@event_handler(event_type='test1')
def call_test1(self, event):
handle_test_handler_test1(event)

@staticmethod
def instance_fun():
print('instance_fun')


init_handler(TestHandle)


def test_when_message_body_is_str():
body = '{"type":"hello", "payload":""}'
Expand Down

0 comments on commit eaec9e7

Please sign in to comment.