@@ -70,6 +70,15 @@ def error(msg: str) -> NoReturn:
70
70
exit (- 1 )
71
71
72
72
73
+ def print_if_json (text : str ):
74
+ '''Print the text if it is valid JSON, and ignore otherwise'''
75
+ try :
76
+ data = json .loads (text )
77
+ print (json .dumps (data , indent = 2 ))
78
+ except json .decoder .JSONDecodeError :
79
+ pass
80
+
81
+
73
82
def read_contests () -> list :
74
83
'''Read all contests from the API.
75
84
@@ -202,15 +211,14 @@ def do_api_request(name: str):
202
211
except requests .exceptions .RequestException as e :
203
212
raise RuntimeError (e )
204
213
214
+ logging .debug (f"API call '{ name } ' returned:\n { response .text } " )
205
215
if response .status_code >= 300 :
206
- print (response .text )
216
+ print_if_json (response .text )
207
217
if response .status_code == 401 :
208
218
raise RuntimeError ('Authentication failed, please check your DOMjudge credentials in ~/.netrc.' )
209
219
else :
210
220
raise RuntimeError (f'API request { name } failed (code { response .status_code } ).' )
211
221
212
- logging .debug (f"API call '{ name } ' returned:\n { response .text } " )
213
-
214
222
return json .loads (response .text )
215
223
216
224
@@ -322,11 +330,10 @@ def do_api_print():
322
330
323
331
response = requests .post (url , data = data , headers = headers )
324
332
325
- logging .debug (f"API call 'printing' returned:\n { response .text } " )
326
-
327
333
# The connection worked, but we may have received an HTTP error
334
+ logging .debug (f"API printing call returned:\n { response .text } " )
328
335
if response .status_code >= 300 :
329
- print (response .text )
336
+ print_if_json (response .text )
330
337
if response .status_code == 401 :
331
338
raise RuntimeError ('Authentication failed, please check your DOMjudge credentials in ~/.netrc.' )
332
339
else :
@@ -367,11 +374,10 @@ def do_api_submit():
367
374
368
375
response = requests .post (url , data = data , files = files , headers = headers )
369
376
370
- logging .debug (f"API call 'submissions' returned:\n { response .text } " )
371
-
372
377
# The connection worked, but we may have received an HTTP error
378
+ logging .debug (f"API submitting call returned:\n { response .text } " )
373
379
if response .status_code >= 300 :
374
- print (response .text )
380
+ print_if_json (response .text )
375
381
if response .status_code == 401 :
376
382
raise RuntimeError ('Authentication failed, please check your DOMjudge credentials in ~/.netrc.' )
377
383
else :
0 commit comments