@@ -48,7 +48,8 @@ def parse_api_response(name: str, response: requests.Response) -> bytes:
48
48
return response.content
49
49
50
50
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):
52
53
'''Perform an API call to the given endpoint and return its data.
53
54
54
55
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:
58
59
name (str): the endpoint to call
59
60
method (str): the method to use, GET or PUT are supported
60
61
jsonData (dict): the JSON data to PUT. Only used when method is PUT
62
+ data (dict): data to pass in a POST request
61
63
decode (bool): whether to decode the returned JSON data, default true
62
64
63
65
Returns:
@@ -78,13 +80,23 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {}, decode:
78
80
if parsed.username and parsed.password:
79
81
auth = (parsed.username, parsed.password)
80
82
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
+
81
98
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)
88
100
except requests.exceptions.SSLError:
89
101
ca_check = not confirm(
90
102
"Can not verify certificate, ignore certificate check?", False)
@@ -107,46 +119,13 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {}, decode:
107
119
return result
108
120
109
121
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.
123
124
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.
126
126
'''
127
127
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})
150
129
151
130
152
131
def api_via_cli(name: str, method: str = 'GET', data: dict = {}, files: dict = {}, jsonData: dict = {}):
0 commit comments