-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapplication.py
75 lines (65 loc) · 2.62 KB
/
application.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
import json
from restClient import RestClient
from utils.utils import DotDict
from resource import Resource
application_const = {
'SECURITY_TYPES': {'BASIC': "basic", 'DIGEST': 'digest'},
'TYPES': {
'PRODUCTION': "production",
'SANDBOX': "sandbox",
'INLINE': "inline",
}
}
class Application(Resource):
CONST = DotDict(application_const)
RESOURCE = "/applications"
APPLICATION = "store"
def __init__(self, name, description, throttlingTier='', **kwargs):
super().__init__()
self.name = name
self.description = description
self.throttlingTier = throttlingTier
self.subscriber = None
self.permission = "[{\"groupId\" : 1000, \"permission\" : [\"READ\",\"UPDATE\"]},{\"groupId\" : 1001, \"permission\" : [\"READ\",\"UPDATE\"]}]"
self.lifeCycleStatus = None
self.keys = []
if kwargs:
self._parse_json(kwargs)
def save(self):
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
data = self.to_json()
res = self.client.session.post(Application.get_endpoint(), data=json.dumps(data), verify=self.client.verify,
headers=headers)
print("Status code: {}".format(res.status_code))
if res.status_code != 201:
print(res.json())
raise Exception("Fail to save the global endpoint via REST API")
self._data = DotDict(res.json())
self.id = self._data.applicationId
return self
@staticmethod
def delete_all(client=RestClient()):
res = client.session.get(Application.get_endpoint(), verify=client.verify)
if not res.status_code == 200:
print(res.json())
raise Exception("Error getting Applications list")
print("Status code: {}".format(res.status_code))
apps_list = res.json()['list']
for app in apps_list:
res = client.session.delete(Application.get_endpoint() + "/{}".format(app['applicationId']), verify=client.verify)
if res.status_code != 200:
print("Warning Error while deleting the API {}\nERROR: {}".format(app['name'], res.content))
print("Status code: {}".format(res.status_code))
# TODO: Make this a generic to_json method for all Resources
def to_json(self):
temp = {
'name': self.name,
'description': self.description,
'throttlingTier': self.throttlingTier
}
return temp
def set_rest_client(self, client=RestClient(APPLICATION)):
self.client = client