11import os
22
3+ from typing import Union
4+
35from .api import (
46 async_execute ,
57 async_run ,
@@ -73,36 +75,25 @@ def _get_implicit_client(flavor: Flavor) -> Client:
7375 if flavor == Flavor .EXTRA_CE and JUDGE0_IMPLICIT_EXTRA_CE_CLIENT is not None :
7476 return JUDGE0_IMPLICIT_EXTRA_CE_CLIENT
7577
76- from .clients import CE , EXTRA_CE
77-
7878 try :
7979 from dotenv import load_dotenv
8080
8181 load_dotenv ()
8282 except : # noqa: E722
8383 pass
8484
85- if flavor == Flavor .CE :
86- client_classes = CE
87- else :
88- client_classes = EXTRA_CE
85+ # Let's check if we can find a self-hosted client.
86+ client = _get_custom_client (flavor )
8987
9088 # Try to find one of the predefined keys JUDGE0_{SULU,RAPID,ATD}_API_KEY
9189 # in environment variables.
92- client = None
93- for client_class in client_classes :
94- api_key = os .getenv (client_class .API_KEY_ENV )
95- if api_key is not None :
96- client = client_class (api_key )
97- break
90+ if client is None :
91+ client = _get_predefined_client (flavor )
9892
9993 # If we didn't find any of the possible predefined keys, initialize
10094 # the preview Sulu client based on the flavor.
10195 if client is None :
102- if flavor == Flavor .CE :
103- client = SuluJudge0CE (retry_strategy = RegularPeriodRetry (0.5 ))
104- else :
105- client = SuluJudge0ExtraCE (retry_strategy = RegularPeriodRetry (0.5 ))
96+ client = _get_preview_client (flavor )
10697
10798 if flavor == Flavor .CE :
10899 JUDGE0_IMPLICIT_CE_CLIENT = client
@@ -112,6 +103,57 @@ def _get_implicit_client(flavor: Flavor) -> Client:
112103 return client
113104
114105
106+ def _get_preview_client (flavor : Flavor ) -> Union [SuluJudge0CE , SuluJudge0ExtraCE ]:
107+ if flavor == Flavor .CE :
108+ return SuluJudge0CE (retry_strategy = RegularPeriodRetry (0.5 ))
109+ else :
110+ return SuluJudge0ExtraCE (retry_strategy = RegularPeriodRetry (0.5 ))
111+
112+
113+ def _get_custom_client (flavor : Flavor ) -> Union [Client , None ]:
114+ ce_endpoint = os .getenv ("JUDGE0_CE_ENDPOINT" )
115+ ce_auth_header = os .getenv ("JUDGE0_CE_AUTH_HEADERS" )
116+ extra_ce_endpoint = os .getenv ("JUDGE0_EXTRA_CE_ENDPOINT" )
117+ extra_ce_auth_header = os .getenv ("JUDGE0_EXTRA_CE_AUTH_HEADERS" )
118+
119+ if flavor == Flavor .CE and ce_endpoint is not None and ce_auth_header is not None :
120+ return Client (
121+ endpoint = ce_endpoint ,
122+ auth_headers = eval (ce_auth_header ),
123+ )
124+
125+ if (
126+ flavor == Flavor .EXTRA_CE
127+ and extra_ce_endpoint is not None
128+ and extra_ce_auth_header is not None
129+ ):
130+ return Client (
131+ endpoint = extra_ce_endpoint ,
132+ auth_headers = eval (extra_ce_auth_header ),
133+ )
134+
135+ return None
136+
137+
138+ def _get_predefined_client (flavor : Flavor ) -> Union [Client , None ]:
139+ from .clients import CE , EXTRA_CE
140+
141+ if flavor == Flavor .CE :
142+ client_classes = CE
143+ else :
144+ client_classes = EXTRA_CE
145+
146+ for client_class in client_classes :
147+ api_key = os .getenv (client_class .API_KEY_ENV )
148+ if api_key is not None :
149+ client = client_class (api_key )
150+ break
151+ else :
152+ client = None
153+
154+ return client
155+
156+
115157CE = Flavor .CE
116158EXTRA_CE = Flavor .EXTRA_CE
117159
0 commit comments