Skip to content

Commit 56b9169

Browse files
committed
Rename parameter 'name' to 'endpoint' for clarity
1 parent c9bb97b commit 56b9169

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

misc-tools/dj_utils.py.in

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def confirm(message: str, default: bool) -> bool:
3131
return answer == 'y'
3232

3333

34-
def parse_api_response(name: str, response: requests.Response) -> bytes:
34+
def parse_api_response(endpoint: str, response: requests.Response) -> bytes:
3535
# The connection worked, but we may have received an HTTP error
3636
if response.status_code >= 300:
3737
print(response.text)
@@ -40,23 +40,23 @@ def parse_api_response(name: str, response: requests.Response) -> bytes:
4040
'Authentication failed, please check your DOMjudge credentials in ~/.netrc.')
4141
else:
4242
raise RuntimeError(
43-
f'API request {name} failed (code {response.status_code}).')
43+
f'API request {endpoint} failed (code {response.status_code}).')
4444

4545
if response.status_code == 204:
4646
return None
4747

4848
return response.content
4949

5050

51-
def do_api_request(name: str, method: str = 'GET', jsonData: dict = {},
51+
def do_api_request(endpoint: str, method: str = 'GET', jsonData: dict = {},
5252
data: dict = {}, files: dict = {}, decode: bool = True):
5353
'''Perform an API call to the given endpoint and return its data.
5454

5555
Based on whether `domjudge_api_url` is set, this will call the DOMjudge
5656
API via HTTP or CLI.
5757

5858
Parameters:
59-
name (str): the endpoint to call
59+
endpoint (str): the endpoint to call
6060
method (str): the method to use, GET or PUT are supported
6161
jsonData (dict): the JSON data to PUT. Only used when method is PUT
6262
data (dict): data to pass in a POST request
@@ -71,10 +71,10 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {},
7171
'''
7272

7373
if domjudge_api_url is None:
74-
result = api_via_cli(name, method, {}, {}, jsonData)
74+
result = api_via_cli(endpoint, method, {}, {}, jsonData)
7575
else:
7676
global ca_check
77-
url = f'{domjudge_api_url}/{name}'
77+
url = f'{domjudge_api_url}/{endpoint}'
7878
parsed = urlparse(domjudge_api_url)
7979
auth = None
8080
if parsed.username and parsed.password:
@@ -107,14 +107,14 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {},
107107
return do_api_request(name)
108108
except requests.exceptions.RequestException as e:
109109
raise RuntimeError(e)
110-
result = parse_api_response(name, response)
110+
result = parse_api_response(endpoint, response)
111111

112112
if decode:
113113
try:
114114
result = json.loads(result)
115115
except json.decoder.JSONDecodeError as e:
116116
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}')
118118

119119
return result
120120

@@ -128,11 +128,11 @@ def upload_file(endpoint: str, apifilename: str, file: str, data: dict = {}):
128128
do_api_request(endpoint, 'POST', data=data, files={apifilename: file})
129129

130130

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 = {}):
132132
'''Perform the given API request using the CLI
133133

134134
Parameters:
135-
name (str): the endpoint to call
135+
endpoint (str): the endpoint to call, relative to API base url
136136
method (str): the method to use. Either GET, POST or PUT
137137
data (dict): the POST data to use. Only used when method is POST or PUT
138138
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 = {
161161
if jsonData:
162162
command.extend(['-j', json.dumps(jsonData)])
163163

164-
command.append(name)
164+
command.append(endpoint)
165165

166166
result = subprocess.run(command, capture_output=True)
167167

@@ -172,6 +172,6 @@ def api_via_cli(name: str, method: str = 'GET', data: dict = {}, files: dict = {
172172
result.stderr.decode('utf-8'),
173173
file=sys.stderr
174174
)
175-
raise RuntimeError(f'API request {name} failed')
175+
raise RuntimeError(f'API request {endpoint} failed')
176176

177177
return result.stdout

0 commit comments

Comments
 (0)