-
Notifications
You must be signed in to change notification settings - Fork 40
/
app.py
221 lines (202 loc) · 7.49 KB
/
app.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
from flask import Flask, request, send_from_directory
from flask_cors import CORS
from hydra_agent.agent import Agent
import sys
import json
import os
app = Flask(__name__, static_folder="console-frontend/build/")
# Setting CORS so it allows requests from our React app in localhost:3000
CORS(app, resources={r"*": {"origins": "http://localhost:3000"}})
# Remove to deploy
url = "http://localhost:8080/serverapi"
agent = Agent(url)
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def serve(path):
"""Default endpoint, it serves the built static React App
:return: Served file
"""
file_path = os.path.join(app.static_folder, path)
if path != "" and os.path.exists(file_path):
return send_from_directory(app.static_folder, path)
else:
return send_from_directory(app.static_folder, "index.html")
@app.route("/start-agent", methods=["POST"])
def start_agent():
"""Receive hydrus server URL and start the Agent
:param body['url']: Entrypoint URL for the hydrus server
:return: Success message
"""
global agent
global url
body = request.get_data()
body = body.decode("utf8").replace("'", '"')
body = json.loads(body)
url = body["url"]
agent = Agent(url)
return "Server started successfully"
@app.route("/hydra-doc", methods=["GET"])
def hydra_doc():
"""Serve Hydra Doc
:return: Hydra Doc loaded on the agent with url for current connected
"""
apidoc = agent.fetch_apidoc()
generatedApiDoc = apidoc.generate()
generatedApiDoc["serverURL"] = url
return generatedApiDoc
@app.route("/apidoc-graph", methods=["GET"])
def apidoc_graph():
"""Sends Formatted ApiDoc Graph in Vis.js network format to Frontend
:return: Dict containing Nodes and Edges
"""
global agent
if agent is None:
return "No agent connected."
# Add the entrypoint node
nodes = [{"id": 1, "shape": "hexagon", "size": 15, "label": "Entrypoint"}]
edges = list()
id = 1
api_doc = agent.fetch_apidoc()
for resource_endpoint in api_doc.entrypoint.entrypoint.supportedProperty:
id += 1
endpoint_id = id
endpoint_path = resource_endpoint.id_.replace("vocab:EntryPoint/", "")
endpoint_node = create_node(endpoint_id, "box", 12, endpoint_path)
nodes.append(endpoint_node)
edge = create_edge(1, endpoint_id)
edges.append(edge)
for supportedOp in resource_endpoint.supportedOperation:
id += 1
op_id = id
operation_node = create_node(op_id, "circle", 10, supportedOp.method)
nodes.append(operation_node)
supportedOp_edge = create_edge(endpoint_id, op_id, "supportedOp")
edges.append(supportedOp_edge)
if supportedOp.expects:
expects = supportedOp.expects
else:
expects = "null"
if supportedOp.returns:
returns = supportedOp.returns
else:
returns = "null"
# Extract class name
if "vocab:" in expects:
expects = expects.replace("vocab:", "")
if "vocab:" in returns:
returns = returns.replace("vocab:", "")
id += 1
expected_class_node = create_node(id, "circle", 8, expects)
nodes.append(expected_class_node)
expects_edge = create_edge(op_id, id, "expects")
edges.append(expects_edge)
if expects in api_doc.parsed_classes:
class_id = id
for supportedProp in api_doc.parsed_classes[expects][
"class"
].supportedProperty:
id += 1
property_node = create_node(id, "box", 7, supportedProp.title)
nodes.append(property_node)
property_edge = create_edge(class_id, id, "supportedProp")
edges.append(property_edge)
id += 1
returned_class_node = create_node(id, "circle", 8, returns)
nodes.append(returned_class_node)
returns_edge = create_edge(op_id, id, "returns")
edges.append(returns_edge)
if returns in api_doc.parsed_classes:
class_id = id
for supportedProp in api_doc.parsed_classes[returns][
"class"
].supportedProperty:
id += 1
property_node = create_node(id, "box", 7, supportedProp.title)
nodes.append(property_node)
property_edge = create_edge(class_id, id, "supportedProp")
edges.append(property_edge)
graph = {"nodes": nodes, "edges": edges}
return graph
def create_node(id, shape, size, label):
"""Auxiliary function that creates a Node in Vis.js format
:return: Dict with Node attributes
"""
node = {
"id": id,
"shape": shape,
"size": size,
"label": label,
"color": {"background": "#FBD20B"},
}
return node
def create_edge(from_, to, label=None):
"""Auxiliary function that creates a Edge in Vis.js format
:return: Dict with Edge attributes
"""
edge = {"from": from_, "to": to}
if label is not None:
edge["label"] = label
return edge
@app.route("/send-command", methods=["POST"])
def send_command():
"""Send Command to Agent and returns hydrus response
:param: All parameters enabled by the Agent.
:param: Please check Agent.py in Agent's main repository.
:return: hydrus response to the request
"""
global agent
if agent is None:
return "No agent connected."
body = request.get_data()
body = body.decode("utf8")
body = json.loads(body)
if "method" not in body:
return "Request must have a method."
if body["method"] == "get":
# Get optional parameters
filters = body.get("filters", {})
cached_limit = body.get("cached_limit", sys.maxsize)
if "url" in body:
return json.dumps(
agent.get(url=body["url"], filters=filters, cached_limit=cached_limit)
)
elif "resource_type" in body:
return json.dumps(
agent.get(
resource_type=body["resource_type"],
filters=filters,
cached_limit=cached_limit,
)
)
else:
return "Must contain url or the resource type"
elif body["method"] == "put":
if "url" in body:
url = body["url"]
else:
return "Put request must contain a url"
if "new_object" in body:
new_object = body["new_object"]
else:
return "Put request must contain the new_object."
return json.dumps(agent.put(url, new_object))
elif body["method"] == "post":
if "url" in body:
url = body["url"]
else:
return "Post request must contain a url"
if "updated_object" in body:
updated_object = body["updated_object"]
else:
return "Put request must contain the updated_object."
return json.dumps(agent.post(url, updated_object))
elif body["method"] == "delete":
if "url" in body:
url = body["url"]
else:
return "Delete request must contain a url"
return json.dumps(agent.delete(url))
else:
return "Method not supported."
if __name__ == "__main__":
app.run(use_reloader=True, port=3000, threaded=True)