-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAPI.py
336 lines (272 loc) · 11.8 KB
/
API.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import requests
import shutil
import json
import urllib3
import urllib.parse
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
from Utils import Utils
# defining a params dict for the parameters to be sent to the API
# PARAMS = {'address':location}
# r = requests.get(url = repositoryResourceURL, params = PARAMS)
class API() :
# ------------------------ SERVICE REGISTRY STUFF --------------------------
srPingURL = "https://localhost:3000/ping/"
srMinersURL = "https://localhost:3000/miners/"
srRepositoriesURL = "https://localhost:3000/repositories/"
srConnectionsURL = "https://localhost:3000/connections/filters/"
srConfigsURL = "https://localhost:3000/config/filters/"
# Ping
def pingSr(self):
response = requests.get(self.srPingURL, verify=False)
responseStr = response.text.strip('\"')
return responseStr
# Add miner
def addminerSr(self, payload):
response = requests.post(self.srMinersURL, data=json.dumps(payload), verify=False)
responseStr = response.text.strip('\"')
return responseStr
# Get registered miner URLs
def getMinersSr(self):
response = requests.get(self.srMinersURL, verify=False)
responseStr = response.text.strip('\"')
return responseStr
# Delete miner
def deleteMinerSr(self, payload):
response = requests.delete(self.srMinersURL, data=json.dumps(payload), verify=False)
responseStr = response.text.strip('\"')
return responseStr
# Add repository
def addRepositorySr(self, payload):
response = requests.post(self.srRepositoriesURL, data=json.dumps(payload), verify=False)
responseStr = response.text.strip('\"')
return responseStr
# Get registered repository URLs
def getRepositoriesSr(self):
response = requests.get(self.srRepositoriesURL, verify=False)
responseStr = response.text.strip('\"')
return responseStr
# Delete repository
def deleteRepositorySr(self, payload):
response = requests.delete(self.srRepositoriesURL, data=json.dumps(payload), verify=False)
responseStr = response.text.strip('\"')
return responseStr
# Get filtered connection URLs
def getFilteredConnectionsSr(self, payload):
response = requests.post(self.srConnectionsURL, data=json.dumps(payload), verify=False)
connectionList = json.loads(response.text)
return connectionList
# Get filtered configs
def getFilteredConfigsSr(self, payload):
response = requests.post(self.srConfigsURL, data=json.dumps(payload), verify=False)
connectionList = json.loads(response.text)
return connectionList
# ------------------------ REPOSITORY STUFF --------------------------
repositoryPingURL = "http://localhost:4001/ping"
repositoryConfigURL = "http://localhost:4001/configurations"
repositoryResourceURL = "http://localhost:4001/resources/"
repositoryMetadataURL = "http://localhost:4001/resources/metadata/"
repositoryGraphURL = "http://localhost:4001/resources/graphs/"
repositoryHistogramURL = "http://localhost:4001/resources/histograms/"
repositoryFilterURL = "http://localhost:4001/resources/metadata/filters"
# Ping
def pingRepo(self):
response = requests.get(self.repositoryPingURL)
responseStr = response.text.strip('\"')
return responseStr
# Config
def getRepoConfig(self):
response = requests.get(self.repositoryConfigURL)
config = json.loads(response.text)
print("Repo config: " + str(config))
return config
# POST resource
def uploadResourceToRepo(self, filePath, payload):
files=[
('file',('ML4_log.xes',open(filePath,'rb'),'application/octet-stream'))
]
response = requests.post(url=self.repositoryResourceURL, data=payload, files=files)
responseStr = response.text.strip('\"')
print("File Resource ID: " + responseStr)
return responseStr
# POST Metadata
def uploadMetadataToRepo(self, payload):
response = requests.post(self.repositoryMetadataURL, data=payload)
print("uploadMetadataToRepo response: " + response.text)
responseStr = response.text.strip('\"')
print("MDO Resource ID: " + responseStr)
return responseStr
# PUT resource
def updateResourceOnRepo(self, rid, filePath):
url = urllib.parse.urljoin(self.repositoryResourceURL, rid)
files=[
('file',('ML4_log.xes',open(filePath,'rb'),'application/octet-stream'))
]
response = requests.put(url=url, files=files)
responseStr = response.text.strip('\"')
print("File Resource ID: " + responseStr)
return responseStr
# PUT Metadata
def updateMetadataOnRepo(self, rid, payload):
url = urllib.parse.urljoin(self.repositoryMetadataURL, rid)
response = requests.put(url, data=payload)
print("updateMetadataOnRepo response: " + response.text)
responseStr = response.text.strip('\"')
print("MDO Resource ID: " + responseStr)
return responseStr
# ------------------------ GET RESOURCE --------------------------
def getResourceFromRepo(self, rid, path):
url = urllib.parse.urljoin(self.repositoryResourceURL, rid)
response = requests.get(url=url, stream=True)
if("No resource with that ID" in response.text):
return False
with open(path, 'wb') as file:
file.write(response.content)
# with open(path, 'wb') as out_file:
# shutil.copyfileobj(response.content, out_file)
print('The file with rid ' + rid + ' was saved successfully')
return True
# ------------------------ GET HISTOGRAM --------------------------
def getHistogramFromRepo(self, rid, path):
url = urllib.parse.urljoin(self.repositoryHistogramURL, rid)
print("Requesting histogram from: " + url)
response = requests.post(url=url, stream=True)
if("No file of type EventLog exist for resource id:" in response.text):
return False
if("No resource with that ID" in response.text):
return False
with open(path, 'wb') as file:
file.write(response.content)
print('The histogram for rid ' + rid + ' was saved successfully')
return True
# ------------------------ GET RESOURCE GRAPH --------------------------
def getGraphFromRepo(self, rid, path):
url = urllib.parse.urljoin(self.repositoryGraphURL, rid)
response = requests.get(url=url, stream=True)
if("No resource exist for that ID" in response.text):
return False
if("No resource with that ID" in response.text):
return False
with open(path, 'wb') as file:
file.write(response.content)
print('The graph for rid ' + rid + ' was saved successfully')
return True
# ------------------------ GET METADATA OBJECT --------------------------
def getMetadataFromRepo(self, rid):
utils = Utils()
if(not utils.isValidGUID(rid)):
print("rid is not valid: " + rid)
return False
url = urllib.parse.urljoin(self.repositoryMetadataURL, rid)
print("url: " + url)
response = requests.get(url)
if("No resource with that ID" in response.text):
return False
mdo = json.loads(response.text)
return mdo
# ------------------------ GET FILTERED LIST OF METADATA OBJECTS --------------------------
def getFilteredMDOList(self, filters):
payload = json.dumps(filters)
response = requests.post(self.repositoryFilterURL, data=payload)
mdo_list = json.loads(response.text)
return mdo_list
# ------------------------ GET CHILDREN METADATA OBJECT LIST --------------------------
def getChildrenMDOList(self, rid):
url = urllib.parse.urljoin(self.repositoryMetadataURL, rid)
url = urllib.parse.urljoin(url + "/", "children")
response = requests.get(url)
print("Request to url: " + str(url) + " gave response: " + response.text)
mdo_list = json.loads(response.text)
return mdo_list
# ------------------------ MINER STUFF --------------------------
minerPingURL = "http://localhost:5000/ping/"
minerConfigURL = "http://localhost:5000/configurations/"
minerRunURL = "http://localhost:5000/miner/"
minerStatusURL = "http://localhost:5000/status/"
minerStopURL = "http://localhost:5000/stop/"
minerShadowURL = "http://localhost:5000/shadow/"
minerShadowRequirementsURL = "http://localhost:5000/shadow/requirements/"
minerShadowStatusURL = "http://localhost:5000/shadow/status/"
# Ping
def pingMiner(self):
response = requests.get(self.minerPingURL)
responseStr = response.text.strip('\"')
return responseStr
# ------------------------ GET MINER CONFIG --------------------------
def getMinerConfigList(self):
# url = urllib.parse.urljoin(self.minerConfig)
response = requests.get(self.minerConfigURL)
config_list = json.loads(response.text)
return config_list
# ------------------------ RUN MINER ALGORITHM --------------------------
# ------------------------ RUN FILE CONSUMING MINER --------------------------
# ------------------------ RUN STREAM CONSUMING MINER --------------------------
def runMiner(self, body):
payload = json.dumps(body)
# print("Run miner on " + self.minerRunURL + " with payload: ")
# print(payload)
headers = {
'Content-Type': 'application/json'
}
response = requests.post(self.minerRunURL, headers=headers, data=payload)
# response = requests.post(self.minerRunURL, data=payload)
responseStr = response.text.strip('\"')
print("runMiner pid: " + responseStr)
return responseStr # PID
# ------------------------ GET SPECIFIC MINER ALGORITHM STATUS --------------------------
def getMinerStatus(self, pid):
url = urllib.parse.urljoin(self.minerStatusURL, pid)
response = requests.get(url)
status_obj = json.loads(response.text)
return status_obj
# ------------------------ GET LIST OF MINER ALGORITHM STATUS --------------------------
def getMinerStatusList(self):
response = requests.get(self.minerStatusURL)
status_list = json.loads(response.text)
return status_list
# ------------------------ STOP MINER ALGORITHM --------------------------
def stopMinerAlgorithm(self, pid):
url = urllib.parse.urljoin(self.minerStopURL, pid)
response = requests.delete(url)
responseStr = response.text.strip('\"')
return responseStr
# ------------------------ GET ALGORITHM FILE --------------------------
def getMinerAlgorithmFile(self, mid, path):
url = urllib.parse.urljoin(self.minerShadowURL, mid)
response = requests.get(url=url, stream=True)
print("algorithm code: " + str(response.status_code))
if(response.status_code != 200):
print("algorithm response: " + str(response.content))
return response
with open(path, 'wb') as file:
file.write(response.content)
print('The algorithm file was saved successfully')
return True
# ------------------------ GET REQUIREMENTS FILE --------------------------
def getMinerRequirementsFile(self, mid, path):
url = urllib.parse.urljoin(self.minerShadowRequirementsURL, mid)
response = requests.get(url=url, stream=True)
print("requirements code: " + str(response.status_code))
if(response.status_code != 200):
print("requirements response: " + str(response.content))
return response
with open(path, 'wb') as file:
file.write(response.content)
print('The requirements file was saved successfully')
return True
# ------------------------ RUN CLONING ACTION --------------------------
def runMinerCloning(self, body):
payload = json.dumps(body)
headers = {
'Content-Type': 'application/json'
}
response = requests.post(self.minerShadowURL, headers=headers, data=payload)
responseStr = response.text.strip('\"')
return responseStr # PID
# ------------------------ GET CLONING STATUS --------------------------
def getMinerCloningStatus(self, cid):
url = urllib.parse.urljoin(self.minerShadowStatusURL, cid)
response = requests.get(url)
# response = requests.get(url)
# status_obj = json.loads(response.text)
status = response.text
return status