Support specify webhook headers

This commit is contained in:
shamoon 2024-10-26 22:09:47 -07:00
parent c4d59e0e77
commit 8d76654ff6
No known key found for this signature in database
7 changed files with 52 additions and 3 deletions

View File

@ -329,6 +329,7 @@
<pngx-input-text i18n-title title="Notification body" formControlName="notification_body" [error]="error?.actions?.[i]?.notification_body"></pngx-input-text>
<pngx-input-text i18n-title title="Notification emails" formControlName="notification_destination_emails" [error]="error?.actions?.[i]?.notification_destination_emails"></pngx-input-text>
<pngx-input-text i18n-title title="Notification url" formControlName="notification_destination_url" [error]="error?.actions?.[i]?.notification_destination_url"></pngx-input-text>
<pngx-input-text i18n-title title="Notification headers" formControlName="notification_destination_url_headers" [error]="error?.actions?.[i]?.notification_destination_url_headers"></pngx-input-text>
<pngx-input-switch i18n-title title="Notification include document" formControlName="notification_include_document"></pngx-input-switch>
</div>
</div>

View File

@ -414,6 +414,9 @@ export class WorkflowEditDialogComponent
notification_destination_url: new FormControl(
action.notification_destination_url
),
notification_destination_url_headers: new FormControl(
action.notification_destination_url_headers
),
notification_include_document: new FormControl(
action.notification_include_document
),
@ -522,6 +525,7 @@ export class WorkflowEditDialogComponent
notification_body: null,
notification_destination_emails: null,
notification_destination_url: null,
notification_destination_url_headers: null,
notification_include_document: null,
}
this.object.actions.push(action)

View File

@ -72,5 +72,7 @@ export interface WorkflowAction extends ObjectWithId {
notification_destination_url?: string
notification_destination_url_headers?: string
notification_include_document?: boolean
}

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(