From 6a9f15d0dba0be8f43995838f6d470ca61a33d8d Mon Sep 17 00:00:00 2001 From: Katherine Bargar Date: Thu, 6 Nov 2025 20:45:56 +0000 Subject: [PATCH 1/2] Handle case when totalRecords is the first key in the json --- src/ldlite/_folio.py | 12 ++++++++++-- tests/test_cases/base.py | 7 +++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/ldlite/_folio.py b/src/ldlite/_folio.py index c7f8046..fed9309 100644 --- a/src/ldlite/_folio.py +++ b/src/ldlite/_folio.py @@ -36,7 +36,7 @@ class FolioClient: def __init__(self, params: FolioParams): self._client_factory = default_client_factory(params) - def iterate_records( + def iterate_records( # noqa: C901 (to be really fixed in a non-bugfix release) self, path: str, timeout: float, @@ -92,7 +92,15 @@ def iterate_records( record = "" return - key = next(iter(j.keys())) + # folio records usually have two keys + # the actual list and the totalRecords key + # totalRecords isn't always in the same spot + # so we check for keys until we find a good one + # totalRecords as of right now is the only key that could get in the way + # but there may end up with more + keys = iter(j.keys()) + while (key := next(keys)) and key in ["totalRecords"]: + ... nonid_key = ( # Grab the first key if there isn't an id column # because we need it to offset page properly diff --git a/tests/test_cases/base.py b/tests/test_cases/base.py index c908912..d11a6dd 100644 --- a/tests/test_cases/base.py +++ b/tests/test_cases/base.py @@ -33,10 +33,13 @@ def patch_request_get( httpx_post_mock.return_value.cookies.__getitem__.return_value = "token" side_effects = [] - for values in self.values.values(): + for i, values in enumerate(self.values.values()): key = next(iter(values[0].keys())) total_mock = MagicMock() - total_mock.text = f'{{"{key}": [{{"id": ""}}], "totalRecords": 100000}}' + if i % 2 == 0: + total_mock.text = f'{{"{key}": [{{"id": ""}}], "totalRecords": 100000}}' + else: + total_mock.text = f'{{"totalRecords": 100000, "{key}": [{{"id": ""}}]}}' value_mocks = [] for v in values: From da4d2af359a9ddf3fff15af27a8a147e1e70b142 Mon Sep 17 00:00:00 2001 From: Katherine Bargar Date: Thu, 6 Nov 2025 20:48:53 +0000 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b958f7..27bc789 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ Please see [MIGRATING.md](./MIGRATING.md) for information on breaking changes. ### Fixed +- Endpoints which return totalRecords as the first key + ### Changed ### Removed