Write Tuple - Response Object

I have a sync client set up (Python SDK) and I am writing a tuple(s) and I can get the write to work but I have no idea what the response object looks like or how I know its successful or not.

<openfga_sdk.client.models.write_response.ClientWriteResponse object at 0x00000202DD53A630>

Looking at the code here:
class ClientWriteSingleResponse:

Looks like there should be a success or error attribute from what I can understand is going on here. But I am not sure how to tell if it was successful or not and I am not sure what I am missing here.

In your code when calling fga_client.write are you awaiting it?

response = await fga_client.write(body, options)

If you prefer to call synchronously instead you would need to import OpenFgaClient from openfga_sdk.sync instead of openfga_sdk.client

Yah I am doing that. I can see it is writing the tuple to the store. However I have no idea what the response object looks like as I want to possibly add some handling on that.

Also while I am on the subject, can you specify an overwrite in the options on a write tuple? I can see if I try sending the same tuple that already exists its handing me this back, which I am assuming is a overwrite issue…

openfga_sdk.exceptions.ValidationException: (400)
Reason: Bad Request

On the write response you should be able to check for response.success

For the overwrite case you can’t directly do this by writing the changed tuple but you can pass a delete and a write in a request in order to update the value.

Yah I had tried that but I am getting:

Traceback (most recent call last):
  File "C:\Temp\openfga\src\openfga\main.py", line 110, in <module>
    print(response.success)
          ^^^^^^^^^^^^^^^^
AttributeError: 'ClientWriteResponse' object has no attribute 'success'

I apologize. White each result has a success value the response you get will be an array of writes, each with a success value.

response = await fga_client.write(body, options)
for write in response.writes:
    print(f"Write Result: {write.success}")

or response.writes[0].success if only a single write is being done.

When deletes are sent as well they will be in response.deletes

@stephenbawks1 if you are not using the non-transactional mode, any failure will throw an exception, the very fact that none is thrown means it’s a success.

If you are using non-transactional mode, you will have to check the status on each element.

This is what the write response looks like: python-sdk/openfga_sdk/client/models/write_response.py at main · openfga/python-sdk · GitHub

1 Like