@@ -44,17 +44,10 @@ def parse_api_response(name: str, response: requests.Response):
44
44
if response .status_code == 204 :
45
45
return None
46
46
47
- # We got a successful HTTP response. It worked. Return the full response
48
- try :
49
- result = json .loads (response .text )
50
- except json .decoder .JSONDecodeError as e :
51
- print (response .text )
52
- raise RuntimeError (f'Failed to JSON decode the response for API request { name } ' )
53
-
54
- return result
47
+ return response .text
55
48
56
49
57
- def do_api_request (name : str , method : str = 'GET' , jsonData : dict = {}):
50
+ def do_api_request (name : str , method : str = 'GET' , jsonData : dict = {}, decode : bool = True ):
58
51
'''Perform an API call to the given endpoint and return its data.
59
52
60
53
Based on whether `domjudge_webapp_folder_or_api_url` is a folder or URL this
@@ -64,16 +57,18 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {}):
64
57
name (str): the endpoint to call
65
58
method (str): the method to use, GET or PUT are supported
66
59
jsonData (dict): the JSON data to PUT. Only used when method is PUT
60
+ decode (bool): whether to decode the returned JSON data, default true
67
61
68
62
Returns:
69
63
The endpoint contents.
70
64
71
65
Raises:
72
- RuntimeError when the response is not JSON or the HTTP status code is non 2xx.
66
+ RuntimeError when the HTTP status code is non-2xx or the response
67
+ cannot be JSON decoded.
73
68
'''
74
69
75
70
if os .path .isdir (domjudge_webapp_folder_or_api_url ):
76
- return api_via_cli (name , method , {}, {}, jsonData )
71
+ result = api_via_cli (name , method , {}, {}, jsonData )
77
72
else :
78
73
global ca_check
79
74
url = f'{ domjudge_webapp_folder_or_api_url } /{ name } '
@@ -97,7 +92,17 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {}):
97
92
return do_api_request (name )
98
93
except requests .exceptions .RequestException as e :
99
94
raise RuntimeError (e )
100
- return parse_api_response (name , response )
95
+ result = parse_api_response (name , response )
96
+
97
+ if decode :
98
+ try :
99
+ result = json .loads (result )
100
+ except json .decoder .JSONDecodeError as e :
101
+ print (result )
102
+ raise RuntimeError (f'Failed to JSON decode the response for API request { name } ' )
103
+
104
+ return result
105
+
101
106
102
107
def upload_file (name : str , apifilename : str , file : str , data : dict = {}):
103
108
'''Upload the given file to the API at the given path with the given name.
@@ -118,7 +123,7 @@ def upload_file(name: str, apifilename: str, file: str, data: dict = {}):
118
123
'''
119
124
120
125
if os .path .isdir (domjudge_webapp_folder_or_api_url ):
121
- return api_via_cli (name , 'POST' , data , {apifilename : file })
126
+ response = api_via_cli (name , 'POST' , data , {apifilename : file })
122
127
else :
123
128
global ca_check
124
129
files = [(apifilename , open (file , 'rb' ))]
@@ -152,7 +157,7 @@ def api_via_cli(name: str, method: str = 'GET', data: dict = {}, files: dict = {
152
157
jsonData (dict): the JSON data to use. Only used when method is POST or PUT
153
158
154
159
Returns:
155
- The parsed endpoint contents.
160
+ The endpoint contents.
156
161
157
162
Raises:
158
163
RuntimeError when the command exit code is not 0.
@@ -183,4 +188,4 @@ def api_via_cli(name: str, method: str = 'GET', data: dict = {}, files: dict = {
183
188
print (response )
184
189
raise RuntimeError (f'API request { name } failed' )
185
190
186
- return json . loads ( response )
191
+ return response
0 commit comments