|
2 | 2 |
|
3 | 3 | import argparse |
4 | 4 | from datetime import datetime |
5 | | -from getpass import getpass |
6 | 5 | import json |
7 | 6 | from time import sleep |
8 | 7 | import requests |
9 | | -from urllib.parse import quote |
10 | 8 |
|
11 | | -def login(base_url: str, authenticator: str, username: str, password: str) -> str: |
12 | | - """ |
13 | | - Args: |
14 | | - base_url (str): URL for DataGateway without path. |
15 | | - authenticator (str): Authentication mechanism to use. |
16 | | - username (str): Username to use. |
17 | | - password (str): Password to use. |
18 | | -
|
19 | | - Raises: |
20 | | - RuntimeError: If a status code other than 200 is returned. |
21 | | -
|
22 | | - Returns: |
23 | | - str: ICAT session id. |
24 | | - """ |
25 | | - url = f"{base_url}/topcat/user/session" |
26 | | - encoded_password = quote(json.dumps(password)[1:-1]) |
27 | | - data = {"plugin": authenticator, "username": username, "password": encoded_password} |
28 | | - response = requests.post(url=url, data=data) |
29 | | - if response.status_code != 200: |
30 | | - raise RuntimeError(response.text) |
31 | | - |
32 | | - return json.loads(response.content)["sessionId"] |
| 9 | +from common import VERIFY, add_common_args, get_password, login |
33 | 10 |
|
34 | 11 |
|
35 | 12 | def queue_all_files( |
@@ -119,14 +96,15 @@ def queue_files( |
119 | 96 | Returns: |
120 | 97 | int: The Download id. |
121 | 98 | """ |
| 99 | + url = f"{base_url}/topcat/user/queue/files" |
122 | 100 | data = { |
123 | 101 | "sessionId": session_id, |
124 | 102 | "transport": transport, |
125 | 103 | "fileName": file_name, |
126 | 104 | "email": email, |
127 | 105 | "files": files, |
128 | 106 | } |
129 | | - response = requests.post(url=base_url + "/topcat/user/queue/files", data=data) |
| 107 | + response = requests.post(url=url, data=data, verify=VERIFY) |
130 | 108 | if response.status_code != 200: |
131 | 109 | raise RuntimeError(response.text) |
132 | 110 |
|
@@ -160,20 +138,21 @@ def monitor( |
160 | 138 | """ |
161 | 139 | url = base_url + "/topcat/user/downloads/status" |
162 | 140 | params = {"sessionId": session_id, "downloadIds": downloads} |
163 | | - response = requests.get(url=url, params=params) |
| 141 | + response = requests.get(url=url, params=params, verify=VERIFY) |
164 | 142 | if response.status_code != 200: |
165 | 143 | raise RuntimeError(response.text) |
166 | 144 | content = json.loads(response.content) |
167 | 145 | print(content) |
168 | 146 |
|
169 | 147 | while any([s in {"QUEUED", "PAUSED", "PREPARING", "RESTORING"} for s in content]): |
| 148 | + sessions_url = f"{base_url}/datagateway-api/sessions" |
170 | 149 | headers = {"Authorization": f"Bearer {session_id}"} |
171 | | - requests.put(url=base_url + "/datagateway-api/sessions", headers=headers) |
| 150 | + requests.put(url=sessions_url, headers=headers, verify=VERIFY) |
172 | 151 | if response.status_code != 200: |
173 | 152 | raise RuntimeError(response.text) |
174 | 153 |
|
175 | 154 | sleep(monitor_sleep * 60) |
176 | | - response = requests.get(url=url, params=params) |
| 155 | + response = requests.get(url=url, params=params, verify=VERIFY) |
177 | 156 | if response.status_code != 200: |
178 | 157 | raise RuntimeError(response.text) |
179 | 158 |
|
@@ -205,35 +184,7 @@ def monitor( |
205 | 184 | "displayed in the DataGateway UI." |
206 | 185 | ), |
207 | 186 | ) |
208 | | - parser.add_argument( |
209 | | - "--url", |
210 | | - type=str, |
211 | | - default="https://datagateway.diamond.ac.uk", |
212 | | - help="The url address of the DataGateway instance to submit requests to.", |
213 | | - ) |
214 | | - parser.add_argument( |
215 | | - "-a", |
216 | | - "--authenticator", |
217 | | - type=str, |
218 | | - default="ldap", |
219 | | - help="The authentication mechanism to use for DataGateway login.", |
220 | | - ) |
221 | | - parser.add_argument( |
222 | | - "-u", |
223 | | - "--username", |
224 | | - type=str, |
225 | | - required=True, |
226 | | - help="The username used for DataGateway login.", |
227 | | - ) |
228 | | - parser.add_argument( |
229 | | - "-p", |
230 | | - "--password-file", |
231 | | - type=str, |
232 | | - help=( |
233 | | - "Location of file containing password for DataGateway login. If not " |
234 | | - "provided, the password will need to be provided by prompt." |
235 | | - ), |
236 | | - ) |
| 187 | + add_common_args(parser) |
237 | 188 | parser.add_argument( |
238 | 189 | "--download-name", |
239 | 190 | type=str, |
@@ -276,12 +227,7 @@ def monitor( |
276 | 227 | ) |
277 | 228 | args = parser.parse_args() |
278 | 229 |
|
279 | | - if args.password_file is None: |
280 | | - password = getpass() |
281 | | - else: |
282 | | - with open(args.password_file) as f: |
283 | | - password = f.readline().strip() |
284 | | - |
| 230 | + password = get_password() |
285 | 231 | session_id = login( |
286 | 232 | base_url=args.url, |
287 | 233 | authenticator=args.authenticator, |
|
0 commit comments