aboutsummaryrefslogtreecommitdiff
path: root/compile_api.py
diff options
context:
space:
mode:
authorromangraef <roman.graef@gmail.com>2018-07-04 14:08:56 +0200
committerromangraef <roman.graef@gmail.com>2018-07-04 14:08:56 +0200
commit168a8ed88b48b539f09bc77bfad3122e7a986675 (patch)
treeb4e3d2e4cb725fd9aa35ad999fbba7682de12d26 /compile_api.py
downloadevalbot-168a8ed88b48b539f09bc77bfad3122e7a986675.tar.gz
evalbot-168a8ed88b48b539f09bc77bfad3122e7a986675.tar.bz2
evalbot-168a8ed88b48b539f09bc77bfad3122e7a986675.zip
Initial commit
Diffstat (limited to 'compile_api.py')
-rw-r--r--compile_api.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/compile_api.py b/compile_api.py
new file mode 100644
index 0000000..5da9f02
--- /dev/null
+++ b/compile_api.py
@@ -0,0 +1,45 @@
+import asyncio
+from dataclasses import dataclass
+
+import aiohttp
+
+from config import jdoodle_id, jdoodle_secret
+
+EXECUTE_ENDPOINT = "https://api.jdoodle.com/v1/execute"
+
+
+def get_parameters(**kwargs):
+ return dict(
+ clientId=jdoodle_id,
+ clientSecret=jdoodle_secret,
+ **kwargs,
+ )
+
+
+def post(url, data=None, json=None, **kwargs):
+ return asyncio.get_event_loop().run_in_executor(None, lambda *_: requests.post(url, data, json, **kwargs))
+
+
+@dataclass
+class ExecuteResponse(object):
+ output: str
+ cpu_time: float
+ memory: int
+ status_code: int
+
+
+def parse_execute_response(response: dict) -> ExecuteResponse:
+ memory = response['memory']
+ output = response['output']
+ cpu_time = response['cpuTime']
+ status_code = response['statusCode']
+ return ExecuteResponse(output, cpu_time, memory, status_code)
+
+
+async def execute(code: str, language: str, version: str) -> ExecuteResponse:
+ async with aiohttp.ClientSession() as session:
+ response = await session.post(EXECUTE_ENDPOINT, json=get_parameters(
+ script=code,
+ language=language,
+ versionIndex=version))
+ return parse_execute_response(await response.json())