16
16
_myself = os.path.basename(sys.argv[0])
17
17
_default_user_agent = requests.utils.default_user_agent()
18
18
headers = {'user-agent': f'dj_utils/{_myself} ({_default_user_agent})'}
19
- domjudge_webapp_folder_or_api_url = 'unset'
19
+ domjudge_webapp_dir = '@domserver_webappdir@'
20
+ domjudge_api_url = None
20
21
ca_check = True
21
22
22
23
@@ -50,8 +51,8 @@ def parse_api_response(name: str, response: requests.Response) -> bytes:
50
51
def do_api_request(name: str, method: str = 'GET', jsonData: dict = {}, decode: bool = True):
51
52
'''Perform an API call to the given endpoint and return its data.
52
53
53
- Based on whether `domjudge_webapp_folder_or_api_url ` is a folder or URL this
54
- will use the DOMjudge CLI or HTTP API .
54
+ Based on whether `domjudge_api_url ` is set, this will call the DOMjudge
55
+ API via HTTP or CLI .
55
56
56
57
Parameters:
57
58
name (str): the endpoint to call
@@ -67,12 +68,12 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {}, decode:
67
68
cannot be JSON decoded.
68
69
'''
69
70
70
- if os . path . isdir ( domjudge_webapp_folder_or_api_url ) :
71
+ if domjudge_api_url is None :
71
72
result = api_via_cli(name, method, {}, {}, jsonData)
72
73
else:
73
74
global ca_check
74
- url = f'{ domjudge_webapp_folder_or_api_url } /{ name } '
75
- parsed = urlparse (domjudge_webapp_folder_or_api_url )
75
+ url = f'{domjudge_api_url }/{name}'
76
+ parsed = urlparse(domjudge_api_url )
76
77
auth = None
77
78
if parsed.username and parsed.password:
78
79
auth = (parsed.username, parsed.password)
@@ -109,8 +110,8 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {}, decode:
109
110
def upload_file(name: str, apifilename: str, file: str, data: dict = {}):
110
111
'''Upload the given file to the API at the given path with the given name.
111
112
112
- Based on whether `domjudge_webapp_folder_or_api_url ` is a folder or URL this
113
- will use the DOMjudge CLI or HTTP API .
113
+ Based on whether `domjudge_api_url ` is set, this will call the DOMjudge
114
+ API via HTTP or CLI .
114
115
115
116
Parameters:
116
117
name (str): the endpoint to call
@@ -124,13 +125,13 @@ def upload_file(name: str, apifilename: str, file: str, data: dict = {}):
124
125
RuntimeError when the HTTP status code is non 2xx.
125
126
'''
126
127
127
- if os . path . isdir ( domjudge_webapp_folder_or_api_url ) :
128
+ if domjudge_api_url is None :
128
129
response = api_via_cli(name, 'POST', data, {apifilename: file})
129
130
else:
130
131
global ca_check
131
132
files = [(apifilename, open(file, 'rb'))]
132
133
133
- url = f'{ domjudge_webapp_folder_or_api_url } /{ name } '
134
+ url = f'{domjudge_api_url }/{name}'
134
135
135
136
try:
136
137
response = requests.post(
@@ -166,7 +167,7 @@ def api_via_cli(name: str, method: str = 'GET', data: dict = {}, files: dict = {
166
167
'''
167
168
168
169
command = [
169
- f'{ domjudge_webapp_folder_or_api_url } /bin/console' ,
170
+ f'{domjudge_webapp_dir }/bin/console',
170
171
'api:call',
171
172
'-m',
172
173
method
0 commit comments