|
6 | 6 | # See https://aboutcode.org for more information about AboutCode FOSS projects. |
7 | 7 | # |
8 | 8 |
|
9 | | -import json |
| 9 | +import logging |
10 | 10 |
|
11 | | -from django.template.defaultfilters import truncatechars |
12 | | - |
13 | | -import requests |
14 | 11 | from django_rq import job |
15 | | -from rest_framework.utils import encoders |
16 | 12 |
|
17 | | -from dje.tasks import logger |
| 13 | +logger = logging.getLogger("dje") |
18 | 14 |
|
19 | 15 |
|
20 | 16 | @job |
21 | | -def deliver_hook_task( |
22 | | - target, payload, instance_id=None, hook_id=None, extra_headers=None, **kwargs |
| 17 | +def deliver_webhook_task( |
| 18 | + webhook_subscription_pk, |
| 19 | + payload_override=None, |
| 20 | + instance_app_label=None, |
| 21 | + instance_model_name=None, |
| 22 | + instance_pk=None, |
23 | 23 | ): |
24 | | - """ |
25 | | - target: the url to receive the payload. |
26 | | - payload: a python primitive data structure |
27 | | - instance_id: a possibly None "trigger" instance ID |
28 | | - hook_id: the ID of defining Hook object |
29 | | - extra_headers: Additional headers such as Authentication ones |
30 | | - """ |
31 | | - session = requests.Session() |
32 | | - |
33 | | - session.headers.update({"Content-Type": "application/json"}) |
34 | | - if extra_headers: |
35 | | - session.headers.update(extra_headers) |
36 | | - |
37 | | - logger.info(f"Delivering Webhook hook_id={hook_id} to target={truncatechars(target, 25)}") |
38 | | - try: |
39 | | - session.post(url=target, data=payload) |
40 | | - except requests.ConnectionError: |
41 | | - return |
42 | | - |
| 24 | + """Deliver a webhook payload to the target URL of the given WebhookSubscription.""" |
| 25 | + from django.apps import apps |
43 | 26 |
|
44 | | -def deliver_hook_wrapper(target, payload, instance, hook): |
45 | | - if hook.extra_payload: |
46 | | - payload.update(hook.extra_payload) |
| 27 | + from notification.models import WebhookSubscription |
47 | 28 |
|
48 | | - # Using ID's instead of objects for proper serialization |
49 | | - kwargs = { |
50 | | - "target": target, |
51 | | - "payload": json.dumps(payload, cls=encoders.JSONEncoder), |
52 | | - "hook_id": hook.id, |
53 | | - } |
54 | | - |
55 | | - if instance: |
56 | | - kwargs["instance_id"] = instance.id |
57 | | - if hook.extra_headers: |
58 | | - kwargs["extra_headers"] = hook.get_extra_headers() |
| 29 | + try: |
| 30 | + webhook_subscription = WebhookSubscription.objects.get(pk=webhook_subscription_pk) |
| 31 | + except WebhookSubscription.DoesNotExist: |
| 32 | + logger.error(f"WebhookSubscription pk={webhook_subscription_pk} not found.") |
| 33 | + return |
59 | 34 |
|
60 | | - deliver_hook_task.delay(**kwargs) |
| 35 | + instance = None |
| 36 | + if instance_app_label and instance_model_name and instance_pk: |
| 37 | + try: |
| 38 | + model_class = apps.get_model(instance_app_label, instance_model_name) |
| 39 | + instance = model_class.objects.get(pk=instance_pk) |
| 40 | + except Exception: |
| 41 | + logger.error( |
| 42 | + f"Instance {instance_app_label}.{instance_model_name} pk={instance_pk} not found." |
| 43 | + ) |
| 44 | + return |
| 45 | + |
| 46 | + webhook_subscription.deliver(instance, payload_override=payload_override) |
0 commit comments