Skip to content

Commit

Permalink
[#4534] Use iterators in the Catalogi client
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Jul 19, 2024
1 parent d1d71cb commit f91af3a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
12 changes: 5 additions & 7 deletions src/openforms/contrib/zgw/clients/catalogi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable
from typing import Callable, Iterator

from zgw_consumers.nlx import NLXClient

Expand All @@ -21,17 +21,16 @@ def match_omschrijving(roltypen: list) -> list:


class CatalogiClient(NLXClient):
def get_all_catalogi(self) -> list[dict]:
def get_all_catalogi(self) -> Iterator[dict]:
"""
List all available catalogi, consuming pagination if relevant.
"""
response = self.get("catalogussen")
response.raise_for_status()
data = response.json()
all_data = pagination_helper(self, data)
return list(all_data)
yield from pagination_helper(self, data)

def get_all_informatieobjecttypen(self, *, catalogus: str = "") -> list[dict]:
def get_all_informatieobjecttypen(self, *, catalogus: str = "") -> Iterator[dict]:
"""List all informatieobjecttypen.
:arg catalogus: the catalogus URL the informatieobjecttypen should belong to.
Expand All @@ -42,8 +41,7 @@ def get_all_informatieobjecttypen(self, *, catalogus: str = "") -> list[dict]:
response = self.get("informatieobjecttypen", params=params)
response.raise_for_status()
data = response.json()
all_data = pagination_helper(self, data)
return list(all_data)
yield from pagination_helper(self, data)

def list_statustypen(self, zaaktype: str) -> list[dict]:
query = {"zaaktype": zaaktype}
Expand Down
9 changes: 4 additions & 5 deletions src/openforms/registrations/contrib/objects_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,18 +288,17 @@ def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:

objects_api_group: ObjectsAPIGroupConfig = attrs["objects_api_group"]
with get_catalogi_client(objects_api_group) as catalogi_client:
informatieobjecttypen = catalogi_client.get_all_informatieobjecttypen()
informatieobjecttypen_urls = [
iot["url"] for iot in catalogi_client.get_all_informatieobjecttypen()
]

for field in (
"informatieobjecttype_submission_report",
"informatieobjecttype_submission_csv",
"informatieobjecttype_attachment",
):
url = attrs.get(field)
if url is not None and not any(
informatieobjecttype["url"] == url
for informatieobjecttype in informatieobjecttypen
):
if url is not None and url not in informatieobjecttypen_urls:
raise serializers.ValidationError(
{
field: _(
Expand Down
2 changes: 1 addition & 1 deletion src/openforms/registrations/contrib/zgw_apis/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:

# Run all validations against catalogi API in the same connection pool.
with get_catalogi_client(group_config) as client:
catalogi = client.get_all_catalogi()
catalogi = list(client.get_all_catalogi())

# validate that the zaaktype is in the provided catalogi
zaaktype_url = attrs["zaaktype"]
Expand Down

0 comments on commit f91af3a

Please sign in to comment.