Skip to content

Commit

Permalink
chore: merge fixes from openfga/sdk-generator#265
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Dec 30, 2023
1 parent fbee6b6 commit cab8916
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 6 deletions.
3 changes: 1 addition & 2 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ By submitting an issue to this repository, you agree to the terms within the [Op
### Version of SDK


```
$ python -m openfga_sdk.help
<paste here>
```

This command is only available on openfga_sdk v0.3.2 and greater. Otherwise, please provide some basic information about your system.
This command is only available on openfga_sdk `v0.3.2` and greater. Otherwise, please provide some basic information about your system.

### Version of OpenFGA (if known)

Expand Down
7 changes: 7 additions & 0 deletions .openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ docs/WriteAssertionsRequest.md
docs/WriteAuthorizationModelRequest.md
docs/WriteAuthorizationModelResponse.md
docs/WriteRequest.md
example/Makefile
example/README.md
example/example1/auth-model.json
example/example1/example1.py
example/example1/requirements.txt
example/example1/setup.cfg
example/example1/setup.py
openfga_sdk/__init__.py
openfga_sdk/api/__init__.py
openfga_sdk/api/open_fga_api.py
Expand Down
10 changes: 8 additions & 2 deletions openfga_sdk/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from openfga_sdk.client.models.write_response import ClientWriteResponse
from openfga_sdk.client.models.expand_request import ClientExpandRequest
from openfga_sdk.client.models.list_objects_request import ClientListObjectsRequest
from openfga_sdk.client.models.list_relations_request import ClientListRelationsRequest
from openfga_sdk.client.models.write_single_response import construct_write_single_response
from openfga_sdk.client.models.write_transaction_opts import WriteTransactionOpts
from openfga_sdk.client.models.read_changes_request import ClientReadChangesRequest
Expand Down Expand Up @@ -352,9 +353,14 @@ async def read(self, body: TupleKey, options: dict[str, str] = None):
options.pop("continuation_token")
kwargs = options_to_kwargs(options)

if body is None or (body.object is None and body.relation is None and body.user is None):
tuple_key = None
else:
tuple_key = body

api_response = await self._api.read(
ReadRequest(
tuple_key=body,
tuple_key=tuple_key,
page_size=page_size,
continuation_token=continuation_token,
),
Expand Down Expand Up @@ -608,7 +614,7 @@ async def list_objects(self, body: ClientListObjectsRequest, options: dict[str,
)
return api_response

async def list_relations(self, body: ClientListObjectsRequest, options: dict[str, str] = None): # noqa: E501
async def list_relations(self, body: ClientListRelationsRequest, options: dict[str, str] = None): # noqa: E501
"""
Return all the relations for which user has a relationship with the object
:param body - list relation request
Expand Down
10 changes: 8 additions & 2 deletions openfga_sdk/sync/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from openfga_sdk.client.models.write_response import ClientWriteResponse
from openfga_sdk.client.models.expand_request import ClientExpandRequest
from openfga_sdk.client.models.list_objects_request import ClientListObjectsRequest
from openfga_sdk.client.models.list_relations_request import ClientListRelationsRequest
from openfga_sdk.client.models.write_single_response import construct_write_single_response
from openfga_sdk.client.models.write_transaction_opts import WriteTransactionOpts
from openfga_sdk.client.models.read_changes_request import ClientReadChangesRequest
Expand Down Expand Up @@ -352,9 +353,14 @@ def read(self, body: TupleKey, options: dict[str, str] = None):
options.pop("continuation_token")
kwargs = options_to_kwargs(options)

if body is None or (body.object is None and body.relation is None and body.user is None):
tuple_key = None
else:
tuple_key = body

api_response = self._api.read(
ReadRequest(
tuple_key=body,
tuple_key=tuple_key,
page_size=page_size,
continuation_token=continuation_token,
),
Expand Down Expand Up @@ -610,7 +616,7 @@ def list_objects(self, body: ClientListObjectsRequest, options: dict[str, str] =
)
return api_response

def list_relations(self, body: ClientListObjectsRequest, options: dict[str, str] = None): # noqa: E501
def list_relations(self, body: ClientListRelationsRequest, options: dict[str, str] = None): # noqa: E501
"""
Return all the relations for which user has a relationship with the object
:param body - list relation request
Expand Down
47 changes: 47 additions & 0 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,53 @@ async def test_read_empty_options(self, mock_request):
_request_timeout=None
)

@patch.object(rest.RESTClientObject, 'request')
async def test_read_empty_body(self, mock_request):
"""Test case for read with empty body
Get tuples from the store that matches a query, without following userset rewrite rules # noqa: E501
"""
response_body = '''
{
"tuples": [
{
"key": {
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
"relation": "reader",
"object": "document:2021-budget"
},
"timestamp": "2021-10-06T15:32:11.128Z"
}
]
}
'''
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
# Enter a context with an instance of the API client
async with OpenFgaClient(configuration) as api_client:
body = TupleKey()
api_response = await api_client.read(
body=body,
options={}
)
self.assertIsInstance(api_response, ReadResponse)
key = TupleKey(user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation="reader", object="document:2021-budget")
timestamp = datetime.fromisoformat("2021-10-06T15:32:11.128+00:00")
expected_data = ReadResponse(tuples=[Tuple(key=key, timestamp=timestamp)])
self.assertEqual(api_response, expected_data)
mock_request.assert_called_once_with(
'POST',
'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/read',
headers=ANY,
query_params=[],
post_params=[],
body={},
_preload_content=ANY,
_request_timeout=None
)

@patch.object(rest.RESTClientObject, 'request')
async def test_write(self, mock_request):
"""Test case for write
Expand Down
47 changes: 47 additions & 0 deletions test/test_client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,53 @@ def test_read_empty_options(self, mock_request):
_request_timeout=None
)

@patch.object(rest.RESTClientObject, 'request')
def test_read_empty_body(self, mock_request):
"""Test case for read with empty body
Get tuples from the store that matches a query, without following userset rewrite rules # noqa: E501
"""
response_body = '''
{
"tuples": [
{
"key": {
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
"relation": "reader",
"object": "document:2021-budget"
},
"timestamp": "2021-10-06T15:32:11.128Z"
}
]
}
'''
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
# Enter a context with an instance of the API client
with OpenFgaClient(configuration) as api_client:
body = TupleKey()
api_response = api_client.read(
body=body,
options={}
)
self.assertIsInstance(api_response, ReadResponse)
key = TupleKey(user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation="reader", object="document:2021-budget")
timestamp = datetime.fromisoformat("2021-10-06T15:32:11.128+00:00")
expected_data = ReadResponse(tuples=[Tuple(key=key, timestamp=timestamp)])
self.assertEqual(api_response, expected_data)
mock_request.assert_called_once_with(
'POST',
'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/read',
headers=ANY,
query_params=[],
post_params=[],
body={},
_preload_content=ANY,
_request_timeout=None
)

@patch.object(rest.RESTClientObject, 'request')
def test_write(self, mock_request):
"""Test case for write
Expand Down

0 comments on commit cab8916

Please sign in to comment.