From b746d0b402df0f86d138fa635ff0156aa577d5cb Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 5 Sep 2023 13:03:30 +0200 Subject: [PATCH] Moved `get_message_response` to `platypush.utils`. It is general-purpose enough to be used by all the integrations, not only by the HTTP backend. --- platypush/backend/http/app/utils/__init__.py | 3 +-- platypush/backend/http/app/utils/bus.py | 25 +------------------- platypush/utils/__init__.py | 23 ++++++++++++++++++ 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/platypush/backend/http/app/utils/__init__.py b/platypush/backend/http/app/utils/__init__.py index cad44b269..ab19ce73d 100644 --- a/platypush/backend/http/app/utils/__init__.py +++ b/platypush/backend/http/app/utils/__init__.py @@ -4,7 +4,7 @@ from .auth import ( authenticate_user_pass, get_auth_status, ) -from .bus import bus, get_message_response, send_message, send_request +from .bus import bus, send_message, send_request from .logger import logger from .routes import ( get_http_port, @@ -25,7 +25,6 @@ __all__ = [ 'get_http_port', 'get_ip_or_hostname', 'get_local_base_url', - 'get_message_response', 'get_remote_base_url', 'get_routes', 'get_streaming_routes', diff --git a/platypush/backend/http/app/utils/bus.py b/platypush/backend/http/app/utils/bus.py index 6571599e9..72f85658f 100644 --- a/platypush/backend/http/app/utils/bus.py +++ b/platypush/backend/http/app/utils/bus.py @@ -1,11 +1,9 @@ -from redis import Redis - from platypush.bus.redis import RedisBus from platypush.config import Config from platypush.context import get_backend from platypush.message import Message from platypush.message.request import Request -from platypush.utils import get_redis_conf, get_redis_queue_name_by_message +from platypush.utils import get_redis_conf, get_message_response from .logger import logger @@ -67,24 +65,3 @@ def send_request(action, wait_for_response=True, **kwargs): msg['args'] = kwargs return send_message(msg, wait_for_response=wait_for_response) - - -def get_message_response(msg): - """ - Get the response to the given message. - - :param msg: The message to get the response for. - :return: The response to the given message. - """ - redis = Redis(**bus().redis_args) - redis_queue = get_redis_queue_name_by_message(msg) - if not redis_queue: - return None - - response = redis.blpop(redis_queue, timeout=60) - if response and len(response) > 1: - response = Message.build(response[1]) - else: - response = None - - return response diff --git a/platypush/utils/__init__.py b/platypush/utils/__init__.py index 45ffbedb9..52516221a 100644 --- a/platypush/utils/__init__.py +++ b/platypush/utils/__init__.py @@ -662,4 +662,27 @@ def is_root() -> bool: return os.getuid() == 0 +def get_message_response(msg): + """ + Get the response to the given message. + + :param msg: The message to get the response for. + :return: The response to the given message. + """ + from platypush.message import Message + + redis = get_redis() + redis_queue = get_redis_queue_name_by_message(msg) + if not redis_queue: + return None + + response = redis.blpop(redis_queue, timeout=60) + if response and len(response) > 1: + response = Message.build(response[1]) + else: + response = None + + return response + + # vim:sw=4:ts=4:et: