Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add status callback url #28

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies = [
"face-recognition>=1.3.0",
"opencv-python>=4.9.0.80",
"django-celery-results>=2.5.1",
"requests>=2.32.3",
]

[tool.pdm.build]
Expand Down
3 changes: 2 additions & 1 deletion src/hope_dedup_engine/apps/api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.6 on 2024-05-24 11:45
# Generated by Django 5.0.6 on 2024-06-04 13:30

import django.db.models.deletion
import uuid
Expand Down Expand Up @@ -32,6 +32,7 @@ class Migration(migrations.Migration):
("error", models.CharField(blank=True, max_length=255, null=True)),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("notification_url", models.CharField(blank=True, max_length=255, null=True)),
(
"created_by",
models.ForeignKey(
Expand Down
1 change: 1 addition & 0 deletions src/hope_dedup_engine/apps/api/models/deduplication.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class State(models.IntegerChoices):
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True, related_name="+"
)
updated_at = models.DateTimeField(auto_now=True)
notification_url = models.CharField(max_length=255, null=True, blank=True)


class Image(models.Model):
Expand Down
10 changes: 10 additions & 0 deletions src/hope_dedup_engine/apps/api/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import requests

from hope_dedup_engine.apps.api.models import DeduplicationSet


Expand All @@ -7,3 +9,11 @@ def start_processing(_: DeduplicationSet) -> None:

def delete_model_data(_: DeduplicationSet) -> None:
pass


REQUEST_TIMEOUT = 5


def send_notification(deduplication_set: DeduplicationSet) -> None:
if url := deduplication_set.notification_url:
requests.get(url, timeout=REQUEST_TIMEOUT)
28 changes: 28 additions & 0 deletions tests/api/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from unittest.mock import MagicMock

from pytest import fixture, mark
from pytest_mock import MockerFixture

from hope_dedup_engine.apps.api.models import DeduplicationSet
from hope_dedup_engine.apps.api.utils import REQUEST_TIMEOUT, send_notification


@fixture
def requests_get_mock(mocker: MockerFixture) -> MagicMock:
return mocker.patch("hope_dedup_engine.apps.api.utils.requests.get")


@mark.parametrize("deduplication_set__notification_url", ("https://example.com",))
def test_notification_is_sent_when_url_is_set(
requests_get_mock: MagicMock, deduplication_set: DeduplicationSet
) -> None:
send_notification(deduplication_set)
requests_get_mock.assert_called_once_with(deduplication_set.notification_url, timeout=REQUEST_TIMEOUT)


@mark.parametrize("deduplication_set__notification_url", (None,))
def test_notification_is_not_sent_when_url_is_not_set(
requests_get_mock: MagicMock, deduplication_set: DeduplicationSet
) -> None:
send_notification(deduplication_set)
requests_get_mock.assert_not_called()
1 change: 1 addition & 0 deletions tests/extras/testutils/factories/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DeduplicationSetFactory(DjangoModelFactory):
reference_pk = fuzzy.FuzzyText()
external_system = SubFactory(ExternalSystemFactory)
state = DeduplicationSet.State.CLEAN
notification_url = fuzzy.FuzzyText()

class Meta:
model = DeduplicationSet
Expand Down
Loading