Handle the case where the condition is a serialized dictionary

This commit is contained in:
Fabio Manganiello 2022-08-31 01:55:21 +02:00
parent db45d7ecbf
commit 67413c02cd
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -6,6 +6,7 @@ from flask.wrappers import Response
from platypush.backend.http.app import template_folder from platypush.backend.http.app import template_folder
from platypush.backend.http.app.utils import logger, send_message from platypush.backend.http.app.utils import logger, send_message
from platypush.config import Config from platypush.config import Config
from platypush.event.hook import EventCondition
from platypush.message.event.http.hook import WebhookEvent from platypush.message.event.http.hook import WebhookEvent
@ -17,6 +18,16 @@ __routes__ = [
] ]
def matches_condition(event: WebhookEvent, hook):
if isinstance(hook, dict):
condition = hook.get('condition', {})
else:
condition = hook.condition
condition = EventCondition.build(condition)
return event.matches_condition(condition)
@hook.route( @hook.route(
'/hook/<hook_name>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'] '/hook/<hook_name>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS']
) )
@ -43,10 +54,7 @@ def hook_route(hook_name):
matching_hooks = [ matching_hooks = [
hook hook
for hook in Config.get_event_hooks().values() for hook in Config.get_event_hooks().values()
if hook.condition.type == WebhookEvent if matches_condition(event, hook)
and hook.condition.args.get('hook') == hook_name
and request.method.lower()
== hook.condition.args.get('method', request.method).lower()
] ]
try: try: