From 78a8324997e69f8f21550eaa8f2ecbb9b4b604d1 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Thu, 28 Nov 2024 10:32:22 +0100 Subject: [PATCH 1/2] :recycle: Slight changes to get_client and to_internal_data * add option to raise exceptions for get_client * no longer make assertions on the response of to_internal_data, because it could be a 204 (empty) response --- vng_api_common/client.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/vng_api_common/client.py b/vng_api_common/client.py index a92c0de2..0b049277 100644 --- a/vng_api_common/client.py +++ b/vng_api_common/client.py @@ -3,10 +3,6 @@ """ import logging -from typing import Any, Optional - -from django.conf import settings -from django.utils.module_loading import import_string from ape_pie import APIClient from requests import JSONDecodeError, RequestException, Response @@ -18,6 +14,10 @@ class ClientError(RuntimeError): pass +class NoServiceConfigured(RuntimeError): + pass + + # TODO: use more approriate method name def to_internal_data(response: Response) -> dict | list | None: try: @@ -33,7 +33,6 @@ def to_internal_data(response: Response) -> dict | list | None: raise raise ClientError(response_json if response_json is not None else {}) from exc - assert response_json return response_json @@ -56,7 +55,9 @@ def request( return super().request(method, url, *args, **kwargs) -def get_client(url: str) -> Client | None: +def get_client( + url: str, raise_exceptions: bool = False, **client_kwargs +) -> Client | None: """ Get a client instance for the given URL. If no suitable client is found, ``None`` is returned. @@ -64,10 +65,12 @@ def get_client(url: str) -> Client | None: from zgw_consumers.client import build_client from zgw_consumers.models import Service - service: Optional[Service] = Service.get_service(url) + service: Service | None = Service.get_service(url) if not service: logger.warning(f"No service configured for {url}") - return None + if raise_exceptions: + raise NoServiceConfigured(f"{url} API should be added to Service model") + return - return build_client(service, client_factory=Client) + return build_client(service, client_factory=Client, **client_kwargs) From 83d4d60ae0dc27c95c917e9fc9b26a214a545a73 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Thu, 28 Nov 2024 10:45:11 +0100 Subject: [PATCH 2/2] :bug: If no Notifications client is configured, ignore checks --- vng_api_common/views.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/vng_api_common/views.py b/vng_api_common/views.py index a8ca9d60..65924d19 100644 --- a/vng_api_common/views.py +++ b/vng_api_common/views.py @@ -183,6 +183,9 @@ def _test_nrc_config(check_autorisaties_subscription=True) -> list: nrc_config = NotificationsConfig.get_solo() nrc_client: Optional[Client] = NotificationsConfig.get_client() + if not nrc_client: + return [((_("NRC"), _("Missing"), False))] + has_nrc_auth = nrc_client.auth is not None if nrc_client else False if not nrc_config.notifications_api_service: