Skip to content

Commit 493f256

Browse files
committed
feat: client to update auth model id and store id
1 parent 3406fe2 commit 493f256

3 files changed

Lines changed: 132 additions & 1 deletion

File tree

openfga_sdk/api_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,3 +705,15 @@ def _get_store_id(self):
705705
'store_id is required but not configured'
706706
)
707707
return configuration.store_id
708+
709+
def set_store_id(self, value):
710+
"""
711+
Update the store ID in the configuration
712+
"""
713+
self.configuration.store_id = value
714+
715+
def get_store_id(self):
716+
"""
717+
Return the store id (if any) store in the configuration
718+
"""
719+
return self.configuration.store_id

openfga_sdk/client/openfga_client.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,30 @@ def _get_authorization_model_id(self, options):
128128
else:
129129
return self._client_configuration.authorization_model_id
130130

131+
def set_store_id(self, value):
132+
"""
133+
Update the store ID in the configuration
134+
"""
135+
self._api_client.set_store_id(value)
136+
137+
def get_store_id(self):
138+
"""
139+
Return the store id (if any) store in the configuration
140+
"""
141+
return self._api_client.get_store_id()
142+
143+
def set_authorization_model_id(self, value):
144+
"""
145+
Update the authorizaiton model id in the configuration
146+
"""
147+
self._client_configuration.authorization_model_id = value
148+
149+
def get_authorization_model_id(self):
150+
"""
151+
Return the authorizaiton model id
152+
"""
153+
return self._client_configuration.authorization_model_id
154+
131155
#################
132156
# Stores
133157
#################

test/test_openfga_client.py

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ async def test_create_store(self, mock_request):
177177
'''
178178
mock_request.return_value = mock_response(response_body, 201)
179179
configuration = self.configuration
180-
configuration.store_id = store_id
181180
async with OpenFgaClient(configuration) as api_client:
182181
api_response = await api_client.create_store(
183182
CreateStoreRequest(name="test-store"),
@@ -1911,3 +1910,99 @@ async def test_write_assertions(self, mock_request):
19111910
_preload_content=ANY,
19121911
_request_timeout=None
19131912
)
1913+
1914+
@patch.object(rest.RESTClientObject, 'request')
1915+
async def test_set_store_id(self, mock_request):
1916+
"""Test case for write assertions
1917+
1918+
Get all stores # noqa: E501
1919+
"""
1920+
mock_request.return_value = mock_response('', 204)
1921+
configuration = self.configuration
1922+
configuration.store_id = store_id
1923+
async with OpenFgaClient(configuration) as api_client:
1924+
api_client.set_store_id("01YCP46JKYM8FJCQ37NMBYHE5Y")
1925+
await api_client.write_assertions(
1926+
[Assertion(
1927+
tuple_key=TupleKey(object="document:2021-budget", relation="reader",
1928+
user="user:anne"),
1929+
expectation=True,
1930+
)],
1931+
options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"}
1932+
)
1933+
self.assertEqual(api_client.get_store_id(), "01YCP46JKYM8FJCQ37NMBYHE5Y")
1934+
mock_request.assert_called_once_with(
1935+
'PUT',
1936+
'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5Y/assertions/01G5JAVJ41T49E9TT3SKVS7X1J',
1937+
headers=ANY,
1938+
body={"assertions": [{"tuple_key": {"object": "document:2021-budget",
1939+
"relation": "reader", "user": "user:anne"}, "expectation": True}]},
1940+
query_params=[],
1941+
post_params=[],
1942+
_preload_content=ANY,
1943+
_request_timeout=None
1944+
)
1945+
1946+
@patch.object(rest.RESTClientObject, 'request')
1947+
async def test_config_auth_model(self, mock_request):
1948+
"""Test case for write assertions
1949+
1950+
Get all stores # noqa: E501
1951+
"""
1952+
mock_request.return_value = mock_response('', 204)
1953+
configuration = self.configuration
1954+
configuration.store_id = store_id
1955+
configuration.authorization_model_id = "01G5JAVJ41T49E9TT3SKVS7X1J"
1956+
async with OpenFgaClient(configuration) as api_client:
1957+
await api_client.write_assertions(
1958+
[Assertion(
1959+
tuple_key=TupleKey(object="document:2021-budget", relation="reader",
1960+
user="user:anne"),
1961+
expectation=True,
1962+
)],
1963+
options={}
1964+
)
1965+
self.assertEqual(api_client.get_authorization_model_id(), "01G5JAVJ41T49E9TT3SKVS7X1J")
1966+
mock_request.assert_called_once_with(
1967+
'PUT',
1968+
'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/assertions/01G5JAVJ41T49E9TT3SKVS7X1J',
1969+
headers=ANY,
1970+
body={"assertions": [{"tuple_key": {"object": "document:2021-budget",
1971+
"relation": "reader", "user": "user:anne"}, "expectation": True}]},
1972+
query_params=[],
1973+
post_params=[],
1974+
_preload_content=ANY,
1975+
_request_timeout=None
1976+
)
1977+
1978+
@patch.object(rest.RESTClientObject, 'request')
1979+
async def test_update_auth_model(self, mock_request):
1980+
"""Test case for write assertions
1981+
1982+
Get all stores # noqa: E501
1983+
"""
1984+
mock_request.return_value = mock_response('', 204)
1985+
configuration = self.configuration
1986+
configuration.store_id = store_id
1987+
configuration.authorization_model_id = "01G5JAVJ41T49E9TT3SKVS7X1J"
1988+
async with OpenFgaClient(configuration) as api_client:
1989+
api_client.set_authorization_model_id("01G5JAVJ41T49E9TT3SKVS7X2J")
1990+
await api_client.write_assertions(
1991+
[Assertion(
1992+
tuple_key=TupleKey(object="document:2021-budget", relation="reader",
1993+
user="user:anne"),
1994+
expectation=True,
1995+
)],
1996+
options={}
1997+
)
1998+
mock_request.assert_called_once_with(
1999+
'PUT',
2000+
'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/assertions/01G5JAVJ41T49E9TT3SKVS7X2J',
2001+
headers=ANY,
2002+
body={"assertions": [{"tuple_key": {"object": "document:2021-budget",
2003+
"relation": "reader", "user": "user:anne"}, "expectation": True}]},
2004+
query_params=[],
2005+
post_params=[],
2006+
_preload_content=ANY,
2007+
_request_timeout=None
2008+
)

0 commit comments

Comments
 (0)