Support specify webhook headers

This commit is contained in:
shamoon
2024-10-26 22:09:47 -07:00
parent c4d59e0e77
commit 8d76654ff6
7 changed files with 52 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
# Generated by Django 5.1.1 on 2024-10-23 17:15
# Generated by Django 5.1.2 on 2024-10-26 19:07
from django.db import migrations
from django.db import models
@@ -40,6 +40,16 @@ class Migration(migrations.Migration):
verbose_name="notification destination url",
),
),
migrations.AddField(
model_name="workflowaction",
name="notification_destination_url_headers",
field=models.JSONField(
blank=True,
help_text="The headers to send with the notification destination URL.",
null=True,
verbose_name="notification destination url headers",
),
),
migrations.AddField(
model_name="workflowaction",
name="notification_include_document",

View File

@@ -1408,6 +1408,13 @@ class WorkflowAction(models.Model):
help_text=_("The destination URL for the notification."),
)
notification_destination_url_headers = models.JSONField(
_("notification destination url headers"),
null=True,
blank=True,
help_text=_("The headers to send with the notification destination URL."),
)
notification_include_document = models.BooleanField(
default=False,
verbose_name=_("include document in notification"),

View File

@@ -1851,6 +1851,7 @@ class WorkflowActionSerializer(serializers.ModelSerializer):
"notification_body",
"notification_destination_emails",
"notification_destination_url",
"notification_destination_url_headers",
"notification_include_document",
]

View File

@@ -1,3 +1,4 @@
import json
import logging
import os
import shutil
@@ -898,7 +899,11 @@ def run_workflows(
)
if action.notification_include_document:
email.attach_file(document.source_path)
email.send()
n_messages = email.send()
logger.debug(
f"Sent {n_messages} notification email(s) to {action.notification_destination_emails}",
extra={"group": logging_group},
)
except Exception as e:
logger.exception(
f"Error occurred sending notification email: {e}",
@@ -911,18 +916,37 @@ def run_workflows(
"message": body,
}
files = None
headers = None
if action.notification_destination_url_headers:
try:
# headers are a JSON object with key-value pairs, needs to be converted to a Mapping[str, str]
header_mapping = json.loads(
action.notification_destination_url_headers,
)
headers = {str(k): str(v) for k, v in header_mapping.items()}
except Exception as e:
logger.error(
f"Error occurred parsing notification destination URL headers: {e}",
extra={"group": logging_group},
)
if action.notification_include_document:
with open(document.source_path, "rb") as f:
files = {"document": f}
httpx.post(
response = httpx.post(
action.notification_destination_url,
data=data,
headers=headers,
files=files,
)
logger.debug(
f"Response from notification destination URL: {response}",
extra={"group": logging_group},
)
else:
httpx.post(
action.notification_destination_url,
data=data,
headers=headers,
)
except Exception as e:
logger.exception(