Make requests to isolator service asynchronous
This commit is contained in:
28
app.py
28
app.py
@@ -78,7 +78,7 @@ def documentation():
|
|||||||
return render_template("documentation.html")
|
return render_template("documentation.html")
|
||||||
|
|
||||||
@app.route('/execute', methods=['POST'])
|
@app.route('/execute', methods=['POST'])
|
||||||
def execute_code():
|
async def execute_code():
|
||||||
try:
|
try:
|
||||||
# Extract code and convert request to a format acceptable by Node.js app
|
# Extract code and convert request to a format acceptable by Node.js app
|
||||||
code = request.json.get('code')
|
code = request.json.get('code')
|
||||||
@@ -92,9 +92,10 @@ def execute_code():
|
|||||||
environment = request.json.get('environment_info')
|
environment = request.json.get('environment_info')
|
||||||
environment_json = json.loads(environment)
|
environment_json = json.loads(environment)
|
||||||
|
|
||||||
# Call the Node.js API
|
# Call the Node.js API asynchronously
|
||||||
response = requests.post(API_URL, json={'code': code, 'request': request_obj, 'environment': environment_json, 'name': "anonymous"})
|
async with aiohttp.ClientSession() as session:
|
||||||
response_data = response.json()
|
async with session.post(API_URL, json={'code': code, 'request': request_obj, 'environment': environment_json, 'name': "anonymous"}) as response:
|
||||||
|
response_data = await response.json()
|
||||||
|
|
||||||
# check if playground=true is in the query string
|
# check if playground=true is in the query string
|
||||||
if request.args.get('playground') == 'true':
|
if request.args.get('playground') == 'true':
|
||||||
@@ -104,12 +105,11 @@ def execute_code():
|
|||||||
flask_response = map_isolator_response_to_flask_response(response_data)
|
flask_response = map_isolator_response_to_flask_response(response_data)
|
||||||
return flask_response
|
return flask_response
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({'error': str(e)}), 500
|
return jsonify({'error': str(e)}), 500
|
||||||
|
|
||||||
@app.route('/f/<int:user_id>/<path:function>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD'])
|
@app.route('/f/<int:user_id>/<path:function>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD'])
|
||||||
def execute_http_function(user_id, function):
|
async def execute_http_function(user_id, function):
|
||||||
try:
|
try:
|
||||||
# Split the function_path into the function name and the sub-path
|
# Split the function_path into the function name and the sub-path
|
||||||
parts = function.split('/', 1)
|
parts = function.split('/', 1)
|
||||||
@@ -175,16 +175,17 @@ def execute_http_function(user_id, function):
|
|||||||
if request.data and not request.is_json:
|
if request.data and not request.is_json:
|
||||||
request_data['text'] = request.data.decode('utf-8')
|
request_data['text'] = request.data.decode('utf-8')
|
||||||
|
|
||||||
# Call the Node.js API
|
# Call the Node.js API asynchronously
|
||||||
response = requests.post(API_URL, json={'code': code, 'request': request_data, 'environment': environment, 'name': function_name})
|
async with aiohttp.ClientSession() as session:
|
||||||
response_data = response.json()
|
async with session.post(API_URL, json={'code': code, 'request': request_data, 'environment': environment, 'name': function_name}) as response:
|
||||||
|
response_data = await response.json()
|
||||||
|
|
||||||
db.update_http_function_environment_info_and_invoked_count(user_id, function_name, response_data['environment'])
|
db.update_http_function_environment_info_and_invoked_count(user_id, function_name, response_data['environment'])
|
||||||
db.add_http_function_invocation(
|
db.add_http_function_invocation(
|
||||||
http_function['id'],
|
http_function['id'],
|
||||||
response_data['status'],
|
response_data['status'],
|
||||||
request_data if log_request else {},
|
request_data if log_request else {},
|
||||||
response_data['result'] if (log_response or response_data['status'] != 'SUCCESS') else {},
|
response_data['result'] if (log_response or response_data['status'] != 'SUCCESS') else {},
|
||||||
response_data['logs'],
|
response_data['logs'],
|
||||||
version_number)
|
version_number)
|
||||||
|
|
||||||
@@ -195,7 +196,6 @@ def execute_http_function(user_id, function):
|
|||||||
flask_response = map_isolator_response_to_flask_response(response_data)
|
flask_response = map_isolator_response_to_flask_response(response_data)
|
||||||
return flask_response
|
return flask_response
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({'error': str(e)}), 500
|
return jsonify({'error': str(e)}), 500
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
Flask==2.2.5
|
Flask[async]==2.2.5
|
||||||
gunicorn==19.7.1
|
gunicorn==19.7.1
|
||||||
Jinja2==3.1.0
|
Jinja2==3.1.0
|
||||||
jinja-partials==0.1.1
|
jinja-partials==0.1.1
|
||||||
|
|||||||
Reference in New Issue
Block a user