Skip to content

Commit e5074eb

Browse files
committed
Consolidate API calls in submit client.
1 parent 17d1b56 commit e5074eb

File tree

1 file changed

+22
-50
lines changed

1 file changed

+22
-50
lines changed

submit/submit

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,16 @@ def read_problems() -> list:
186186
return problems
187187

188188

189-
def do_api_request(name: str):
189+
def do_api_request(endpoint: str, data=None, files=None):
190190
'''Perform an API call to the given endpoint and return its data.
191191
192192
Parameters:
193-
name (str): the endpoint to call
193+
endpoint (str): the endpoint to call
194+
data (dict): optional data to send in the POST request
195+
files (list): optional files to send in the POST request
194196
195197
Returns:
196-
The endpoint contents.
198+
The decoded endpoint contents.
197199
198200
Raises:
199201
RuntimeError when the response is not JSON or the HTTP status code is non 2xx.
@@ -202,24 +204,32 @@ def do_api_request(name: str):
202204
if not baseurl:
203205
raise RuntimeError('No baseurl set')
204206

205-
url = f'{baseurl}api/{api_version}{name}'
207+
url = f'{baseurl}api/{api_version}{endpoint}'
206208

207209
logging.info(f'Connecting to {url}')
208210

209211
try:
210-
response = requests.get(url, headers=headers)
212+
if data or files:
213+
response = requests.post(url, data=data, files=files, headers=headers)
214+
else:
215+
response = requests.get(url, headers=headers)
211216
except requests.exceptions.RequestException as e:
212217
raise RuntimeError(e)
213218

214-
logging.debug(f"API call '{name}' returned:\n{response.text}")
219+
logging.debug(f"API call '{endpoint}' returned:\n{response.text}")
215220
if response.status_code >= 300:
216221
print_if_json(response.text)
217222
if response.status_code == 401:
218223
raise RuntimeError('Authentication failed, please check your DOMjudge credentials in ~/.netrc.')
219224
else:
220-
raise RuntimeError(f'API request {name} failed (code {response.status_code}).')
225+
raise RuntimeError(f'API request {endpoint} failed (code {response.status_code}).')
226+
227+
try:
228+
decoded_response = json.loads(response.text)
229+
except json.decoder.JSONDecodeError as e:
230+
raise RuntimeError(f'Parsing DOMjudge\'s API output failed: {e}')
221231

222-
return json.loads(response.text)
232+
return decoded_response
223233

224234

225235
def kotlin_base_entry_point(filebase: str) -> str:
@@ -330,27 +340,8 @@ def do_api_print():
330340
if entry_point:
331341
data['entry_point'] = entry_point
332342

333-
url = f"{baseurl}api/{api_version}printing/team"
334-
logging.info(f'connecting to {url}')
335-
336-
response = requests.post(url, data=data, headers=headers)
337-
338-
# The connection worked, but we may have received an HTTP error
339-
logging.debug(f"API printing call returned:\n{response.text}")
340-
if response.status_code >= 300:
341-
print_if_json(response.text)
342-
if response.status_code == 401:
343-
raise RuntimeError('Authentication failed, please check your DOMjudge credentials in ~/.netrc.')
344-
else:
345-
raise RuntimeError(f'Printing failed (code {response.status_code})')
346-
347-
# We got a successful HTTP response. It worked.
348-
# But check that we indeed received a success response.
349-
350-
try:
351-
result = json.loads(response.text)
352-
except json.decoder.JSONDecodeError as e:
353-
error(f'Parsing DOMjudge\'s API output failed: {e}')
343+
endpoint = f"printing/team"
344+
result = do_api_request(endpoint, data)
354345

355346
if not isinstance(result, dict) or 'success' not in result:
356347
error('DOMjudge\'s API returned unexpected JSON data.')
@@ -374,27 +365,8 @@ def do_api_submit():
374365

375366
files = [('code[]', open(filename, 'rb')) for filename in filenames]
376367

377-
url = f"{baseurl}api/{api_version}contests/{my_contest['id']}/submissions"
378-
logging.info(f'connecting to {url}')
379-
380-
response = requests.post(url, data=data, files=files, headers=headers)
381-
382-
# The connection worked, but we may have received an HTTP error
383-
logging.debug(f"API submitting call returned:\n{response.text}")
384-
if response.status_code >= 300:
385-
print_if_json(response.text)
386-
if response.status_code == 401:
387-
raise RuntimeError('Authentication failed, please check your DOMjudge credentials in ~/.netrc.')
388-
else:
389-
raise RuntimeError(f'Submission failed (code {response.status_code})')
390-
391-
# We got a successful HTTP response. It worked.
392-
# But check that we indeed received a submission ID.
393-
394-
try:
395-
submission = json.loads(response.text)
396-
except json.decoder.JSONDecodeError as e:
397-
error(f'Parsing DOMjudge\'s API output failed: {e}')
368+
endpoint = f"contests/{my_contest['id']}/submissions"
369+
submission = do_api_request(endpoint, data, files)
398370

399371
if (not isinstance(submission, dict)
400372
or 'id' not in submission

0 commit comments

Comments
 (0)