openfga / python-sdk

OpenFGA SDK for Python 3 - https://pypi.org/project/openfga-sdk/
https://openfga.dev
Apache License 2.0
29 stars 10 forks source link

`TupleKey` should support `relation=None` #95

Open jnu opened 3 weeks ago

jnu commented 3 weeks ago

Checklist

Description

The docs describe how to use the read() method with a TupleKey with no relation set to get a list of all relations a user has with an object. Currently the TupleKey object validation prevents the relation from being None.

Expectation

Not to throw an error when omitting relation or setting to None.

Reproduction

Essentially the code from the docs:

body = TupleKey(
            user="user:bob",
            object="document:123",

)

Working around the validation and submitting the query confirms the behavior describes from the docs (i.e., listing the relations) works as expected:

Issuing a query with a workaround:

body = TupleKey(
            user="user:bob",
            relation="",
            object="document:123",

)
body._relation = None

# call `read(body)` ...

OpenFGA SDK version

0.4.0 (but the code in main appears broken)

OpenFGA version

v1.5.0

SDK Configuration

[default options]

Logs

No response

References

No response

rhamzeh commented 2 weeks ago

Hi @jnu - Thanks for raising this! I will transfer this to be an issue in the docs, the SDK docs here are more up to date

https://github.com/openfga/python-sdk?tab=readme-ov-file#read-relationship-tuples

And you would want to do something like

# from openfga_sdk import OpenFgaClient, ReadRequestTupleKey

# Initialize the fga_client
# fga_client = OpenFgaClient(configuration)

# Find all relationship tuples where a certain user has a relationship as any relation to a certain document
body = ReadRequestTupleKey(
    user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
    object="document:roadmap",
)

response = await fga_client.read(body)
# response = ReadResponse({"tuples": [Tuple({"key": TupleKey({"user":"...","relation":"...","object":"..."}), "timestamp": datetime.fromisoformat("...") })]})
rhamzeh commented 2 weeks ago

Before we move it, can you clarify:

0.4.0 (but the code in main appears broken)

Is there something else not working for you?