Skip to content

Commit 49ef11f

Browse files
committed
Initial implementation (without submission endpoints) of Sulu clients.
1 parent ff50b56 commit 49ef11f

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

examples/sulu_clients.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
3+
import judge0
4+
from dotenv import load_dotenv
5+
6+
load_dotenv()
7+
8+
sulu_auth_token = os.getenv("SULU_API_KEY")
9+
10+
client_ce = judge0.SuluCEClient(auth_token=sulu_auth_token)
11+
print(client_ce.get_about())
12+
print(client_ce.get_config_info())
13+
print(client_ce.get_statuses())
14+
print(client_ce.get_languages())
15+
16+
client_extra_ce = judge0.SuluExtraCEClient(auth_token=sulu_auth_token)
17+
print(client_extra_ce.get_about())
18+
print(client_extra_ce.get_config_info())
19+
print(client_extra_ce.get_statuses())
20+
print(client_extra_ce.get_languages())

src/judge0/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .clients import SuluCEClient, SuluExtraCEClient
2+
3+
__all__ = [SuluCEClient, SuluExtraCEClient]

src/judge0/base.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from enum import IntEnum
2+
3+
DEFAULT_SULU_CE_ENDPOINT: str = "https://judge0-ce.p.sulu.sh"
4+
DEFAULT_SULU_EXTRA_CE_ENDPOINT: str = "https://judge0-extra-ce.p.sulu.sh"
5+
6+
7+
class Status(IntEnum):
8+
IN_QUEUE = 1
9+
PROCESSING = 2
10+
ACCEPTED = 3
11+
WRONG_ANSWER = 4
12+
TIME_LIMIT_EXCEEDED = 5
13+
COMPILATION_ERROR = 6
14+
RUNTIME_ERROR_SIGSEGV = 7
15+
RUNTIME_ERROR_SIGXFSZ = 8
16+
RUNTIME_ERROR_SIGFPE = 9
17+
RUNTIME_ERROR_SIGABRT = 10
18+
RUNTIME_ERROR_NZEC = 11
19+
RUNTIME_ERROR_OTHER = 12
20+
INTERNAL_ERROR = 13
21+
EXEC_FORMAT_ERROR = 14

src/judge0/clients.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from abc import abstractmethod
2+
from typing import Union
3+
4+
import requests
5+
6+
from .base import DEFAULT_SULU_CE_ENDPOINT, DEFAULT_SULU_EXTRA_CE_ENDPOINT
7+
8+
9+
class BaseSuluClient:
10+
11+
def __init__(
12+
self,
13+
*,
14+
endpoint: Union[str, None] = None,
15+
auth_token: Union[str, None] = None,
16+
):
17+
if endpoint is None:
18+
endpoint = self.default_endpoint
19+
self.endpoint = endpoint
20+
self.auth_token = auth_token
21+
self._session = requests.Session()
22+
23+
def get_about(self) -> dict:
24+
# TODO: Potentially think about caching the successful return.
25+
headers = {"Authorization": f"Bearer {self.auth_token}"}
26+
r = requests.get(f"{self.endpoint}/about", headers=headers)
27+
r.raise_for_status()
28+
self._session.headers.update(headers)
29+
return r.json()
30+
31+
def get_config_info(self) -> dict:
32+
# TODO: Potentially think about caching the successful return.
33+
headers = {"Authorization": f"Bearer {self.auth_token}"}
34+
r = requests.get(f"{self.endpoint}/config_info", headers=headers)
35+
r.raise_for_status()
36+
self._session.headers.update(headers)
37+
return r.json()
38+
39+
def get_statuses(self) -> list[dict]:
40+
# TODO: Potentially think about caching the successful return.
41+
# TODO: Add docs about Status enum.
42+
headers = {"Authorization": f"Bearer {self.auth_token}"}
43+
r = requests.get(f"{self.endpoint}/statuses", headers=headers)
44+
r.raise_for_status()
45+
self._session.headers.update(headers)
46+
return r.json()
47+
48+
def get_languages(self, *, language_id: Union[int, None] = None) -> list[dict]:
49+
# TODO: Potentially think about caching the successful return.
50+
headers = {"Authorization": f"Bearer {self.auth_token}"}
51+
request_url = f"{self.endpoint}/languages"
52+
if language_id is not None:
53+
request_url = f"{request_url}/{language_id}"
54+
r = requests.get(request_url, headers=headers)
55+
r.raise_for_status()
56+
self._session.headers.update(headers)
57+
return r.json()
58+
59+
@property
60+
@abstractmethod
61+
def default_endpoint(self) -> str:
62+
raise NotImplementedError("Subclasses must define a default endpoint property.")
63+
64+
65+
class SuluCEClient(BaseSuluClient):
66+
67+
@property
68+
def default_endpoint(self):
69+
return DEFAULT_SULU_CE_ENDPOINT
70+
71+
72+
class SuluExtraCEClient(BaseSuluClient):
73+
74+
@property
75+
def default_endpoint(self):
76+
return DEFAULT_SULU_EXTRA_CE_ENDPOINT

0 commit comments

Comments
 (0)