From 0167e8e85533b57253cf95e5f99490482cb7a97b Mon Sep 17 00:00:00 2001 From: Sidney Richards Date: Wed, 23 Oct 2024 17:35:32 +0200 Subject: [PATCH 1/2] Follow the zgw_consumers APIClient interface --- src/open_inwoner/openklant/models.py | 12 +++++++--- src/open_inwoner/openklant/services.py | 6 +++-- src/openklant2/client.py | 32 ++++++++++++++------------ src/openklant2/tests/helpers.py | 4 ++-- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/open_inwoner/openklant/models.py b/src/open_inwoner/openklant/models.py index 8334940da5..50d49fd0f4 100644 --- a/src/open_inwoner/openklant/models.py +++ b/src/open_inwoner/openklant/models.py @@ -1,7 +1,9 @@ import uuid from dataclasses import dataclass +from typing import Self from urllib.parse import urljoin +from django.core.exceptions import ImproperlyConfigured from django.db import models from django.utils.translation import gettext_lazy as _ @@ -208,8 +210,12 @@ def api_url(self): return urljoin(self.api_root, self.api_path) @classmethod - def from_django_settings(cls): + def from_django_settings(cls) -> Self: from django.conf import settings - if config := getattr(settings, "OPENKLANT2_CONFIG", None): - return cls(**config) + if not (config := getattr(settings, "OPENKLANT2_CONFIG", None)): + raise ImproperlyConfigured( + "Please set OPENKLANT2_CONFIG in your settings to configure OpenKlant2" + ) + + return cls(**config) diff --git a/src/open_inwoner/openklant/services.py b/src/open_inwoner/openklant/services.py index ddf6e33a6d..8f4027c396 100644 --- a/src/open_inwoner/openklant/services.py +++ b/src/open_inwoner/openklant/services.py @@ -154,8 +154,10 @@ class OpenKlant2Service: def __init__(self, config: OpenKlant2Config | None = None): self.config = config or OpenKlant2Config.from_django_settings() self.client = OpenKlant2Client( - api_url=self.config.api_url, - api_token=self.config.api_token, + base_url=self.config.api_url, + request_kwargs={ + "headers": {"Authorization": f"Token {self.config.api_token}"} + }, ) if mijn_vragen_actor := getattr(config, "mijn_vragen_actor", None): self.mijn_vragen_actor = ( diff --git a/src/openklant2/client.py b/src/openklant2/client.py index efa6b5d183..f71780dffb 100644 --- a/src/openklant2/client.py +++ b/src/openklant2/client.py @@ -1,3 +1,5 @@ +from typing import Any + from ape_pie import APIClient from openklant2._resources.actor import ActorResource @@ -10,21 +12,21 @@ from openklant2._resources.partij_identificator import PartijIdentificatorResource -class OpenKlant2Client: - http_client: APIClient +class OpenKlant2Client(APIClient): partij: PartijResource - def __init__(self, api_url: str, api_token: str): - self.http_client = APIClient( - request_kwargs={"headers": {"Authorization": f"Token {api_token}"}}, - base_url=api_url, - ) + def __init__( + self, + base_url: str, + request_kwargs: dict[str, Any] | None = None, + ): + super().__init__(base_url=base_url, request_kwargs=request_kwargs) - self.partij = PartijResource(self.http_client) - self.partij_identificator = PartijIdentificatorResource(self.http_client) - self.digitaal_adres = DigitaalAdresResource(self.http_client) - self.klant_contact = KlantContactResource(self.http_client) - self.onderwerp_object = OnderwerpObjectResource(self.http_client) - self.actor = ActorResource(self.http_client) - self.interne_taak = InterneTaakResource(self.http_client) - self.betrokkene = BetrokkeneResource(self.http_client) + self.partij = PartijResource(self) + self.partij_identificator = PartijIdentificatorResource(self) + self.digitaal_adres = DigitaalAdresResource(self) + self.klant_contact = KlantContactResource(self) + self.onderwerp_object = OnderwerpObjectResource(self) + self.actor = ActorResource(self) + self.interne_taak = InterneTaakResource(self) + self.betrokkene = BetrokkeneResource(self) diff --git a/src/openklant2/tests/helpers.py b/src/openklant2/tests/helpers.py index fd6ef2f110..e9f8079394 100644 --- a/src/openklant2/tests/helpers.py +++ b/src/openklant2/tests/helpers.py @@ -148,8 +148,8 @@ def api_url(self): def client_factory(self): return OpenKlant2Client( - api_url=self.api_url, - api_token=self._api_token, + base_url=self.api_url, + request_kwargs={"headers": {"Authorization": f"Token {self._api_token}"}}, ) def clean_state(self): From 4f586eb808f28295ae7c5c49d7011a6aec834947 Mon Sep 17 00:00:00 2001 From: Sidney Richards Date: Thu, 24 Oct 2024 10:08:20 +0200 Subject: [PATCH 2/2] Add explicit types for all the OpenKlant2 client resources --- src/openklant2/client.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/openklant2/client.py b/src/openklant2/client.py index f71780dffb..1347103f5f 100644 --- a/src/openklant2/client.py +++ b/src/openklant2/client.py @@ -14,6 +14,13 @@ class OpenKlant2Client(APIClient): partij: PartijResource + partij_identificator: PartijIdentificatorResource + digitaal_adres: DigitaalAdresResource + klant_contact: KlantContactResource + onderwerp_object: OnderwerpObjectResource + actor: ActorResource + interne_taak: InterneTaakResource + betrokkene: BetrokkeneResource def __init__( self,