Skip to content

Commit

Permalink
Minor refactor of _create_default_ce_client. Use raise from when auth…
Browse files Browse the repository at this point in the history
…entication fails in Client.__init__.
  • Loading branch information
fkdosilovic committed Nov 4, 2024
1 parent bfd1222 commit 7253a7f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
38 changes: 10 additions & 28 deletions src/judge0/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
import os

from .api import async_execute, execute, sync_execute, wait
from .clients import (
ATDJudge0CE,
ATDJudge0ExtraCE,
Client,
RapidJudge0CE,
RapidJudge0ExtraCE,
SuluJudge0CE,
SuluJudge0ExtraCE,
)
from .clients import CE, Client, EXTRA_CE
from .retry import MaxRetries, MaxWaitTime, RegularPeriodRetry
from .submission import Submission

__all__ = [
ATDJudge0CE,
ATDJudge0ExtraCE,
Client,
RapidJudge0CE,
RapidJudge0ExtraCE,
SuluJudge0CE,
SuluJudge0ExtraCE,
*CE,
*EXTRA_CE,
Submission,
RegularPeriodRetry,
MaxRetries,
Expand All @@ -40,19 +28,13 @@ def _create_default_ce_client():
except: # noqa: E722
pass

rapid_api_key = os.getenv("JUDGE0_RAPID_API_KEY")
sulu_api_key = os.getenv("JUDGE0_SULU_API_KEY")
atd_api_key = os.getenv("JUDGE0_ATD_API_KEY")

if rapid_api_key is not None:
client = RapidJudge0CE(api_key=rapid_api_key)
elif sulu_api_key is not None:
client = SuluJudge0CE(api_key=sulu_api_key)
elif atd_api_key is not None:
client = ATDJudge0CE(api_key=atd_api_key)
else:
# TODO: Create SuluJudge0CE with default client.
client = None
client = None
for client_class in CE:
api_key = os.getenv(client_class.API_KEY_ENV)
if api_key is not None:
client = client_class(api_key)

# TODO: If client is none, instantiate the default client.

globals()["judge0_default_client"] = client

Expand Down
24 changes: 19 additions & 5 deletions src/judge0/clients.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
from typing import Iterable, Optional, Union
from typing import Iterable, Union

import requests

from .retry import RegularPeriodRetry, RetryMechanism

from .submission import Submission


class Client:
API_KEY_ENV = "JUDGE0_API_KEY"

def __init__(self, endpoint, auth_headers) -> None:
self.endpoint = endpoint
self.auth_headers = auth_headers

try:
self.languages = {lang["id"]: lang for lang in self.get_languages()}
except Exception:
raise RuntimeError("Client authentication failed.")
except Exception as e:
raise RuntimeError("Client authentication failed.") from e

def get_about(self) -> dict:
r = requests.get(
Expand Down Expand Up @@ -172,6 +173,8 @@ def get_submissions(


class ATD(Client):
API_KEY_ENV = "JUDGE0_ATD_API_KEY"

def __init__(self, endpoint, host_header_value, api_key):
self.api_key = api_key
super().__init__(
Expand Down Expand Up @@ -323,6 +326,8 @@ def get_submissions(


class Rapid(Client):
API_KEY_ENV = "JUDGE0_RAPID_API_KEY"

def __init__(self, endpoint, host_header_value, api_key):
self.api_key = api_key
super().__init__(
Expand Down Expand Up @@ -359,6 +364,8 @@ def __init__(self, api_key):


class Sulu(Client):
API_KEY_ENV = "JUDGE0_SULU_API_KEY"

def __init__(self, endpoint, api_key):
self.api_key = api_key
super().__init__(endpoint, {"Authorization": f"Bearer {api_key}"})
Expand All @@ -376,3 +383,10 @@ class SuluJudge0ExtraCE(Sulu):

def __init__(self, api_key):
super().__init__(self.DEFAULT_ENDPOINT, api_key=api_key)


DEFAULT_CE_CLIENT_CLASS = SuluJudge0CE
DEFAULT_EXTRA_CE_CLASS = SuluJudge0ExtraCE

CE = [RapidJudge0CE, SuluJudge0CE, ATDJudge0CE]
EXTRA_CE = [RapidJudge0ExtraCE, SuluJudge0ExtraCE, ATDJudge0ExtraCE]

0 comments on commit 7253a7f

Please sign in to comment.