Skip to content

Commit b1feeda

Browse files
committed
refactor: rename variable, improve duplicate id error message
1 parent 0a02932 commit b1feeda

4 files changed

Lines changed: 22 additions & 12 deletions

File tree

openfga_sdk/client/client.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -707,18 +707,20 @@ async def batch_check(self, body: ClientBatchCheckRequest, options=None):
707707
elif isinstance(options["max_batch_size"], int):
708708
max_batch_size = options["max_batch_size"]
709709

710-
check_to_id: dict[str, ClientBatchCheckItem] = {}
710+
id_to_check: dict[str, ClientBatchCheckItem] = {}
711711

712712
def track_and_transform(checks):
713713
transformed = []
714714
for check in checks:
715715
if check.correlation_id is None:
716716
check.correlation_id = str(uuid.uuid4())
717717

718-
if check.correlation_id in check_to_id:
719-
raise FgaValidationException("Duplicate correlation_id provided")
718+
if check.correlation_id in id_to_check:
719+
raise FgaValidationException(
720+
f"Duplicate correlation_id ({check.correlation_id}) provided"
721+
)
720722

721-
check_to_id[check.correlation_id] = check
723+
id_to_check[check.correlation_id] = check
722724

723725
transformed.append(construct_batch_item(check))
724726
return transformed
@@ -734,7 +736,7 @@ def track_and_transform(checks):
734736
sem = asyncio.Semaphore(max_parallel_requests)
735737

736738
def map_response(id, result):
737-
check = check_to_id[id]
739+
check = id_to_check[id]
738740
return ClientBatchCheckSingleResponse(
739741
allowed=result.allowed,
740742
request=check,

openfga_sdk/sync/client/client.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -693,18 +693,20 @@ def batch_check(self, body: ClientBatchCheckRequest, options=None):
693693
elif isinstance(options["max_batch_size"], int):
694694
max_batch_size = options["max_batch_size"]
695695

696-
check_to_id: dict[str, ClientBatchCheckItem] = {}
696+
id_to_check: dict[str, ClientBatchCheckItem] = {}
697697

698698
def track_and_transform(checks):
699699
transformed = []
700700
for check in checks:
701701
if check.correlation_id is None:
702702
check.correlation_id = str(uuid.uuid4())
703703

704-
if check.correlation_id in check_to_id:
705-
raise FgaValidationException("Duplicate correlation_id provided")
704+
if check.correlation_id in id_to_check:
705+
raise FgaValidationException(
706+
f"Duplicate correlation_id ({check.correlation_id}) provided"
707+
)
706708

707-
check_to_id[check.correlation_id] = check
709+
id_to_check[check.correlation_id] = check
708710

709711
transformed.append(construct_batch_item(check))
710712
return transformed
@@ -717,7 +719,7 @@ def track_and_transform(checks):
717719
]
718720

719721
def map_response(id, result):
720-
check = check_to_id[id]
722+
check = id_to_check[id]
721723
return ClientBatchCheckSingleResponse(
722724
allowed=result.allowed,
723725
request=check,

test/client/client_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2415,11 +2415,14 @@ async def test_batch_check_errors_dupe_cor_id(self):
24152415
configuration = self.configuration
24162416
configuration.store_id = store_id
24172417
async with OpenFgaClient(configuration) as api_client:
2418-
with self.assertRaises(FgaValidationException):
2418+
with self.assertRaises(FgaValidationException) as error:
24192419
await api_client.batch_check(
24202420
body=body,
24212421
options={"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
24222422
)
2423+
self.assertEqual(
2424+
"Duplicate correlation_id (1) provided", str(error.exception)
2425+
)
24232426
await api_client.close()
24242427

24252428
@patch.object(rest.RESTClientObject, "request")

test/sync/client/client_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2417,11 +2417,14 @@ def test_batch_check_errors_dupe_cor_id(self):
24172417
configuration = self.configuration
24182418
configuration.store_id = store_id
24192419
with OpenFgaClient(configuration) as api_client:
2420-
with self.assertRaises(FgaValidationException):
2420+
with self.assertRaises(FgaValidationException) as error:
24212421
api_client.batch_check(
24222422
body=body,
24232423
options={"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
24242424
)
2425+
self.assertEqual(
2426+
"Duplicate correlation_id (1) provided", str(error.exception)
2427+
)
24252428
api_client.close()
24262429

24272430
@patch.object(rest.RESTClientObject, "request")

0 commit comments

Comments
 (0)