From 2a917092c7bd9b3be5feb9aa0a755e63a723fe43 Mon Sep 17 00:00:00 2001 From: baoj2010 Date: Thu, 30 Jun 2022 11:07:41 +0800 Subject: [PATCH 1/3] Fix the bug of HANDLER is None --- bali/decorators.py | 8 ++++---- tests/test_event.py | 10 +++++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/bali/decorators.py b/bali/decorators.py index e2f629a..483a7b1 100644 --- a/bali/decorators.py +++ b/bali/decorators.py @@ -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(body, message): try: if isinstance(body, str): body = json.loads(body) @@ -175,13 +175,13 @@ def wrapper(self, body, message): ): event = param.annotation(**body) break - res = func(self, event or body) + res = func(HANDLER, event or body) message.ack() return res except: logger.error(traceback.format_exc()) - callback = functools.partial(wrapper, HANDLER) - register_callback(event_type, callback) + + register_callback(event_type, wrapper) return wrapper return decorator diff --git a/tests/test_event.py b/tests/test_event.py index 6bf4c64..7fa0400 100644 --- a/tests/test_event.py +++ b/tests/test_event.py @@ -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') @@ -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":""}' From 4b2b52fe7698d05ea1edf2debff10c513e6cbaad Mon Sep 17 00:00:00 2001 From: baoj2010 Date: Thu, 30 Jun 2022 11:09:43 +0800 Subject: [PATCH 2/3] Bump version 3.2.2 --- bali/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bali/__init__.py b/bali/__init__.py index 4afaba9..b932fe3 100644 --- a/bali/__init__.py +++ b/bali/__init__.py @@ -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): From 3ca945d02e0c36d6984b49b2aae547f1cf9d7abf Mon Sep 17 00:00:00 2001 From: baoj2010 Date: Thu, 30 Jun 2022 11:19:52 +0800 Subject: [PATCH 3/3] pass test --- bali/decorators.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/bali/decorators.py b/bali/decorators.py index 483a7b1..3de7f03 100644 --- a/bali/decorators.py +++ b/bali/decorators.py @@ -158,7 +158,7 @@ def event_handler(event_type): # find queue by event_type def decorator(func): @functools.wraps(func) - def wrapper(body, message): + def wrapper(handler, body, message): try: if isinstance(body, str): body = json.loads(body) @@ -175,13 +175,15 @@ def wrapper(body, message): ): event = param.annotation(**body) break - res = func(HANDLER, event or body) + if not handler: + handler = HANDLER + res = func(handler, event or body) message.ack() return res except: logger.error(traceback.format_exc()) - - register_callback(event_type, wrapper) + callback = functools.partial(wrapper, None) + register_callback(event_type, callback) return wrapper return decorator