Skip to content

Commit eeab382

Browse files
committed
add query params for metrics
1 parent eca5e90 commit eeab382

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ client = OpaClient()
145145

146146
print(client.get_opa_raw_data("testapi/testdata")) # response is {'result': ['world', 'hello']}
147147

148+
# You can use query params for additional info
149+
# provenance - If parameter is true, response will include build/version info in addition to the result.
150+
# metrics - Return query performance metrics in addition to result
151+
152+
print(client.get_opa_raw_data("userinfo",query_params={"provenance": True}))
153+
# response is {'provenance': {'version': '0.25.2', 'build_commit': '4c6e524', 'build_timestamp': '2020-12-08T16:56:55Z', 'build_hostname': '3bb58334a5a9'}, 'result': {'user_roles': {'alice': ['admin'], 'bob': ['employee', 'billing'], 'eve': ['customer']}}}
154+
155+
print(client.get_opa_raw_data("userinfo",query_params={"metrics": True}))
156+
157+
# response is {'metrics': {'counter_server_query_cache_hit': 0, 'timer_rego_external_resolve_ns': 231, 'timer_rego_input_parse_ns': 381, 'timer_rego_query_compile_ns': 40173, 'timer_rego_query_eval_ns': 12674, 'timer_rego_query_parse_ns': 5692, 'timer_server_handler_ns': 83490}, 'result': {'user_roles': {'alice': ['admin'], 'bob': ['employee', 'billing'], 'eve': ['customer']}}}
158+
148159
del client
149160
```
150161

@@ -212,6 +223,10 @@ client.check_permission(input_data=permission_you_want_check, policy_name="testp
212223

213224
# response is {'result': True}
214225

226+
# You can use query params for additional info
227+
# provenance - If parameter is true, response will include build/version info in addition to the result.
228+
# metrics - Return query performance metrics in addition to result
229+
215230
del client
216231
```
217232

opa_client/opa.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
SSLError, FileError, TypeExecption,
2323
QueryExecuteError)
2424

25-
__version__ = "1.3.0"
25+
__version__ = "1.3.1"
2626
__author__ = "Tural Muradov"
2727
__license__ = 'MIT'
2828

@@ -248,7 +248,12 @@ def update_or_create_opa_data(self, new_data: dict, endpoint: str) -> bool:
248248
return self.__update_opa_data(new_data, endpoint)
249249

250250
def get_opa_raw_data(self, data_name: str = "", query_params: Dict[str, bool] = dict()) -> dict:
251-
"""Returns OPA raw data in string type """
251+
""" Returns OPA raw data in string type
252+
```
253+
param :: data_name : OPA data name you want get
254+
param :: query_params : query params in url for more information about metrics
255+
```
256+
"""
252257
return self.__get_opa_raw_data(data_name, query_params)
253258

254259
def opa_policy_to_file(self,
@@ -297,6 +302,8 @@ def check_permission(self, input_data: dict, policy_name: str,
297302
298303
params :: rule_name : the name included in the policy
299304
type :: rule_name : str
305+
param :: query_params : query params in url for more information about metrics
306+
type :: query_params : dict
300307
```
301308
"""
302309

opa_client/test/test_opa.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ def test_client(self):
2727
client = OpaClient("localhost", 8181, "v1")
2828
self.assertEqual("http://localhost:8181/v1", client._root_url)
2929

30-
client = OpaClient("127.0.0.1", 8181, "v1")
31-
self.assertEqual("http://127.0.0.1:8181/v1", client._root_url)
30+
client = OpaClient("localhost", 8181, "v1")
31+
self.assertEqual("http://localhost:8181/v1", client._root_url)
3232

3333
self.assertFalse(False, self.myclient._secure)
3434
self.assertEqual("http://", self.myclient._schema)
3535
self.assertEqual("v1", self.myclient._version)
36-
self.assertEqual("127.0.0.1", self.myclient._host)
36+
self.assertEqual("localhost", self.myclient._host)
3737
self.assertEqual(8181, self.myclient._port)
3838

3939
def test_functions(self):
@@ -53,7 +53,7 @@ def test_functions(self):
5353
print(rr)
5454
try:
5555
my_dict = {'test': {'path': [
56-
'http://127.0.0.1:8181/v1/data/play'], 'rules': ['http://127.0.0.1:8181/v1/data/play/hello']}}
56+
'http://localhost:8181/v1/data/play'], 'rules': ['http://localhost:8181/v1/data/play/hello']}}
5757

5858
self.assertEqual(my_dict, self.myclient.get_policies_info())
5959
except Exception as testing:
@@ -77,8 +77,8 @@ def test_functions(self):
7777

7878
# self.assertEqual(self.myclient.update_opa_policy_fromurl())
7979
self.assertEqual(["test"], self.myclient.get_policies_list())
80-
my_dict = {'test': {'path': ['http://127.0.0.1:8181/v1/data/play'],
81-
'rules': ['http://127.0.0.1:8181/v1/data/play/hello']}}
80+
my_dict = {'test': {'path': ['http://localhost:8181/v1/data/play'],
81+
'rules': ['http://localhost:8181/v1/data/play/hello']}}
8282

8383
self.assertEqual(my_dict, self.myclient.get_policies_info())
8484

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
setuptools.setup(
1313
name="OPA-python-client",
14-
version="1.3.0",
14+
version="1.3.1",
1515
author="Tural Muradov",
1616
author_email="tural_m@hotmail.com",
1717
description="Client for connection to the OPA service",

0 commit comments

Comments
 (0)