-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_api.py
More file actions
27 lines (22 loc) · 865 Bytes
/
Copy pathpython_api.py
File metadata and controls
27 lines (22 loc) · 865 Bytes
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
from flask import Flask, request, jsonify
def add(params):
return params['a'] + params['b']
functions_list = [add]
app = Flask(__name__)
@app.route('/<func_name>', methods=['POST'])
def api_root(func_name):
for function in functions_list:
if function.__name__ == func_name:
try:
json_req_data = request.get_json()
if json_req_data:
res = function(json_req_data)
else:
return jsonify({"error": "error in receiving the json input"})
except Exception as e:
return jsonify({"error": "error while running the function"})
return jsonify({"result": res})
output_string = 'function: %s not found' % func_name
return jsonify({"error": output_string})
if __name__ == '__main__':
app.run(host='0.0.0.0')