Skip to content

Commit dd3ba0c

Browse files
Add support for Judge0 Cloud client
1 parent 59175c5 commit dd3ba0c

File tree

4 files changed

+112
-8
lines changed

4 files changed

+112
-8
lines changed

src/judge0/__init__.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
ATDJudge0CE,
2020
ATDJudge0ExtraCE,
2121
Client,
22+
Judge0Cloud,
23+
Judge0CloudCE,
24+
Judge0CloudExtraCE,
2225
Rapid,
2326
RapidJudge0CE,
2427
RapidJudge0ExtraCE,
@@ -39,6 +42,9 @@
3942
"Client",
4043
"File",
4144
"Filesystem",
45+
"Judge0Cloud",
46+
"Judge0CloudCE",
47+
"Judge0CloudExtraCE",
4248
"Language",
4349
"LanguageAlias",
4450
"MaxRetries",
@@ -54,12 +60,12 @@
5460
"SuluJudge0ExtraCE",
5561
"TestCase",
5662
"async_execute",
63+
"async_run",
5764
"execute",
5865
"get_client",
59-
"async_run",
60-
"sync_run",
6166
"run",
6267
"sync_execute",
68+
"sync_run",
6369
"wait",
6470
]
6571

@@ -122,11 +128,9 @@ def _get_preview_client(flavor: Flavor) -> Union[SuluJudge0CE, SuluJudge0ExtraCE
122128
def _get_custom_client(flavor: Flavor) -> Union[Client, None]:
123129
from json import loads
124130

125-
ce_endpoint = os.getenv("JUDGE0_CE_ENDPOINT", "https://ce.judge0.com")
131+
ce_endpoint = os.getenv("JUDGE0_CE_ENDPOINT")
126132
ce_auth_header = os.getenv("JUDGE0_CE_AUTH_HEADERS")
127-
extra_ce_endpoint = os.getenv(
128-
"JUDGE0_EXTRA_CE_ENDPOINT", "https://extra-ce.judge0.com"
129-
)
133+
extra_ce_endpoint = os.getenv("JUDGE0_EXTRA_CE_ENDPOINT")
130134
extra_ce_auth_header = os.getenv("JUDGE0_EXTRA_CE_AUTH_HEADERS")
131135

132136
if flavor == Flavor.CE and ce_endpoint is not None and ce_auth_header is not None:

src/judge0/clients.py

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,5 +715,77 @@ def __init__(self, api_key=None, **kwargs):
715715
super().__init__(self.DEFAULT_ENDPOINT, api_key, **kwargs)
716716

717717

718-
CE = (SuluJudge0CE, RapidJudge0CE, ATDJudge0CE)
719-
EXTRA_CE = (SuluJudge0ExtraCE, RapidJudge0ExtraCE, ATDJudge0ExtraCE)
718+
class Judge0Cloud(Client):
719+
"""Base class for all Judge0 Cloud clients.
720+
721+
Parameters
722+
----------
723+
endpoint : str
724+
Default request endpoint.
725+
auth_headers : str or dict
726+
Judge0 Cloud authentication headers, either as a JSON string or a dictionary.
727+
**kwargs : dict
728+
Additional keyword arguments for the base Client.
729+
"""
730+
731+
def __init__(self, endpoint, auth_headers, **kwargs):
732+
if isinstance(auth_headers, str):
733+
from json import loads
734+
735+
auth_headers = loads(auth_headers)
736+
737+
super().__init__(
738+
endpoint,
739+
auth_headers,
740+
**kwargs,
741+
)
742+
743+
744+
class Judge0CloudCE(Judge0Cloud):
745+
"""Judge0 Cloud client for CE flavor.
746+
747+
Parameters
748+
----------
749+
endpoint : str
750+
Default request endpoint.
751+
auth_headers : str or dict
752+
Judge0 Cloud authentication headers, either as a JSON string or a dictionary.
753+
**kwargs : dict
754+
Additional keyword arguments for the base Client.
755+
"""
756+
757+
DEFAULT_ENDPOINT: ClassVar[str] = "https://ce.judge0.com"
758+
HOME_URL: ClassVar[str] = "https://ce.judge0.com"
759+
API_KEY_ENV: ClassVar[str] = "JUDGE0_CLOUD_CE_AUTH_HEADERS"
760+
761+
def __init__(self, auth_headers, **kwargs):
762+
super().__init__(
763+
self.DEFAULT_ENDPOINT,
764+
auth_headers,
765+
**kwargs,
766+
)
767+
768+
769+
class Judge0CloudExtraCE(Judge0Cloud):
770+
"""Judge0 Cloud client for Extra CE flavor.
771+
772+
Parameters
773+
----------
774+
endpoint : str
775+
Default request endpoint.
776+
auth_headers : str or dict
777+
Judge0 Cloud authentication headers, either as a JSON string or a dictionary.
778+
**kwargs : dict
779+
Additional keyword arguments for the base Client.
780+
"""
781+
782+
DEFAULT_ENDPOINT: ClassVar[str] = "https://extra-ce.judge0.com"
783+
HOME_URL: ClassVar[str] = "https://extra-ce.judge0.com"
784+
API_KEY_ENV: ClassVar[str] = "JUDGE0_CLOUD_EXTRA_CE_AUTH_HEADERS"
785+
786+
def __init__(self, auth_headers, **kwargs):
787+
super().__init__(self.DEFAULT_ENDPOINT, auth_headers, **kwargs)
788+
789+
790+
CE = (Judge0CloudCE, SuluJudge0CE, RapidJudge0CE, ATDJudge0CE)
791+
EXTRA_CE = (Judge0CloudExtraCE, SuluJudge0ExtraCE, RapidJudge0ExtraCE, ATDJudge0ExtraCE)

tests/conftest.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ def sulu_extra_ce_client():
9191
return clients.SuluJudge0ExtraCE(api_key)
9292

9393

94+
@pytest.fixture(scope="session")
95+
def judge0_cloud_ce_client():
96+
auth_headers = os.getenv("JUDGE0_CLOUD_CE_AUTH_HEADERS")
97+
98+
if auth_headers is None:
99+
return None
100+
else:
101+
return clients.Judge0CloudCE(auth_headers)
102+
103+
104+
@pytest.fixture(scope="session")
105+
def judge0_cloud_extra_ce_client():
106+
auth_headers = os.getenv("JUDGE0_CLOUD_EXTRA_CE_AUTH_HEADERS")
107+
108+
if auth_headers is None:
109+
return None
110+
else:
111+
return clients.Judge0CloudExtraCE(auth_headers)
112+
113+
94114
@pytest.fixture(scope="session")
95115
def preview_ce_client() -> clients.SuluJudge0CE:
96116
return clients.SuluJudge0CE(retry_strategy=RegularPeriodRetry(0.5))
@@ -104,13 +124,16 @@ def preview_extra_ce_client() -> clients.SuluJudge0ExtraCE:
104124
@pytest.fixture(scope="session")
105125
def ce_client(
106126
custom_ce_client,
127+
judge0_cloud_ce_client,
107128
sulu_ce_client,
108129
rapid_ce_client,
109130
atd_ce_client,
110131
preview_ce_client,
111132
):
112133
if custom_ce_client is not None:
113134
return custom_ce_client
135+
if judge0_cloud_ce_client is not None:
136+
return judge0_cloud_ce_client
114137
if sulu_ce_client is not None:
115138
return sulu_ce_client
116139
if rapid_ce_client is not None:
@@ -126,13 +149,16 @@ def ce_client(
126149
@pytest.fixture(scope="session")
127150
def extra_ce_client(
128151
custom_extra_ce_client,
152+
judge0_cloud_extra_ce_client,
129153
sulu_extra_ce_client,
130154
rapid_extra_ce_client,
131155
atd_extra_ce_client,
132156
preview_extra_ce_client,
133157
):
134158
if custom_extra_ce_client is not None:
135159
return custom_extra_ce_client
160+
if judge0_cloud_extra_ce_client is not None:
161+
return judge0_cloud_extra_ce_client
136162
if sulu_extra_ce_client is not None:
137163
return sulu_extra_ce_client
138164
if rapid_extra_ce_client is not None:

tests/test_clients.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"rapid_extra_ce_client",
88
"sulu_ce_client",
99
"sulu_extra_ce_client",
10+
"judge0_cloud_ce_client",
11+
"judge0_cloud_extra_ce_client",
1012
)
1113

1214

0 commit comments

Comments
 (0)