Skip to content

Commit

Permalink
Merge pull request #15 from maykinmedia/update-zgw-consumers
Browse files Browse the repository at this point in the history
Update zgw consumers
  • Loading branch information
stevenbal authored Oct 24, 2024
2 parents 7abd32e + 2508086 commit aa73035
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 1,003 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python: ['3.8', '3.9', '3.10']
django: ['3.2', '4.1', '4.2']
python: ['3.10', '3.11', '3.12']
django: ['4.2']

name: Run the test suite (Python ${{ matrix.python }}, Django ${{ matrix.django }})

services:
postgres:
image: docker.io/library/postgres:12
image: docker.io/library/postgres:14
env:
POSTGRES_HOST_AUTH_METHOD: trust
ports:
Expand Down Expand Up @@ -61,7 +61,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.8'
python-version: '3.10'

- name: Build sdist and wheel
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/code_quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.9'
python-version: '3.10'
- name: Install dependencies
run: pip install tox
- run: tox
Expand Down
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ notifications-api-common
:Version: 0.2.2
:Source: https://github.com/maykinmedia/notifications-api-common
:Keywords: notifications, REST, API, Common Ground, ZGW
:PythonVersion: 3.9
:PythonVersion: 3.10

|build-status| |code-quality| |black| |coverage| |docs|

Expand All @@ -29,9 +29,9 @@ Installation
Requirements
------------

* Python 3.8 or above
* Python 3.10 or above
* setuptools 30.3.0 or above
* Django 3.2 or newer
* Django 4.2 or newer
* Celery 5.0 or newer setup with one worker deployed


Expand Down
3 changes: 1 addition & 2 deletions notifications_api_common/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from requests.exceptions import RequestException
from solo.admin import SingletonModelAdmin
from zds_client.client import ClientError

from .models import NotificationsConfig, Subscription

Expand All @@ -20,7 +19,7 @@ def register_webhook(modeladmin, request, queryset):

try:
sub.register()
except (ClientError, RequestException) as e:
except RequestException as e:
messages.error(
request,
_(
Expand Down
1 change: 1 addition & 0 deletions notifications_api_common/kanalen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Provide notifications kanaal/exchange classes.
"""

from collections import defaultdict
from typing import Dict, Tuple

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ def create_kanaal(kanaal: str) -> None:
"""
client = NotificationsConfig.get_client()

assert client

# look up the exchange in the registry
_kanaal = next(k for k in KANAAL_REGISTRY if k.label == kanaal)

kanalen = client.list("kanaal", query_params={"naam": kanaal})
kanalen = client.get("kanaal", params={"naam": kanaal})
if kanalen:
raise KanaalExists()

Expand All @@ -35,9 +37,9 @@ def create_kanaal(kanaal: str) -> None:
f"{protocol}://{domain}{reverse('notifications:kanalen')}#{kanaal}"
)

client.create(
client.post(
"kanaal",
{
json={
"naam": kanaal,
"documentatieLink": documentation_url,
"filters": list(_kanaal.kenmerken),
Expand Down
24 changes: 12 additions & 12 deletions notifications_api_common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

from ape_pie.client import APIClient
from solo.models import SingletonModel
from zds_client import Client, ClientAuth
from zgw_consumers.client import ZGWAuth, build_client
from zgw_consumers.constants import APITypes
from zgw_consumers.models import Service

Expand Down Expand Up @@ -56,13 +57,15 @@ def __str__(self):
)

@classmethod
def get_client(cls) -> Optional[Client]:
def get_client(cls) -> Optional[APIClient]:
"""
Construct a client, prepared with the required auth.
"""
config = cls.get_solo()
if config.notifications_api_service:
return config.notifications_api_service.build_client()
return build_client(
config.notifications_api_service, client_factory=APIClient
)
return None


Expand Down Expand Up @@ -110,23 +113,20 @@ def register(self) -> None:
"""
Registers the webhook with the notification component.
"""
assert (
NotificationsConfig.get_solo().notifications_api_service
), "No service for Notifications API configured"
service = NotificationsConfig.get_solo().notifications_api_service
assert service, "No service for Notifications API configured"

client = NotificationsConfig.get_client()
assert client

# This authentication is for the NC to call us. Thus, it's *not* for
# calling the NC to create a subscription.
# TODO should be replaced with `TokenAuth`
# see: maykinmedia/notifications-api-common/pull/1#discussion_r941450384
self_auth = ClientAuth(
client_id=self.client_id,
secret=self.secret,
)
self_auth = ZGWAuth(service)
data = {
"callbackUrl": self.callback_url,
"auth": self_auth.credentials()["Authorization"],
"auth": f"Bearer {self_auth._token}",
"kanalen": [
{
"naam": channel,
Expand All @@ -138,7 +138,7 @@ def register(self) -> None:
}

# register the subscriber
subscriber = client.create("abonnement", data=data)
subscriber = client.post("abonnement", json=data).json()

self._subscription = subscriber["url"]
self.save(update_fields=["_subscription"])
10 changes: 5 additions & 5 deletions notifications_api_common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import requests
from celery import shared_task
from zds_client import ClientError

from .autoretry import add_autoretry_behaviour
from .models import NotificationsConfig
Expand All @@ -27,13 +26,14 @@ def send_notification(self, message: dict) -> None:
return

try:
client.create("notificaties", message)
response = client.post("notificaties", json=message)
response.raise_for_status()
# any unexpected errors should show up in error-monitoring, so we only
# catch ClientError exceptions
except (ClientError, requests.HTTPError) as exc:
# catch HTTPError exceptions
except requests.HTTPError as exc:
logger.warning(
"Could not deliver message to %s",
client.api_root,
client.base_url,
exc_info=exc,
extra={
"notification_msg": message,
Expand Down
14 changes: 7 additions & 7 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,28 @@ keywords = notifications, REST, API, Common Ground, ZGW
classifiers =
Development Status :: 4 - Beta
Framework :: Django
Framework :: Django :: 3.2
Framework :: Django :: 4.1
Framework :: Django :: 4.2
Intended Audience :: Developers
Operating System :: Unix
Operating System :: MacOS
Operating System :: Microsoft :: Windows
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Programming Language :: Python :: 3.12
Topic :: Software Development :: Libraries :: Python Modules

[options]
zip_safe = False
include_package_data = True
packages = find:
install_requires =
django>=3.2.0
django>=4.2.0
django-solo
djangorestframework>=3.12.0
celery
djangorestframework_camel_case>=1.2.0
gemma-zds-client>=0.15.0
zgw-consumers
zgw-consumers>=0.35.1
ape-pie
tests_require =
psycopg2
pytest
Expand All @@ -50,6 +48,7 @@ tests_require =
isort
black
flake8
zgw-consumers[testutils]>=0.35.1

[options.packages.find]
include =
Expand All @@ -68,6 +67,7 @@ tests =
isort
black
flake8
zgw-consumers[testutils]>=0.35.1
pep8 = flake8
coverage = pytest-cov
docs =
Expand Down
4 changes: 2 additions & 2 deletions testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

DEBUG = True

USE_TZ = False

BASE_DIR = os.path.abspath(os.path.dirname(__file__))

SECRET_KEY = "so-secret-i-cant-believe-you-are-looking-at-this"
Expand Down Expand Up @@ -62,5 +64,3 @@
]

ROOT_URLCONF = "testapp.urls"

ZGW_CONSUMERS_CLIENT_CLASS = "zgw_consumers.client.ZGWClient"
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from rest_framework.test import APIClient
from zgw_consumers.constants import APITypes
from zgw_consumers.service import Service
from zgw_consumers.models import Service

from notifications_api_common.models import NotificationsConfig
from testapp import urls # noqa
Expand Down
Loading

0 comments on commit aa73035

Please sign in to comment.