This repository has been archived by the owner on Jan 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Key.py
102 lines (83 loc) · 3.12 KB
/
Key.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import packages.requests as requests
def list_keys(username, password):
"""
List all API Keys
:param username: Admin Username
:type username: str
:param password: Admin Password
:type password: str
:return: List of all API Keys
:rtype: list
"""
keys = requests.get(os.getenv("key_server_site") + "/rest/key/list", auth=(username, password))
try:
assert keys.status_code / 100 == 2
return keys.json()
except AssertionError:
exit("Problem Listing Keys (Check Server Logs for Details) - HTTP Status: %i" % keys.status_code)
def create_key(username, password, application_name, permissions):
"""
Create API Key
:param username: Admin Username
:type username: str
:param password: Admin Password
:type password: str
:param application_name: Name of the Application that will be using the API Key
:type application_name: str
:param permissions: Permissions for the API Key
:type permissions: str
:return: Generated API Key
:rtype: dict
"""
key = requests.post(os.getenv("key_server_site") + "/rest/key/issue",
auth=(username, password),
json={"application_name": application_name,
"permissions": permissions})
try:
assert key.status_code == 201
return key.json()
except AssertionError:
exit("Problem Creating Key (Check Server Logs for Details) - HTTP Status: %i" % key.status_code)
def update_key(username, password, application_key, application_name=None, permissions=None):
"""
Update API Key
:param username: Admin Username
:type username: str
:param password: Admin Password
:type password: str
:param application_key: Application Key to Update
:type application_key: str
:param application_name: New Application Name
:type application_name: str
:param permissions: New Application Permissions
:type permissions: str
:return: Updated API Key
:rtype: dict
"""
key = requests.put(os.getenv("key_server_site") + "/rest/key/update",
auth=(username, password),
json={"application_key": application_key,
"application_name": application_name,
"permissions": permissions})
try:
assert key.status_code == 202
return key
except AssertionError:
exit("Problem Updating Key (Check Server Logs for Details) - HTTP Status: %i" % key.status_code)
def delete_key(username, password, application_key):
"""
Delete API Key
:param username: Admin Username
:type username: str
:param password: Admin Password
:type password: str
:param application_key: Application Key to Delete
:type application_key: str
:return: If the Key was revoked
:rtype: bool
"""
key = requests.delete(os.getenv("key_server_site") + "/rest/key/revoke",
auth=(username, password),
json={"application_key": application_key})
return key.status_code == 202