@@ -31,7 +31,7 @@ def confirm(message: str, default: bool) -> bool:
31
31
return answer == 'y'
32
32
33
33
34
- def parse_api_response(name : str, response: requests.Response) -> bytes:
34
+ def parse_api_response(endpoint : str, response: requests.Response) -> bytes:
35
35
# The connection worked, but we may have received an HTTP error
36
36
if response.status_code >= 300:
37
37
print(response.text)
@@ -40,23 +40,23 @@ def parse_api_response(name: str, response: requests.Response) -> bytes:
40
40
'Authentication failed, please check your DOMjudge credentials in ~/.netrc.')
41
41
else:
42
42
raise RuntimeError(
43
- f'API request {name } failed (code {response.status_code}).')
43
+ f'API request {endpoint } failed (code {response.status_code}).')
44
44
45
45
if response.status_code == 204:
46
46
return None
47
47
48
48
return response.content
49
49
50
50
51
- def do_api_request(name : str, method: str = 'GET', jsonData: dict = {},
51
+ def do_api_request(endpoint : str, method: str = 'GET', jsonData: dict = {},
52
52
data: dict = {}, files: dict = {}, decode: bool = True):
53
53
'''Perform an API call to the given endpoint and return its data.
54
54
55
55
Based on whether `domjudge_api_url` is set, this will call the DOMjudge
56
56
API via HTTP or CLI.
57
57
58
58
Parameters:
59
- name (str): the endpoint to call
59
+ endpoint (str): the endpoint to call
60
60
method (str): the method to use, GET or PUT are supported
61
61
jsonData (dict): the JSON data to PUT. Only used when method is PUT
62
62
data (dict): data to pass in a POST request
@@ -71,10 +71,10 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {},
71
71
'''
72
72
73
73
if domjudge_api_url is None:
74
- result = api_via_cli(name , method, {}, {}, jsonData)
74
+ result = api_via_cli(endpoint , method, {}, {}, jsonData)
75
75
else:
76
76
global ca_check
77
- url = f'{domjudge_api_url}/{name }'
77
+ url = f'{domjudge_api_url}/{endpoint }'
78
78
parsed = urlparse(domjudge_api_url)
79
79
auth = None
80
80
if parsed.username and parsed.password:
@@ -107,14 +107,14 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {},
107
107
return do_api_request(name)
108
108
except requests.exceptions.RequestException as e:
109
109
raise RuntimeError(e)
110
- result = parse_api_response(name , response)
110
+ result = parse_api_response(endpoint , response)
111
111
112
112
if decode:
113
113
try:
114
114
result = json.loads(result)
115
115
except json.decoder.JSONDecodeError as e:
116
116
print(result)
117
- raise RuntimeError(f'Failed to JSON decode the response for API request {name }')
117
+ raise RuntimeError(f'Failed to JSON decode the response for API request {endpoint }')
118
118
119
119
return result
120
120
@@ -128,11 +128,11 @@ def upload_file(endpoint: str, apifilename: str, file: str, data: dict = {}):
128
128
do_api_request(endpoint, 'POST', data=data, files={apifilename: file})
129
129
130
130
131
- def api_via_cli(name : str, method: str = 'GET', data: dict = {}, files: dict = {}, jsonData: dict = {}):
131
+ def api_via_cli(endpoint : str, method: str = 'GET', data: dict = {}, files: dict = {}, jsonData: dict = {}):
132
132
'''Perform the given API request using the CLI
133
133
134
134
Parameters:
135
- name (str): the endpoint to call
135
+ endpoint (str): the endpoint to call, relative to API base url
136
136
method (str): the method to use. Either GET, POST or PUT
137
137
data (dict): the POST data to use. Only used when method is POST or PUT
138
138
files (dict): the files to use. Only used when method is POST or PUT
@@ -161,7 +161,7 @@ def api_via_cli(name: str, method: str = 'GET', data: dict = {}, files: dict = {
161
161
if jsonData:
162
162
command.extend(['-j', json.dumps(jsonData)])
163
163
164
- command.append(name )
164
+ command.append(endpoint )
165
165
166
166
result = subprocess.run(command, capture_output=True)
167
167
@@ -172,6 +172,6 @@ def api_via_cli(name: str, method: str = 'GET', data: dict = {}, files: dict = {
172
172
result.stderr.decode('utf-8'),
173
173
file=sys.stderr
174
174
)
175
- raise RuntimeError(f'API request {name } failed')
175
+ raise RuntimeError(f'API request {endpoint } failed')
176
176
177
177
return result.stdout
0 commit comments