Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36
feat: support write conflicts options#237
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d096fca
feat: implementation along with tests
SoulPancake 32b18f6
feat: support write conflict idempotency
SoulPancake 292c480
fix : tests for default on missing and on dupl
SoulPancake 2a75532
fix: tests fix and ruff fix
SoulPancake 112d178
feat: add readme updates for write conflict changes
SoulPancake 5e4c8bd
feat: changelog and readme changes
SoulPancake d25b2d3
fix: lint errors and update from sdk generator
SoulPancake 6135882
fix: apply suggestions from code review
SoulPancake 647acec
feat: remove API options from readme
SoulPancake aae9ac1
feat: add all changes to tests and client
SoulPancake 7e461d0
feat: address comments, pass settings in opts
SoulPancake 165652b
feat: condense readme text and give options
SoulPancake 2214d45
feat: reformat readme.md
SoulPancake 27521be
feat: minor fmt
SoulPancake 4197704
feat: lint fix add commas
SoulPancake 153261b
fix: value error f string
SoulPancake f0bc591
feat: backward compatibility
SoulPancake 386df2e
Merge branch 'main' into feat/support-write-conflict-options
rhamzeh e531371
Merge branch 'main' into feat/support-write-conflict-options
SoulPancake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1291,7 +1291,7 @@ No authorization required | ||
| Add or delete tuples from the store | ||
| The Write API will transactionally update the tuples for a certain store. Tuples and type definitions allow OpenFGA to determine whether a relationship exists between an object and an user. In the body, `writes` adds new tuples and `deletes` removes existing tuples. When deleting a tuple, any `condition` specified with it is ignored. The API is not idempotent: if, later on, you try to add the same tuple key (even if the `condition` is different), or if you try to delete a non-existing tuple, it will throw an error. The API will not allow you to write tuples such as `document:2021-budget#viewer@document:2021-budget#viewer`, because they are implicit. An `authorization_model_id` may be specified in the body. If it is, it will be used to assert that each written tuple (not deleted) is valid for the model specified. If it is not specified, the latest authorization model ID will be used. ## Example ### Adding relationships To add `user:anne` as a `writer` for `document:2021-budget`, call write API with the following ```json { \"writes\": { \"tuple_keys\": [ { \"user\": \"user:anne\", \"relation\": \"writer\", \"object\": \"document:2021-budget\" } ] }, \"authorization_model_id\": \"01G50QVV17PECNVAHX1GG4Y5NC\" } ``` ### Removing relationships To remove `user:bob` as a `reader` for `document:2021-budget`, call write API with the following ```json { \"deletes\": { \"tuple_keys\": [ { \"user\": \"user:bob\", \"relation\": \"reader\", \"object\": \"document:2021-budget\" } ] } } ``` | ||
| The Write API will transactionally update the tuples for a certain store. Tuples and type definitions allow OpenFGA to determine whether a relationship exists between an object and an user. In the body, `writes` adds new tuples and `deletes` removes existing tuples. When deleting a tuple, any `condition` specified with it is ignored. The API is not idempotent by default: if, later on, you try to add the same tuple key (even if the `condition` is different), or if you try to delete a non-existing tuple, it will throw an error. To allow writes when an identical tuple already exists in the database, set `\"on_duplicate\": \"ignore\"` on the `writes` object. To allow deletes when a tuple was already removed from the database, set `\"on_missing\": \"ignore\"` on the `deletes` object. If a Write request contains both idempotent (ignore) and non-idempotent (error) operations, the most restrictive action (error) will take precedence. If a condition fails for a sub-request with an error flag, the entire transaction will be rolled back. This gives developers explicit control over the atomicity of the requests. The API will not allow you to write tuples such as `document:2021-budget#viewer@document:2021-budget#viewer`, because they are implicit. An `authorization_model_id` may be specified in the body. If it is, it will be used to assert that each written tuple (not deleted) is valid for the model specified. If it is not specified, the latest authorization model ID will be used. ## Example ### Adding relationships To add `user:anne` as a `writer` for `document:2021-budget`, call write API with the following ```json { \"writes\": { \"tuple_keys\": [ { \"user\": \"user:anne\", \"relation\": \"writer\", \"object\": \"document:2021-budget\" } ], \"on_duplicate\": \"ignore\" }, \"authorization_model_id\": \"01G50QVV17PECNVAHX1GG4Y5NC\" } ``` ### Removing relationships To remove `user:bob` as a `reader` for `document:2021-budget`, call write API with the following ```json { \"deletes\": { \"tuple_keys\": [ { \"user\": \"user:bob\", \"relation\": \"reader\", \"object\": \"document:2021-budget\" } ], \"on_missing\": \"ignore\" } } ``` | ||
SoulPancake marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ### Example | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -134,6 +134,17 @@ def options_to_transaction_info( | ||
| return WriteTransactionOpts() | ||
| def options_to_conflict_info( | ||
| options: dict[str, int | str | dict[str, int | str]] | None = None, | ||
| ): | ||
| """ | ||
| Return the conflict info | ||
| """ | ||
| if options is not None and options.get("conflict"): | ||
| return options["conflict"] | ||
| return None | ||
| def _check_errored(response: ClientBatchCheckClientResponse): | ||
| """ | ||
| Helper function to return whether the response is errored | ||
| @@ -520,12 +531,23 @@ async def _write_with_transaction( | ||
| Write or deletes tuples | ||
| """ | ||
| kwargs = options_to_kwargs(options) | ||
| conflict_options = options_to_conflict_info(options) | ||
| # Extract conflict options to pass to the tuple key methods | ||
| on_duplicate = None | ||
| on_missing = None | ||
| if conflict_options: | ||
SoulPancake marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if conflict_options.on_duplicate_writes: | ||
| on_duplicate = conflict_options.on_duplicate_writes.value | ||
| if conflict_options.on_missing_deletes: | ||
| on_missing = conflict_options.on_missing_deletes.value | ||
| writes_tuple_keys = None | ||
| deletes_tuple_keys = None | ||
| if body.writes_tuple_keys: | ||
| writes_tuple_keys = body.writes_tuple_keys | ||
| if body.deletes_tuple_keys: | ||
| deletes_tuple_keys = body.deletes_tuple_keys | ||
| if body.writes: | ||
| writes_tuple_keys = body.get_writes_tuple_keys(on_duplicate=on_duplicate) | ||
| if body.deletes: | ||
| deletes_tuple_keys = body.get_deletes_tuple_keys(on_missing=on_missing) | ||
| await self._api.write( | ||
| WriteRequest( | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| """ | ||
| Python SDK for OpenFGA | ||
| API version: 1.x | ||
| Website: https://openfga.dev | ||
| Documentation: https://openfga.dev/docs | ||
| Support: https://openfga.dev/community | ||
| License: [Apache-2.0](https://github.com/openfga/python-sdk/blob/main/LICENSE) | ||
| NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT. | ||
| """ | ||
| from enum import Enum | ||
| class ClientWriteRequestOnDuplicateWrites(str, Enum): | ||
| ERROR = "error" | ||
| IGNORE = "ignore" | ||
| class ClientWriteRequestOnMissingDeletes(str, Enum): | ||
| ERROR = "error" | ||
| IGNORE = "ignore" | ||
| class ConflictOptions: | ||
| """ | ||
| OpenFGA client write conflict options | ||
| """ | ||
| def __init__( | ||
| self, | ||
| on_duplicate_writes: ClientWriteRequestOnDuplicateWrites | None = None, | ||
| on_missing_deletes: ClientWriteRequestOnMissingDeletes | None = None, | ||
| ) -> None: | ||
| self._on_duplicate_writes = on_duplicate_writes | ||
| self._on_missing_deletes = on_missing_deletes | ||
| @property | ||
| def on_duplicate_writes(self) -> ClientWriteRequestOnDuplicateWrites | None: | ||
| """ | ||
| Return on_duplicate_writes | ||
| """ | ||
| return self._on_duplicate_writes | ||
| @on_duplicate_writes.setter | ||
| def on_duplicate_writes( | ||
| self, | ||
| value: ClientWriteRequestOnDuplicateWrites | None, | ||
| ) -> None: | ||
| """ | ||
| Set on_duplicate_writes | ||
| """ | ||
| self._on_duplicate_writes = value | ||
| @property | ||
| def on_missing_deletes(self) -> ClientWriteRequestOnMissingDeletes | None: | ||
| """ | ||
| Return on_missing_deletes | ||
| """ | ||
| return self._on_missing_deletes | ||
| @on_missing_deletes.setter | ||
| def on_missing_deletes( | ||
| self, | ||
| value: ClientWriteRequestOnMissingDeletes | None, | ||
| ) -> None: | ||
| """ | ||
| Set on_missing_deletes | ||
| """ | ||
| self._on_missing_deletes = value |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.