-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathendpoint.py
142 lines (120 loc) · 4.41 KB
/
endpoint.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import json
from restClient import RestClient
from utils.utils import DotDict
from resource import Resource
endpoint_const = {
'SECURITY_TYPES': {'BASIC': "basic", 'DIGEST': 'digest'},
'TYPES': {
'PRODUCTION': "production",
'SANDBOX': "sandbox",
'INLINE': "inline",
}
}
class Endpoint(Resource):
CONST = DotDict(endpoint_const)
RESOURCE = "/endpoints"
def __init__(self, name, type, service_url='', maxTps=10, **kwargs):
super().__init__()
if type not in ['http', 'https']:
raise Exception("endpoint_type should be either http or https")
self.endpointConfig = {
"endpointType": "SINGLE",
"list": [
{
"url": service_url,
"timeout": "1000",
"attributes": []
}
]
}
self.endpointSecurity = {'enabled': False}
self.name = name
self.max_tps = maxTps
self.type = type
if kwargs:
self._parse_json(kwargs)
def is_secured(self):
return self.endpointSecurity['enabled']
def set_rest_client(self, client=RestClient()):
self.client = client
def set_security(self, security_type, username, password):
if security_type not in Endpoint.CONST.SECURITY_TYPES.values():
raise Exception(
"Invalid security type, please proved one of follows {}".format(Endpoint.CONST.SECURITY_TYPES))
self.endpointSecurity = {
'enabled': True,
'type': security_type,
'username': username,
'password': password
}
return self.endpointSecurity
@property
def _parsers(self):
parsers = {
# 'endpointConfig': self._parse_endpointConfig
}
return parsers
def _parse_endpointConfig(self, value):
endpoint_config_json = json.loads(value)
self.endpointConfig = DotDict(endpoint_config_json)
@property
def service_url(self):
return self.endpointConfig['list'][0]['url']
@service_url.setter
def service_url(self, value):
self.endpointConfig['list'][0]['url'] = value
def save(self):
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
data = self.to_json()
res = self.client.session.post(Endpoint.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.id
return self
def delete(self):
res = self.client.session.delete(Endpoint.get_endpoint(
) + "/{}".format(self.id), verify=self.client.verify)
if res.status_code != 200:
print("Warning Error while deleting the API {}\nERROR: {}".format(
self.name, res.content))
print("Status code: {}".format(res.status_code))
def to_json(self):
temp = {
'name': self.name,
'endpointConfig': self.endpointConfig,
'endpointSecurity': self.endpointSecurity,
'maxTps': self.max_tps,
'type': self.type
}
return temp
@staticmethod
def delete_all(client=RestClient()):
res = client.session.get(Endpoint.get_endpoint(), verify=client.verify)
if not res.status_code == 200:
print(res.json())
raise Exception("Error getting APIs list")
print("Status code: {}".format(res.status_code))
apis_list = res.json()['list']
for api in apis_list:
res = client.session.delete(Endpoint.get_endpoint(
) + "/{}".format(api['id']), verify=client.verify)
if res.status_code != 200:
print("Warning Error while deleting the API {}\nERROR: {}".format(
api['name'], res.content))
print("Status code: {}".format(res.status_code))
@staticmethod
def all():
raise NotImplemented("Not implemented yet ...")
def main():
print("Endpoint testing")
endpoint = Endpoint("sample_4", "http", "http://www.google.com/sample")
endpoint.save()
if __name__ == '__main__':
main()