Skip to content

Commit 5865291

Browse files
authored
Merge pull request #1 from warrant-dev/AddListWarrants
Add list warrants
2 parents 0c02215 + d6bb50b commit 5865291

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

examples/example.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ def make_warrant_requests(api_key):
44
client = Warrant(api_key)
55

66
# Create users and session tokens
7-
print("Created user with provided id: " + client.create_user("custom_id_001"))
7+
print("Created tenant with provided id: " + client.create_tenant("custom_tenant_001"))
8+
print("Created user with provided id: " + client.create_user("custom_user_001"))
89
new_user = client.create_user()
910
print("Created user with generated id: " + new_user)
11+
print(client.create_warrant(object_type="tenant", object_id="custom_tenant_001", relation="member", user=new_user))
1012
print("Created session token: " + client.create_session(new_user))
1113

1214
# Create and check warrants
1315
print(client.create_warrant(object_type="store", object_id="store1", relation="owner", user=new_user))
1416
is_authorized = client.is_authorized(object_type="store", object_id="store1", relation="owner", user_to_check=new_user)
1517
print(f"New user authorization result: {is_authorized}")
18+
print(f"All warrants: {client.list_warrants()}")
1619

1720
if __name__ == '__main__':
1821
# Replace with your Warrant api key

warrant/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ def _make_post_request(self, uri, json={}):
3232
else:
3333
raise WarrantException(msg=resp.text, status_code=resp.status_code)
3434

35+
def _make_get_request(self, uri, params={}):
36+
headers = { "Authorization": "ApiKey " + self._apiKey }
37+
resp = requests.get(url = API_ENDPOINT+API_VERSION+uri, headers = headers, params = params)
38+
if resp.status_code == 200:
39+
return resp.json()
40+
else:
41+
raise WarrantException(msg=resp.text, status_code=resp.status_code)
42+
3543
def create_user(self, user_id=""):
3644
if user_id == "":
3745
payload = {}
@@ -40,6 +48,14 @@ def create_user(self, user_id=""):
4048
json = self._make_post_request(uri="/users", json=payload)
4149
return json['userId']
4250

51+
def create_tenant(self, tenant_id=""):
52+
if tenant_id == "":
53+
payload = {}
54+
else:
55+
payload = { "tenantId": tenant_id }
56+
json = self._make_post_request(uri="/tenants", json=payload)
57+
return json['tenantId']
58+
4359
def create_session(self, user_id):
4460
if user_id == "":
4561
raise WarrantException(msg="Invalid userId provided")
@@ -63,6 +79,16 @@ def create_warrant(self, object_type, object_id, relation, user):
6379
resp = self._make_post_request(uri="/warrants", json=payload)
6480
return resp['id']
6581

82+
def list_warrants(self, object_type="", object_id="", relation="", user_id=""):
83+
filters = {
84+
"objectType": object_type,
85+
"objectId": object_id,
86+
"relation": relation,
87+
"userId": user_id,
88+
}
89+
resp = self._make_get_request(uri="/warrants", params=filters)
90+
return resp
91+
6692
def is_authorized(self, object_type, object_id, relation, user_to_check):
6793
if object_type == "" or object_id == "" or relation == "":
6894
raise WarrantException(msg="Invalid object_type, object_id and/or relation")

0 commit comments

Comments
 (0)