Skip to content

fix: remove ReadAuthorizationModel calls from BatchCheck and writes #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ credentials.json

VERSION.txt
git_push.sh
setup.local.cfg
25 changes: 9 additions & 16 deletions openfga_sdk/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
construct_write_single_response,
)
from openfga_sdk.client.models.write_transaction_opts import WriteTransactionOpts
from openfga_sdk.exceptions import FgaValidationException
from openfga_sdk.exceptions import (
AuthenticationError,
FgaValidationException,
UnauthorizedException,
)
from openfga_sdk.models.assertion import Assertion
from openfga_sdk.models.check_request import CheckRequest
from openfga_sdk.models.contextual_tuple_keys import ContextualTupleKeys
Expand Down Expand Up @@ -178,16 +182,6 @@ def get_authorization_model_id(self):
"""
return self._client_configuration.authorization_model_id

async def _check_valid_api_connection(self, options: dict[str, int | str]):
"""
Checks that a connection with the given configuration can be established
"""
authorization_model_id = self._get_authorization_model_id(options)
if authorization_model_id is not None and authorization_model_id != "":
await self.read_authorization_model(options)
else:
await self.read_latest_authorization_model(options)

#################
# Stores
#################
Expand Down Expand Up @@ -412,6 +406,8 @@ async def _write_single_batch(
ClientWriteRequest(writes=write_batch, deletes=delete_batch), options
)
return [construct_write_single_response(i, True, None) for i in batch]
except (AuthenticationError, UnauthorizedException) as err:
raise err
except Exception as err:
return [construct_write_single_response(i, False, err) for i in batch]

Expand Down Expand Up @@ -496,8 +492,6 @@ async def write(self, body: ClientWriteRequest, options: dict[str, str] = None):
options = set_heading_if_not_set(
options, CLIENT_BULK_REQUEST_ID_HEADER, str(uuid.uuid4())
)
# TODO: this should be run in parallel
await self._check_valid_api_connection(options)

# otherwise, it is not a transaction and it is a batch write requests
writes_response = None
Expand Down Expand Up @@ -595,6 +589,8 @@ async def _single_batch_check(
response=api_response,
error=None,
)
except (AuthenticationError, UnauthorizedException) as err:
raise err
except Exception as err:
return BatchCheckResponse(
allowed=False, request=body, response=None, error=err
Expand All @@ -620,9 +616,6 @@ async def batch_check(
options, CLIENT_BULK_REQUEST_ID_HEADER, str(uuid.uuid4())
)

# TODO: this should be run in parallel
await self._check_valid_api_connection(options)

max_parallel_requests = 10
if options is not None and "max_parallel_requests" in options:
max_parallel_requests = options["max_parallel_requests"]
Expand Down
25 changes: 9 additions & 16 deletions openfga_sdk/sync/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
construct_write_single_response,
)
from openfga_sdk.client.models.write_transaction_opts import WriteTransactionOpts
from openfga_sdk.exceptions import FgaValidationException
from openfga_sdk.exceptions import (
AuthenticationError,
FgaValidationException,
UnauthorizedException,
)
from openfga_sdk.models.assertion import Assertion
from openfga_sdk.models.check_request import CheckRequest
from openfga_sdk.models.contextual_tuple_keys import ContextualTupleKeys
Expand Down Expand Up @@ -178,16 +182,6 @@ def get_authorization_model_id(self):
"""
return self._client_configuration.authorization_model_id

def _check_valid_api_connection(self, options: dict[str, int | str]):
"""
Checks that a connection with the given configuration can be established
"""
authorization_model_id = self._get_authorization_model_id(options)
if authorization_model_id is not None and authorization_model_id != "":
self.read_authorization_model(options)
else:
self.read_latest_authorization_model(options)

#################
# Stores
#################
Expand Down Expand Up @@ -410,6 +404,8 @@ def _write_single_batch(
ClientWriteRequest(writes=write_batch, deletes=delete_batch), options
)
return [construct_write_single_response(i, True, None) for i in batch]
except (AuthenticationError, UnauthorizedException) as err:
raise err
except Exception as err:
return [construct_write_single_response(i, False, err) for i in batch]

Expand Down Expand Up @@ -493,8 +489,6 @@ def write(self, body: ClientWriteRequest, options: dict[str, str] = None):
options = set_heading_if_not_set(
options, CLIENT_BULK_REQUEST_ID_HEADER, str(uuid.uuid4())
)
# TODO: this should be run in parallel
self._check_valid_api_connection(options)

# otherwise, it is not a transaction and it is a batch write requests
writes_response = None
Expand Down Expand Up @@ -584,6 +578,8 @@ def _single_batch_check(
response=api_response,
error=None,
)
except (AuthenticationError, UnauthorizedException) as err:
raise err
except Exception as err:
return BatchCheckResponse(
allowed=False, request=body, response=None, error=err
Expand All @@ -607,9 +603,6 @@ def batch_check(
options, CLIENT_BULK_REQUEST_ID_HEADER, str(uuid.uuid4())
)

# TODO: this should be run in parallel
self._check_valid_api_connection(options)

max_parallel_requests = 10
if options is not None and "max_parallel_requests" in options:
max_parallel_requests = options["max_parallel_requests"]
Expand Down
Loading