@@ -186,14 +186,16 @@ def read_problems() -> list:
186
186
return problems
187
187
188
188
189
- def do_api_request (name : str ):
189
+ def do_api_request (endpoint : str , data = None , files = None ):
190
190
'''Perform an API call to the given endpoint and return its data.
191
191
192
192
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
194
196
195
197
Returns:
196
- The endpoint contents.
198
+ The decoded endpoint contents.
197
199
198
200
Raises:
199
201
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):
202
204
if not baseurl :
203
205
raise RuntimeError ('No baseurl set' )
204
206
205
- url = f'{ baseurl } api/{ api_version } { name } '
207
+ url = f'{ baseurl } api/{ api_version } { endpoint } '
206
208
207
209
logging .info (f'Connecting to { url } ' )
208
210
209
211
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 )
211
216
except requests .exceptions .RequestException as e :
212
217
raise RuntimeError (e )
213
218
214
- logging .debug (f"API call '{ name } ' returned:\n { response .text } " )
219
+ logging .debug (f"API call '{ endpoint } ' returned:\n { response .text } " )
215
220
if response .status_code >= 300 :
216
221
print_if_json (response .text )
217
222
if response .status_code == 401 :
218
223
raise RuntimeError ('Authentication failed, please check your DOMjudge credentials in ~/.netrc.' )
219
224
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 } ' )
221
231
222
- return json . loads ( response . text )
232
+ return decoded_response
223
233
224
234
225
235
def kotlin_base_entry_point (filebase : str ) -> str :
@@ -330,27 +340,8 @@ def do_api_print():
330
340
if entry_point :
331
341
data ['entry_point' ] = entry_point
332
342
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 )
354
345
355
346
if not isinstance (result , dict ) or 'success' not in result :
356
347
error ('DOMjudge\' s API returned unexpected JSON data.' )
@@ -374,27 +365,8 @@ def do_api_submit():
374
365
375
366
files = [('code[]' , open (filename , 'rb' )) for filename in filenames ]
376
367
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 )
398
370
399
371
if (not isinstance (submission , dict )
400
372
or 'id' not in submission
0 commit comments