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.
This commit is contained in:
Fabio Manganiello 2023-09-05 13:03:30 +02:00
parent f1acff00e9
commit b746d0b402
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
3 changed files with 25 additions and 26 deletions

View file

@ -4,7 +4,7 @@ from .auth import (
authenticate_user_pass, authenticate_user_pass,
get_auth_status, 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 .logger import logger
from .routes import ( from .routes import (
get_http_port, get_http_port,
@ -25,7 +25,6 @@ __all__ = [
'get_http_port', 'get_http_port',
'get_ip_or_hostname', 'get_ip_or_hostname',
'get_local_base_url', 'get_local_base_url',
'get_message_response',
'get_remote_base_url', 'get_remote_base_url',
'get_routes', 'get_routes',
'get_streaming_routes', 'get_streaming_routes',

View file

@ -1,11 +1,9 @@
from redis import Redis
from platypush.bus.redis import RedisBus from platypush.bus.redis import RedisBus
from platypush.config import Config from platypush.config import Config
from platypush.context import get_backend from platypush.context import get_backend
from platypush.message import Message from platypush.message import Message
from platypush.message.request import Request 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 from .logger import logger
@ -67,24 +65,3 @@ def send_request(action, wait_for_response=True, **kwargs):
msg['args'] = kwargs msg['args'] = kwargs
return send_message(msg, wait_for_response=wait_for_response) 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

View file

@ -662,4 +662,27 @@ def is_root() -> bool:
return os.getuid() == 0 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: # vim:sw=4:ts=4:et: