Skip to content

Commit

Permalink
♻️ [#2060] Replace get_paginated_results with pagination_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Jan 30, 2024
1 parent 4ae9a20 commit b7b6345
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ def test_categories_based_on_cases_for_eherkenning_user_with_vestigingsnummer(
self.assertEqual(context["categories"][3], self.category7)

@patch(
"open_inwoner.openzaak.cases.get_paginated_results",
"zgw_consumers.service.pagination_helper",
side_effect=RequestException,
)
def test_categories_fail_to_fetch_cases(self, m):
Expand Down
30 changes: 15 additions & 15 deletions src/open_inwoner/openklant/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from requests import RequestException
from zgw_consumers.api_models.base import factory
from zgw_consumers.service import pagination_helper

from open_inwoner.accounts.models import User
from open_inwoner.kvk.branches import get_kvk_branch_number
Expand All @@ -18,7 +19,7 @@
from open_inwoner.openklant.clients import build_client
from open_inwoner.openklant.models import OpenKlantConfig
from open_inwoner.openzaak.cases import fetch_case_by_url_no_cache
from open_inwoner.utils.api import ClientError, get_paginated_results
from open_inwoner.utils.api import ClientError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -96,16 +97,15 @@ def _fetch_objectcontactmomenten_for_contactmoment(
return []

try:
response = get_paginated_results(
client,
"objectcontactmomenten",
params={"contactmoment": contactmoment.url},
data = client.get(
"objectcontactmomenten", params={"contactmoment": contactmoment.url}
)
all_data = list(pagination_helper(client, data))
except (RequestException, ClientError) as e:
logger.exception("exception while making request", exc_info=e)
return []

object_contact_momenten = factory(ObjectContactMoment, response)
object_contact_momenten = factory(ObjectContactMoment, all_data)

# resolve linked resources
object_mapping = {}
Expand Down Expand Up @@ -153,16 +153,16 @@ def _fetch_klanten_for_bsn(user_bsn: str, *, client=None) -> List[Klant]:
return []

try:
response = get_paginated_results(
client,
data = client.get(
"klanten",
params={"subjectNatuurlijkPersoon__inpBsn": user_bsn},
)
all_data = list(pagination_helper(client, data))
except (RequestException, ClientError) as e:
logger.exception("exception while making request", exc_info=e)
return []

klanten = factory(Klant, response)
klanten = factory(Klant, all_data)

return klanten

Expand All @@ -182,16 +182,16 @@ def _fetch_klanten_for_kvk_or_rsin(
}

try:
response = get_paginated_results(
client,
data = client.get(
"klanten",
params=params,
)
all_data = list(pagination_helper(client, data))
except (RequestException, ClientError) as e:
logger.exception("exception while making request", exc_info=e)
return []

klanten = factory(Klant, response)
klanten = factory(Klant, all_data)

return klanten

Expand Down Expand Up @@ -223,16 +223,16 @@ def _fetch_klantcontactmomenten_for_klant(
return []

try:
response = get_paginated_results(
client,
data = client.get(
"klantcontactmomenten",
params={"klant": klant.url},
)
all_data = list(pagination_helper(client, data))
except (RequestException, ClientError) as e:
logger.exception("exception while making request", exc_info=e)
return []

klanten_contact_moments = factory(KlantContactMoment, response)
klanten_contact_moments = factory(KlantContactMoment, all_data)

# resolve linked resources
for kcm in klanten_contact_moments:
Expand Down
41 changes: 21 additions & 20 deletions src/open_inwoner/openzaak/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from requests import RequestException
from zgw_consumers.api_models.base import factory
from zgw_consumers.api_models.constants import RolOmschrijving, RolTypes
from zgw_consumers.service import pagination_helper

from open_inwoner.utils.api import ClientError, get_paginated_results
from open_inwoner.utils.api import ClientError

from ..utils.decorators import cache as cache_result
from .api_models import Resultaat, Rol, Status, Zaak, ZaakInformatieObject
Expand All @@ -22,17 +23,16 @@


@cache_result(
"cases:{user_bsn}:{max_cases}:{identificatie}",
"cases:{user_bsn}:{max_requests}:{identificatie}",
timeout=settings.CACHE_ZGW_ZAKEN_TIMEOUT,
)
def fetch_cases(
user_bsn: str, max_cases: Optional[int] = 100, identificatie: Optional[str] = None
user_bsn: str, max_requests: Optional[int] = 1, identificatie: Optional[str] = None
) -> List[Zaak]:
"""
retrieve cases for particular user with allowed confidentiality level
:param:max_cases - used to limit the number of requests to list_zaken resource. The default
value = 100, which means only one 1 request
:param:max_requests - used to limit the number of requests to list_zaken resource.
:param:identificatie - used to filter the cases by a specific identification
"""
client = build_client("zaak")
Expand All @@ -50,37 +50,39 @@ def fetch_cases(
params.update({"identificatie": identificatie})

try:
response = get_paginated_results(
client,
data = client.get(
"zaken",
minimum=max_cases,
params=params,
headers=CRS_HEADERS,
)
all_data = list(
pagination_helper(
client, data, max_requests=max_requests, headers=CRS_HEADERS
)
)
except (RequestException, ClientError) as e:
logger.exception("exception while making request", exc_info=e)
return []

cases = factory(Zaak, response)
cases = factory(Zaak, all_data)

return cases


@cache_result(
"cases:{kvk_or_rsin}:{vestigingsnummer}:{max_cases}:{zaak_identificatie}",
"cases:{kvk_or_rsin}:{vestigingsnummer}:{max_requests}:{zaak_identificatie}",
timeout=settings.CACHE_ZGW_ZAKEN_TIMEOUT,
)
def fetch_cases_by_kvk_or_rsin(
kvk_or_rsin: Optional[str],
max_cases: Optional[int] = 100,
max_requests: Optional[int] = 1,
zaak_identificatie: Optional[str] = None,
vestigingsnummer: Optional[str] = None,
) -> List[Zaak]:
"""
retrieve cases for particular company with allowed confidentiality level
:param max_cases: - used to limit the number of requests to list_zaken resource. The default
value = 100, which means only one 1 request
:param max_requests: - used to limit the number of requests to list_zaken resource.
:param zaak_identificatie: - used to filter the cases by a unique Zaak identification number
:param vestigingsnummer: - used to filter the cases by a vestigingsnummer
"""
Expand Down Expand Up @@ -110,18 +112,17 @@ def fetch_cases_by_kvk_or_rsin(
params.update({"identificatie": zaak_identificatie})

try:
response = get_paginated_results(
client,
data = client.get(
"zaken",
minimum=max_cases,
params=params,
headers=CRS_HEADERS,
)
all_data = list(pagination_helper(client, data, max_requests=max_requests))
except (RequestException, ClientError) as e:
logger.exception("exception while making request", exc_info=e)
return []

cases = factory(Zaak, response)
cases = factory(Zaak, all_data)

return cases

Expand Down Expand Up @@ -258,16 +259,16 @@ def fetch_case_roles(
params["omschrijvingGeneriek"] = role_desc_generic

try:
response = get_paginated_results(
client,
data = client.get(
"rollen",
params=params,
)
all_data = list(pagination_helper(client, data))
except (RequestException, ClientError) as e:
logger.exception("exception while making request", exc_info=e)
return []

roles = factory(Rol, response)
roles = factory(Rol, all_data)

# Taiga #961 process eSuite response to apply ignored filter query
if role_desc_generic:
Expand Down
37 changes: 17 additions & 20 deletions src/open_inwoner/openzaak/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from requests import RequestException
from zgw_consumers.api_models.base import factory
from zgw_consumers.api_models.catalogi import Catalogus
from zgw_consumers.service import pagination_helper

from open_inwoner.utils.api import ClientError, get_paginated_results
from open_inwoner.utils.api import ClientError

from ..utils.decorators import cache as cache_result
from .api_models import InformatieObjectType, ResultaatType, StatusType, ZaakType
Expand All @@ -25,16 +26,16 @@ def fetch_status_types_no_cache(case_type_url: str) -> List[StatusType]:
return []

try:
response = get_paginated_results(
client,
data = client.get(
"statustypen",
params={"zaaktype": case_type_url},
)
all_data = list(pagination_helper(client, data))
except (RequestException, ClientError) as e:
logger.exception("exception while making request", exc_info=e)
return []

status_types = factory(StatusType, response)
status_types = factory(StatusType, all_data)

return status_types

Expand All @@ -48,16 +49,16 @@ def fetch_result_types_no_cache(case_type_url: str) -> List[ResultaatType]:
return []

try:
response = get_paginated_results(
client,
data = client.get(
"resultaattypen",
params={"zaaktype": case_type_url},
)
all_data = list(pagination_helper(client, data))
except (RequestException, ClientError) as e:
logger.exception("exception while making request", exc_info=e)
return []

result_types = factory(ResultaatType, response)
result_types = factory(ResultaatType, all_data)

return result_types

Expand Down Expand Up @@ -114,12 +115,13 @@ def fetch_zaaktypes_no_cache() -> List[ZaakType]:
return []

try:
response = get_paginated_results(client, "zaaktypen")
data = client.get("zaaktypen")
all_data = list(pagination_helper(client, data))
except (RequestException, ClientError) as e:
logger.exception("exception while making request", exc_info=e)
return []

zaak_types = factory(ZaakType, response)
zaak_types = factory(ZaakType, all_data)

return zaak_types

Expand All @@ -141,16 +143,13 @@ def fetch_case_types_by_identification_no_cache(
if catalog_url:
params["catalogus"] = catalog_url

response = get_paginated_results(
client,
"zaaktypen",
params=params,
)
data = client.get("zaaktypen", params=params)
all_data = list(pagination_helper(client, data))
except (RequestException, ClientError) as e:
logger.exception("exception while making request", exc_info=e)
return []

zaak_types = factory(ZaakType, response)
zaak_types = factory(ZaakType, all_data)

return zaak_types

Expand Down Expand Up @@ -197,15 +196,13 @@ def fetch_catalogs_no_cache() -> List[Catalogus]:
return []

try:
response = get_paginated_results(
client,
"catalogussen",
)
data = client.get("catalogussen")
all_data = list(pagination_helper(client, data))
except (RequestException, ClientError) as e:
logger.exception("exception while making request", exc_info=e)
return []

catalogs = factory(Catalogus, response)
catalogs = factory(Catalogus, all_data)

return catalogs

Expand Down
9 changes: 5 additions & 4 deletions src/open_inwoner/openzaak/formapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

from requests import RequestException
from zgw_consumers.api_models.base import Model, factory
from zgw_consumers.service import pagination_helper

from open_inwoner.openzaak.clients import build_client
from open_inwoner.utils.api import ClientError, get_paginated_results
from open_inwoner.utils.api import ClientError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -51,15 +52,15 @@ def fetch_open_submissions(bsn: str) -> List[OpenSubmission]:
return []

try:
response = get_paginated_results(
client,
data = client.get(
"openstaande-inzendingen",
params={"bsn": bsn},
)
all_data = list(pagination_helper(client, data))
except (RequestException, ClientError) as e:
logger.exception("exception while making request", exc_info=e)
return []

results = factory(OpenSubmission, response)
results = factory(OpenSubmission, all_data)

return results
Loading

0 comments on commit b7b6345

Please sign in to comment.