Make requests to isolator service asynchronous
This commit is contained in:
20
app.py
20
app.py
@@ -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,9 +175,10 @@ 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(
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Flask==2.2.5
|
||||
Flask[async]==2.2.5
|
||||
gunicorn==19.7.1
|
||||
Jinja2==3.1.0
|
||||
jinja-partials==0.1.1
|
||||
|
||||
Reference in New Issue
Block a user