1
1
import os
2
2
3
+ from typing import Union
4
+
3
5
from .api import (
4
6
async_execute ,
5
7
async_run ,
@@ -73,36 +75,25 @@ def _get_implicit_client(flavor: Flavor) -> Client:
73
75
if flavor == Flavor .EXTRA_CE and JUDGE0_IMPLICIT_EXTRA_CE_CLIENT is not None :
74
76
return JUDGE0_IMPLICIT_EXTRA_CE_CLIENT
75
77
76
- from .clients import CE , EXTRA_CE
77
-
78
78
try :
79
79
from dotenv import load_dotenv
80
80
81
81
load_dotenv ()
82
82
except : # noqa: E722
83
83
pass
84
84
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 )
89
87
90
88
# Try to find one of the predefined keys JUDGE0_{SULU,RAPID,ATD}_API_KEY
91
89
# 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 )
98
92
99
93
# If we didn't find any of the possible predefined keys, initialize
100
94
# the preview Sulu client based on the flavor.
101
95
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 )
106
97
107
98
if flavor == Flavor .CE :
108
99
JUDGE0_IMPLICIT_CE_CLIENT = client
@@ -112,6 +103,57 @@ def _get_implicit_client(flavor: Flavor) -> Client:
112
103
return client
113
104
114
105
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
+
115
157
CE = Flavor .CE
116
158
EXTRA_CE = Flavor .EXTRA_CE
117
159
0 commit comments