Skip to content

Commit b5fc7f0

Browse files
committed
add support for Rapid API instance of judge0
- add authenticate() method for adding API_KEY - change base API URLs
1 parent 7ea5b2e commit b5fc7f0

3 files changed

Lines changed: 36 additions & 16 deletions

File tree

coderunner/coderunner.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@
5252
"max_file_size": "1024",
5353
}
5454

55-
API_URL = "https://api.judge0.com/submissions/"
55+
HEADERS = {
56+
"x-rapidapi-host": "judge0.p.rapidapi.com",
57+
"useQueryString": True
58+
}
59+
60+
HOST_URL = "https://judge0.p.rapidapi.com/"
61+
API_URL = "https://judge0.p.rapidapi.com/submissions/"
5662
FIELDS = "?fields=stdout,memory,time,status,stderr,exit_code,created_at"
5763

5864

@@ -135,7 +141,7 @@ def __readStatus(self, token: str):
135141
Check Submission status
136142
"""
137143
while True:
138-
req = urllib.request.Request(API_URL + token["token"] + FIELDS)
144+
req = urllib.request.Request(API_URL + token["token"] + FIELDS, headers=HEADERS)
139145
with urllib.request.urlopen(req) as response:
140146
req = response.read()
141147

@@ -161,13 +167,17 @@ def __submit(self):
161167
api_params["source_code"] = self.source
162168

163169
post_data = urllib.parse.urlencode(api_params).encode("ascii")
164-
req = urllib.request.Request(API_URL, post_data)
170+
req = urllib.request.Request(API_URL, post_data, headers=HEADERS)
165171
with urllib.request.urlopen(req) as response:
166172
req = response.read()
167173
token = json.loads(req.decode("utf-8"))
168174

169175
return token
170176

177+
def authenticate(self, key: str):
178+
self.API_KEY = key
179+
HEADERS["x-rapidapi-key"] = self.API_KEY
180+
171181
def getSubmissionDate(self):
172182
"""Submission date/time of program"""
173183
return self.__response["created_at"]

demo.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
from coderunner import coderunner
22
import pprint
3+
import os
4+
5+
from dotenv import load_dotenv
6+
load_dotenv()
37

48
source_code = "testfiles/" + "test_python_input.py"
59
language = "Python3"
610
output = "testfiles/output/" + "output2.txt"
711
Input = "testfiles/input/" + "input.txt"
12+
13+
14+
API_KEY = os.environ["API_KEY"]
15+
816
r = coderunner.code(source_code, language, output, Input)
17+
r.authenticate(API_KEY)
918

10-
r2 = coderunner.code("print(\"yo\")", "Python3", "YO", path=False)
19+
# r2 = coderunner.code("print(\"yo\")", "Python3", "YO", path=False)
1120

12-
# run the code
21+
# # run the code
1322
r.run()
1423

1524
print("Run r :")
1625
print("Status : " + r.getStatus())
1726

18-
r2.run()
27+
# r2.run()
1928

20-
print("Run r2 :")
21-
print("Status : " + r2.getStatus())
29+
# print("Run r2 :")
30+
# print("Status : " + r2.getStatus())
2231

23-
# check if any error occured
24-
if r.getError() is not None:
25-
pprint.pprint("Error : " + r.getError())
26-
else:
27-
print("Standard Output : ")
28-
pprint.pprint(r.getOutput())
29-
print("Execution Time : " + r.getTime())
30-
print("Memory : " + str(r.getMemory()))
32+
# # check if any error occured
33+
# if r.getError() is not None:
34+
# pprint.pprint("Error : " + r.getError())
35+
# else:
36+
# print("Standard Output : ")
37+
# pprint.pprint(r.getOutput())
38+
# print("Execution Time : " + r.getTime())
39+
# print("Memory : " + str(r.getMemory()))

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ isort
22
black
33
flake8
44
pylint
5+
python-dotenv

0 commit comments

Comments
 (0)