diff options
Diffstat (limited to 'instances.py')
-rw-r--r-- | instances.py | 56 |
1 files changed, 33 insertions, 23 deletions
diff --git a/instances.py b/instances.py index c57d164..f7f0774 100644 --- a/instances.py +++ b/instances.py @@ -1,26 +1,25 @@ # importing the global modules import pexpect import os +import os.path import re import time import sys import psutil import requests -import json import urllib.request import base64 - +import simplejson as json from datetime import datetime from django.template.loader import render_to_string, get_template from django.core.mail import EmailMultiAlternatives # importing the local variables from R_on_Cloud.settings import PROJECT_DIR -from R_on_Cloud.config import (BIN, API_URL, API_URL_PLOT) +from R_on_Cloud.config import (BIN, API_URL, TEMP_DIR, AUTH_KEY) -def execute_code(code, session_id, R_file_id): - #session_id = self.request.session['session_id'] +def execute_code(code, user_id, R_file_id): # Check for system commands system_commands = re.compile( @@ -37,26 +36,37 @@ def execute_code(code, session_id, R_file_id): code = re.sub(r"View\(", "print(", code) body = { - 'code': code, - 'session_id': session_id, - 'R_file_id': R_file_id, + 'code': code, + 'user_id': user_id, + 'R_file_id': R_file_id, } - req = urllib.request.Request(API_URL) - req.add_header('Content-Type', 'application/json; charset=utf-8') - jsondata = json.dumps(body) - jsondataasbytes = jsondata.encode('utf-8') - req.add_header('Content-Length', len(jsondataasbytes)) - print(jsondataasbytes) - result = urllib.request.urlopen(req, jsondataasbytes) - result = result.read().decode("utf8") - output = json.loads(result)["output"] - graph_exist = json.loads(result)["graph_exist"] - graph_path = API_URL_PLOT + "?session_id=" + session_id +"&R_file_id="+ R_file_id - data = { - 'output': output, - 'graph_exist': graph_exist, - 'graph_path': graph_path, + headers = { + "Content-Type": "application/json", + "Accept": "application/json", + "X-Api-Key": AUTH_KEY, } + jsondata = json.dumps(body) + if not os.path.exists(TEMP_DIR): + os.makedirs(TEMP_DIR) + result = requests.post(API_URL, json=jsondata, headers=headers) + output = result.json() + output_status = json.loads(json.dumps(output['status'])) + if output_status != '400': + output_data = json.loads(json.dumps(output['data'])) + output_error = json.loads(json.dumps(output['error'])) + plot_exist = json.loads(json.dumps(output['is_plot'])) + plot_path_req = json.loads(json.dumps(output['plot_path_req'])) + + data = { + 'output': output_data, + 'error': output_error, + 'plot_exist': plot_exist, + 'plot_path': plot_path_req, + } + else: + data = {'error': "Invalid request, please try after some time {0}" + .format(output_status) + } return data |