python-sdk
python-sdk copied to clipboard
Read and write using different tuple models
Checklist
- [X] I agree to the terms within the OpenFGA Code of Conduct.
Describe the problem you'd like to have solved
I added the SDK to our backend yesterday and found that there are two different models being used when reading from the store and writing to the store.
from openfga_sdk import ReadRequestTupleKey, Tuple as FgaTuple
from openfga_sdk.client.models import ClientTuple
What's the reason for this? It would just save us some code to convert between them and make things cleaner with a single model/source of truth in the backend.
Thanks!
Describe the ideal solution
Read and write uses the same model for tuples
Alternatives and current workarounds
Cast in between them when reading, altering, and writing back to the store.
References
No response
Additional context
No response
Hey @harry-hause 👋
Our base models are generated from the OpenAPI spec; the first line of the code sample you provided is importing those low-level, generated classes.
Built on top of those generated models, we also provide purpose-built Client models (that live beneath that .client namespace) that tend to offer some developer experience advantages. There's generally no wrong answer as to which to use, but the client models would be our recommended usage approach whenever possible.
Hi @evansims , thanks for taking a look at this.
I should have been more specific. When calling the read method with the example from the docs like:
# from openfga_sdk import OpenFgaClient, ReadRequestTupleKey
# Initialize the fga_client
# fga_client = OpenFgaClient(configuration)
# Find if a relationship tuple stating that a certain user is a viewer of certain document
body = ReadRequestTupleKey(
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation="viewer",
object="document:roadmap",
)
response = await fga_client.read(body)
# response = ReadResponse({"tuples": [Tuple({"key": TupleKey({"user":"...","relation":"...","object":"..."}), "timestamp": datetime.fromisoformat("...") })]})
The type of the list of tuples returned is the generated model from OpenAPI spec:
from openfga_sdk import Tuple as FgaTuple
Is there a way to elegantly convert this to the ClientTuple? Since the write method requires that type:
body = ClientWriteRequest(
writes=[
ClientTuple(
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation="viewer",
object="document:roadmap",
),
ClientTuple(
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation="viewer",
object="document:budget",
condition=RelationshipCondition(
name='ViewCountLessThan200',
context=dict(
Name='Roadmap',
Type='Document',
),
),
),
],
deletes=[
ClientTuple(
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation="writer",
object="document:roadmap",
),
],
)
We can write a conversion method but having the read() return the ClientTuple type would save us from having to do that.