Skip to content

Commit

Permalink
Merge pull request #1472 from maykinmedia/tasks/2838-ok2-client-zgw-c…
Browse files Browse the repository at this point in the history
…onsumers-compat

[#2838] Make OpenKlant2 client compatible with zgw_consumers logic
  • Loading branch information
alextreme authored Oct 28, 2024
2 parents 2327a68 + 4f586eb commit 40bc625
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
12 changes: 9 additions & 3 deletions src/open_inwoner/openklant/models.py
Original file line number Diff line number Diff line change
@@ -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 _

Expand Down Expand Up @@ -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)
6 changes: 4 additions & 2 deletions src/open_inwoner/openklant/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
39 changes: 24 additions & 15 deletions src/openklant2/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

from ape_pie import APIClient

from openklant2._resources.actor import ActorResource
Expand All @@ -10,21 +12,28 @@
from openklant2._resources.partij_identificator import PartijIdentificatorResource


class OpenKlant2Client:
http_client: APIClient
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, 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)
4 changes: 2 additions & 2 deletions src/openklant2/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 40bc625

Please sign in to comment.