Make requests to isolator service asynchronous

This commit is contained in:
Peter Stockings
2025-07-23 22:11:52 +10:00
parent a4c29e0d8f
commit 54347ded89
2 changed files with 15 additions and 15 deletions

28
app.py
View File

@@ -78,7 +78,7 @@ def documentation():
return render_template("documentation.html")
@app.route('/execute', methods=['POST'])
def execute_code():
async def execute_code():
try:
# Extract code and convert request to a format acceptable by Node.js app
code = request.json.get('code')
@@ -92,9 +92,10 @@ def execute_code():
environment = request.json.get('environment_info')
environment_json = json.loads(environment)
# Call the Node.js API
response = requests.post(API_URL, json={'code': code, 'request': request_obj, 'environment': environment_json, 'name': "anonymous"})
response_data = response.json()
# Call the Node.js API asynchronously
async with aiohttp.ClientSession() as session:
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
if request.args.get('playground') == 'true':
@@ -104,12 +105,11 @@ def execute_code():
flask_response = map_isolator_response_to_flask_response(response_data)
return flask_response
except Exception as e:
return jsonify({'error': str(e)}), 500
@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:
# Split the function_path into the function name and the sub-path
parts = function.split('/', 1)
@@ -175,16 +175,17 @@ def execute_http_function(user_id, function):
if request.data and not request.is_json:
request_data['text'] = request.data.decode('utf-8')
# Call the Node.js API
response = requests.post(API_URL, json={'code': code, 'request': request_data, 'environment': environment, 'name': function_name})
response_data = response.json()
# Call the Node.js API asynchronously
async with aiohttp.ClientSession() as session:
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.add_http_function_invocation(
http_function['id'],
response_data['status'],
request_data if log_request else {},
response_data['result'] if (log_response or response_data['status'] != 'SUCCESS') else {},
http_function['id'],
response_data['status'],
request_data if log_request else {},
response_data['result'] if (log_response or response_data['status'] != 'SUCCESS') else {},
response_data['logs'],
version_number)
@@ -195,7 +196,6 @@ def execute_http_function(user_id, function):
flask_response = map_isolator_response_to_flask_response(response_data)
return flask_response
except Exception as e:
return jsonify({'error': str(e)}), 500