@@ -48,7 +48,8 @@ def parse_api_response(name: str, response: requests.Response) -> bytes:
4848 return response.content
4949
5050
51- def do_api_request(name: str, method: str = 'GET', jsonData: dict = {}, decode: bool = True):
51+ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {},
52+ data: dict = {}, files: dict = {}, decode: bool = True):
5253 '''Perform an API call to the given endpoint and return its data.
5354
5455 Based on whether `domjudge_api_url` is set, this will call the DOMjudge
@@ -58,6 +59,7 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {}, decode:
5859 name (str): the endpoint to call
5960 method (str): the method to use, GET or PUT are supported
6061 jsonData (dict): the JSON data to PUT. Only used when method is PUT
62+ data (dict): data to pass in a POST request
6163 decode (bool): whether to decode the returned JSON data, default true
6264
6365 Returns:
@@ -78,13 +80,23 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {}, decode:
7880 if parsed.username and parsed.password:
7981 auth = (parsed.username, parsed.password)
8082
83+ kwargs = {
84+ 'headers': headers,
85+ 'verify': ca_check,
86+ 'auth': auth,
87+ }
88+ if method == 'GET':
89+ pass
90+ elif method == 'PUT':
91+ kwargs['json'] = jsonData
92+ elif method == 'POST':
93+ kwargs['data'] = data
94+ kwargs['files'] = [(name, open(file, 'rb')) for (name, file) in files]
95+ else:
96+ raise RuntimeError(f"Method {method} not supported")
97+
8198 try:
82- if method == 'GET':
83- response = requests.get(url, headers=headers, verify=ca_check, auth=auth)
84- elif method == 'PUT':
85- response = requests.put(url, headers=headers, verify=ca_check, auth=auth, json=jsonData)
86- else:
87- raise RuntimeError("Method not supported")
99+ response = requests.request(method, url, **kwargs)
88100 except requests.exceptions.SSLError:
89101 ca_check = not confirm(
90102 "Can not verify certificate, ignore certificate check?", False)
@@ -107,46 +119,13 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {}, decode:
107119 return result
108120
109121
110- def upload_file(name: str, apifilename: str, file: str, data: dict = {}):
111- '''Upload the given file to the API at the given path with the given name.
112-
113- Based on whether `domjudge_api_url` is set, this will call the DOMjudge
114- API via HTTP or CLI.
115-
116- Parameters:
117- name (str): the endpoint to call
118- apifilename (str): the argument name for the file to upload
119- file (str): the file to upload
120-
121- Returns:
122- The parsed endpoint contents.
122+ def upload_file(endpoint: str, apifilename: str, file: str, data: dict = {}):
123+ '''Upload the given file to the API endpoint as apifilename.
123124
124- Raises:
125- RuntimeError when the HTTP status code is non 2xx.
125+ Thin wrapper around do_api_request, see that function for more details.
126126 '''
127127
128- if domjudge_api_url is None:
129- response = api_via_cli(name, 'POST', data, {apifilename: file})
130- else:
131- global ca_check
132- files = [(apifilename, open(file, 'rb'))]
133-
134- url = f'{domjudge_api_url}/{name}'
135-
136- try:
137- response = requests.post(
138- url, files=files, headers=headers, data=data, verify=ca_check)
139- except requests.exceptions.SSLError:
140- ca_check = not confirm(
141- "Can not verify certificate, ignore certificate check?", False)
142- if ca_check:
143- print('Can not verify certificate chain for DOMserver.')
144- exit(1)
145- else:
146- response = requests.post(
147- url, files=files, headers=headers, data=data, verify=ca_check)
148-
149- return parse_api_response(name, response)
128+ do_api_request(endpoint, 'POST', data=data, files={apifilename: file})
150129
151130
152131def api_via_cli(name: str, method: str = 'GET', data: dict = {}, files: dict = {}, jsonData: dict = {}):
0 commit comments