Skip to content
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

Read and write using different tuple models #106

Open
1 task done
harry-hause opened this issue Jun 18, 2024 · 2 comments
Open
1 task done

Read and write using different tuple models #106

harry-hause opened this issue Jun 18, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@harry-hause
Copy link

Checklist

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

@evansims
Copy link
Member

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.

@harry-hause
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

2 participants