-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
49 lines (33 loc) · 1.34 KB
/
server.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
from flask import Flask, request, jsonify
import json
from music import Albums, Songs, all_functions
from utils import match_functions_with_metadata, convert_args, handle_function, FunctionExecutionError, FunctionNotFoundError
app = Flask(__name__)
# data and APIs
Albums.load_albums("./data/popular_albums.csv")
Songs.load_songs("./data/tracks.csv")
with open('functions.json', 'r') as file:
function_metadata = json.load(file)
functions = match_functions_with_metadata(
all_functions, function_metadata)
@app.route('/function_call/<function_name>', methods=['GET'])
def function_call(function_name):
try:
print(functions)
converted_args = convert_args(function_name, functions)
except FunctionNotFoundError as e: # TODO conversion error
return jsonify({"error": e}), 500
class_method = functions[function_name].function
cls = class_method.cls
function = class_method.method
try:
result = handle_function(cls=cls, function=function, **converted_args)
return jsonify({"result": result})
except FunctionExecutionError as e:
return jsonify({"error": e})
@app.route('/functions/all', methods=['GET'])
def functions_all():
print(all_functions)
return jsonify({"result": sorted([name for _, name, _ in all_functions])})
if __name__ == '__main__':
app.run(debug=True)