refactor webhook service

This commit is contained in:
hjlarry
2025-10-01 12:46:42 +08:00
parent 9114881623
commit 604651873e
3 changed files with 340 additions and 734 deletions

View File

@ -12,14 +12,17 @@ logger = logging.getLogger(__name__)
def _prepare_webhook_execution(webhook_id: str):
"""Fetch trigger context, extract request data, and validate payload."""
"""Fetch trigger context, extract request data, and validate payload using unified processing."""
webhook_trigger, workflow, node_config = WebhookService.get_webhook_trigger_and_workflow(webhook_id)
webhook_data = WebhookService.extract_webhook_data(webhook_trigger)
validation_result = WebhookService.validate_webhook_request(webhook_data, node_config)
if not validation_result["valid"]:
return webhook_trigger, workflow, node_config, webhook_data, validation_result["error"]
return webhook_trigger, workflow, node_config, webhook_data, None
try:
# Use new unified extraction and validation
webhook_data = WebhookService.extract_and_validate_webhook_data(webhook_trigger, node_config)
return webhook_trigger, workflow, node_config, webhook_data, None
except ValueError as e:
# Fall back to raw extraction for error reporting
webhook_data = WebhookService.extract_webhook_data(webhook_trigger)
return webhook_trigger, workflow, node_config, webhook_data, str(e)
@bp.route("/webhook/<string:webhook_id>", methods=["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"])
@ -55,7 +58,7 @@ def handle_webhook(webhook_id: str):
def handle_webhook_debug(webhook_id: str):
"""Handle webhook debug calls without triggering production workflow execution."""
try:
webhook_trigger, workflow, node_config, webhook_data, error = _prepare_webhook_execution(webhook_id)
webhook_trigger, _, node_config, webhook_data, error = _prepare_webhook_execution(webhook_id)
if error:
return jsonify({"error": "Bad Request", "message": error}), 400