Skip to content

Commit

Permalink
[#2566] Persist originating service on zgw clients
Browse files Browse the repository at this point in the history
To support multiple zgw backends, we frequently need to determine
which backend to query for which resource. As a first step to
keeping better track of where ZGW objects come from, in this
commit we persist the zgw service used to configure a client
on the client object, so that we can later fetch and persist
this service to facilitate easier target backend resolution.
  • Loading branch information
swrichards committed Jun 18, 2024
1 parent fefe559 commit dd3f053
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/open_inwoner/openzaak/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from zgw_consumers.api_models.catalogi import Catalogus
from zgw_consumers.api_models.constants import RolOmschrijving, RolTypes
from zgw_consumers.client import build_client
from zgw_consumers.models import Service
from zgw_consumers.service import pagination_helper

from open_inwoner.openzaak.api_models import InformatieObject
Expand All @@ -39,7 +40,18 @@
logger = logging.getLogger(__name__)


class ZakenClient(APIClient):
class ZgwAPIClient(APIClient):
"""A client for interacting with ZGW services."""

configured_from: Service
"""The Service instance used to configure this client."""

def __init__(self, *args, **kwargs):
self.configured_from = kwargs.pop("configured_from")
super().__init__(*args, **kwargs)


class ZakenClient(ZgwAPIClient):
def fetch_cases(
self,
user_bsn: str | None = None,
Expand Down Expand Up @@ -424,7 +436,7 @@ def connect_case_with_document(
return data


class CatalogiClient(APIClient):
class CatalogiClient(ZgwAPIClient):
# not cached because only used by tools,
# and because caching (stale) listings can break lookups
def fetch_status_types_no_cache(self, case_type_url: str) -> list[StatusType]:
Expand Down Expand Up @@ -583,7 +595,7 @@ def fetch_single_information_object_type(
return information_object_type


class DocumentenClient(APIClient):
class DocumentenClient(ZgwAPIClient):
def _fetch_single_information_object(
self, *, url: str | None = None, uuid: str | None = None
) -> InformatieObject | None:
Expand Down Expand Up @@ -645,7 +657,7 @@ def upload_document(
return data


class FormClient(APIClient):
class FormClient(ZgwAPIClient):
def fetch_open_submissions(self, bsn: str) -> list[OpenSubmission]:
if not bsn:
return []
Expand Down Expand Up @@ -696,7 +708,9 @@ def _build_zgw_client(type_: ZgwClientType) -> ZgwClient | None:
if client_class := services_to_client_mapping.get(type_):
service = getattr(config, f"{type_}_service")
if service:
client = build_client(service, client_factory=client_class)
client = build_client(
service, client_factory=client_class, configured_from=service
)
return client

logger.warning("no service defined for %s", type_)
Expand Down
28 changes: 28 additions & 0 deletions src/open_inwoner/openzaak/tests/test_clients.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.test import TestCase

from zgw_consumers.constants import APITypes
from zgw_consumers.models import Service

from open_inwoner.openzaak.clients import (
build_catalogi_client,
build_documenten_client,
build_forms_client,
build_zaken_client,
)
from open_inwoner.openzaak.tests.factories import ZGWApiGroupConfigFactory


class ClientFactoryTestCase(TestCase):
def setUp(self):
ZGWApiGroupConfigFactory()

def test_originating_service_is_persisted_on_client(self):
for factory, api_type in (
(build_forms_client, APITypes.orc),
(build_zaken_client, APITypes.zrc),
(build_documenten_client, APITypes.drc),
(build_catalogi_client, APITypes.ztc),
):
client = factory()
self.assertIsInstance(client.configured_from, Service)
self.assertEqual(client.configured_from.api_type, api_type)

0 comments on commit dd3f053

Please sign in to comment.