Skip to content

Commit

Permalink
refactor(python-sdk): rename variable, improve duplicate id error mes…
Browse files Browse the repository at this point in the history
…sage
  • Loading branch information
ewanharris committed Dec 18, 2024
1 parent 225735f commit 783f5a7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
10 changes: 5 additions & 5 deletions config/clients/python/template/src/client/client.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -643,17 +643,17 @@ class OpenFgaClient:
elif isinstance(options["max_batch_size"], int):
max_batch_size = options["max_batch_size"]

check_to_id: dict[str, ClientBatchCheckItem]= {}
id_to_check: dict[str, ClientBatchCheckItem]= {}
def track_and_transform(checks):
transformed = []
for check in checks:
if check.correlation_id is None:
check.correlation_id = str(uuid.uuid4())

if check.correlation_id in check_to_id:
raise FgaValidationException("Duplicate correlation_id provided")
if check.correlation_id in id_to_check:
raise FgaValidationException(f"Duplicate correlation_id ({check.correlation_id}) provided")

check_to_id[check.correlation_id] = check
id_to_check[check.correlation_id] = check

transformed.append(construct_batch_item(check))
return transformed
Expand All @@ -666,7 +666,7 @@ class OpenFgaClient:
result = []
sem = asyncio.Semaphore(max_parallel_requests)
def map_response(id, result):
check = check_to_id[id]
check = id_to_check[id]
return ClientBatchCheckSingleResponse(
allowed=result.allowed,
request=check,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,18 +615,18 @@ class OpenFgaClient:
elif isinstance(options["max_batch_size"], int):
max_batch_size = options["max_batch_size"]

check_to_id: dict[str, ClientBatchCheckItem] = {}
id_to_check: dict[str, ClientBatchCheckItem] = {}

def track_and_transform(checks):
transformed = []
for check in checks:
if check.correlation_id is None:
check.correlation_id = str(uuid.uuid4())

if check.correlation_id in check_to_id:
raise FgaValidationException("Duplicate correlation_id provided")
if check.correlation_id in id_to_check:
raise FgaValidationException(f"Duplicate correlation_id ({check.correlation_id}) provided")

check_to_id[check.correlation_id] = check
id_to_check[check.correlation_id] = check

transformed.append(construct_batch_item(check))
return transformed
Expand All @@ -639,7 +639,7 @@ class OpenFgaClient:
]

def map_response(id, result):
check = check_to_id[id]
check = id_to_check[id]
return ClientBatchCheckSingleResponse(
allowed=result.allowed,
request=check,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,7 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
"result": {
"fake-uuid": {
"error": {
"inputError": "validation_error",
"input_error": "validation_error",
"message": "type 'doc' not found"
}
}
Expand Down Expand Up @@ -2046,11 +2046,15 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
configuration = self.configuration
configuration.store_id = store_id
async with OpenFgaClient(configuration) as api_client:
with self.assertRaises(FgaValidationException):
with self.assertRaises(FgaValidationException) as error:
await api_client.batch_check(
body=body,
options={"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
)
self.assertEqual(
"Duplicate correlation_id (1) provided",
str(error.exception)
)
await api_client.close()

@patch.object(rest.RESTClientObject, "request")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1954,7 +1954,7 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
"result": {
"fake-uuid": {
"error": {
"inputError": "validation_error",
"input_error": "validation_error",
"message": "type 'doc' not found"
}
}
Expand Down Expand Up @@ -2092,11 +2092,15 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
configuration = self.configuration
configuration.store_id = store_id
with OpenFgaClient(configuration) as api_client:
with self.assertRaises(FgaValidationException):
with self.assertRaises(FgaValidationException) as error:
api_client.batch_check(
body=body,
options={"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
)
self.assertEqual(
"Duplicate correlation_id (1) provided",
str(error.exception)
)
api_client.close()

@patch.object(rest.RESTClientObject, "request")
Expand Down

0 comments on commit 783f5a7

Please sign in to comment.