Skip to content

Commit

Permalink
Merge pull request #47 from maykinmedia/feature/client-changes
Browse files Browse the repository at this point in the history
♻️ Slight changes to get_client and to_internal_data
  • Loading branch information
stevenbal authored Nov 29, 2024
2 parents e5a5ed7 + 83d4d60 commit 0308f58
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
21 changes: 12 additions & 9 deletions vng_api_common/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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


Expand All @@ -56,18 +55,22 @@ 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.
"""
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)
3 changes: 3 additions & 0 deletions vng_api_common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0308f58

Please sign in to comment.