Skip to content

Commit

Permalink
Add code to create a webhook url #504
Browse files Browse the repository at this point in the history
Signed-off-by: Jono Yang <[email protected]>
  • Loading branch information
JonoYang committed Jul 26, 2024
1 parent c674d63 commit e09e8c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions minecode/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from minecode.models import PriorityResourceURI, ResourceURI, ScannableURI
from minecode.permissions import IsScanQueueWorkerAPIUser
from minecode.utils import get_temp_file
from minecode.utils import get_webhook_url


class ResourceURISerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -109,10 +110,15 @@ def get_next_download_url(self, request, *args, **kwargs):
with transaction.atomic():
scannable_uri = ScannableURI.objects.get_next_scannable()
if scannable_uri:
user = self.request.user
# TODO: Create scan index API view
# TODO: Create custom User class and use UUID as id field
webhook_url = get_webhook_url("notifications:send_scan_notification", user.id)
response = {
'scannable_uri_uuid': scannable_uri.uuid,
'download_url': scannable_uri.uri,
'pipelines': scannable_uri.pipelines,
'webhook_url': webhook_url,
}
scannable_uri.scan_status = ScannableURI.SCAN_SUBMITTED
scannable_uri.scan_date = timezone.now()
Expand All @@ -122,6 +128,7 @@ def get_next_download_url(self, request, *args, **kwargs):
'scannable_uri_uuid': '',
'download_url': '',
'pipelines': [],
'webhook_url': '',
}
return Response(response)

Expand Down
14 changes: 14 additions & 0 deletions minecode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import uuid

from django.conf import settings
from django.core import signing
from django.urls import reverse
from django.utils.encoding import force_str

import arrow
Expand Down Expand Up @@ -415,3 +417,15 @@ def __iter__(self):

def next(self):
return self._generator.next()


def get_webhook_url(view_name, user_uuid):
"""
Return a Webhook target URL based on the `user_uuid`.
This URL is used to create notifications for this user.
"""
user_key = signing.dumps(str(user_uuid))
target_url = reverse(view_name, args=[user_key])
site_url = settings.SITE_URL.rstrip("/")
webhook_url = site_url + target_url
return webhook_url

0 comments on commit e09e8c7

Please sign in to comment.