Skip to content

Commit 7253a7f

Browse files
committed
Minor refactor of _create_default_ce_client. Use raise from when authentication fails in Client.__init__.
1 parent bfd1222 commit 7253a7f

File tree

2 files changed

+29
-33
lines changed

2 files changed

+29
-33
lines changed

src/judge0/__init__.py

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
import os
22

33
from .api import async_execute, execute, sync_execute, wait
4-
from .clients import (
5-
ATDJudge0CE,
6-
ATDJudge0ExtraCE,
7-
Client,
8-
RapidJudge0CE,
9-
RapidJudge0ExtraCE,
10-
SuluJudge0CE,
11-
SuluJudge0ExtraCE,
12-
)
4+
from .clients import CE, Client, EXTRA_CE
135
from .retry import MaxRetries, MaxWaitTime, RegularPeriodRetry
146
from .submission import Submission
157

168
__all__ = [
17-
ATDJudge0CE,
18-
ATDJudge0ExtraCE,
199
Client,
20-
RapidJudge0CE,
21-
RapidJudge0ExtraCE,
22-
SuluJudge0CE,
23-
SuluJudge0ExtraCE,
10+
*CE,
11+
*EXTRA_CE,
2412
Submission,
2513
RegularPeriodRetry,
2614
MaxRetries,
@@ -40,19 +28,13 @@ def _create_default_ce_client():
4028
except: # noqa: E722
4129
pass
4230

43-
rapid_api_key = os.getenv("JUDGE0_RAPID_API_KEY")
44-
sulu_api_key = os.getenv("JUDGE0_SULU_API_KEY")
45-
atd_api_key = os.getenv("JUDGE0_ATD_API_KEY")
46-
47-
if rapid_api_key is not None:
48-
client = RapidJudge0CE(api_key=rapid_api_key)
49-
elif sulu_api_key is not None:
50-
client = SuluJudge0CE(api_key=sulu_api_key)
51-
elif atd_api_key is not None:
52-
client = ATDJudge0CE(api_key=atd_api_key)
53-
else:
54-
# TODO: Create SuluJudge0CE with default client.
55-
client = None
31+
client = None
32+
for client_class in CE:
33+
api_key = os.getenv(client_class.API_KEY_ENV)
34+
if api_key is not None:
35+
client = client_class(api_key)
36+
37+
# TODO: If client is none, instantiate the default client.
5638

5739
globals()["judge0_default_client"] = client
5840

src/judge0/clients.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
from typing import Iterable, Optional, Union
1+
from typing import Iterable, Union
22

33
import requests
44

5-
from .retry import RegularPeriodRetry, RetryMechanism
6-
75
from .submission import Submission
86

97

108
class Client:
9+
API_KEY_ENV = "JUDGE0_API_KEY"
10+
1111
def __init__(self, endpoint, auth_headers) -> None:
1212
self.endpoint = endpoint
1313
self.auth_headers = auth_headers
14+
1415
try:
1516
self.languages = {lang["id"]: lang for lang in self.get_languages()}
16-
except Exception:
17-
raise RuntimeError("Client authentication failed.")
17+
except Exception as e:
18+
raise RuntimeError("Client authentication failed.") from e
1819

1920
def get_about(self) -> dict:
2021
r = requests.get(
@@ -172,6 +173,8 @@ def get_submissions(
172173

173174

174175
class ATD(Client):
176+
API_KEY_ENV = "JUDGE0_ATD_API_KEY"
177+
175178
def __init__(self, endpoint, host_header_value, api_key):
176179
self.api_key = api_key
177180
super().__init__(
@@ -323,6 +326,8 @@ def get_submissions(
323326

324327

325328
class Rapid(Client):
329+
API_KEY_ENV = "JUDGE0_RAPID_API_KEY"
330+
326331
def __init__(self, endpoint, host_header_value, api_key):
327332
self.api_key = api_key
328333
super().__init__(
@@ -359,6 +364,8 @@ def __init__(self, api_key):
359364

360365

361366
class Sulu(Client):
367+
API_KEY_ENV = "JUDGE0_SULU_API_KEY"
368+
362369
def __init__(self, endpoint, api_key):
363370
self.api_key = api_key
364371
super().__init__(endpoint, {"Authorization": f"Bearer {api_key}"})
@@ -376,3 +383,10 @@ class SuluJudge0ExtraCE(Sulu):
376383

377384
def __init__(self, api_key):
378385
super().__init__(self.DEFAULT_ENDPOINT, api_key=api_key)
386+
387+
388+
DEFAULT_CE_CLIENT_CLASS = SuluJudge0CE
389+
DEFAULT_EXTRA_CE_CLASS = SuluJudge0ExtraCE
390+
391+
CE = [RapidJudge0CE, SuluJudge0CE, ATDJudge0CE]
392+
EXTRA_CE = [RapidJudge0ExtraCE, SuluJudge0ExtraCE, ATDJudge0ExtraCE]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy