Skip to content

Commit a3765cc

Browse files
committed
refactor file retrieval methods to streamline zip handling and improve readability
1 parent adb79a2 commit a3765cc

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

hsclient/hydroshare.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,19 +1359,38 @@ def retrieve_string(self, path):
13591359

13601360
def retrieve_file(self, path, save_path=""):
13611361
file = self.get(path, status_code=200, allow_redirects=True)
1362-
filename = path.split("/")[-1]
1363-
downloaded_file = os.path.join(save_path, filename)
1364-
with open(downloaded_file, 'wb') as f:
1365-
f.write(file.content)
1366-
return downloaded_file
1362+
self.write_file(path, file.content, save_path)
13671363

13681364
def retrieve_bag(self, path, save_path=""):
1369-
file = self.get(path, status_code=200, allow_redirects=True)
1370-
1371-
if file.headers['Content-Type'] != "application/zip":
1365+
print(f"Retrieving {path}")
1366+
response = self.get(path, status_code=200, allow_redirects=True)
1367+
1368+
file_is_ready = False
1369+
content_type = response.headers['Content-Type']
1370+
1371+
if content_type == "application/zip":
1372+
# if the path doesn't end with .zip, add it
1373+
if not path.endswith(".zip"):
1374+
path += ".zip"
1375+
file_is_ready = True
1376+
if content_type == "binary/octet-stream":
1377+
# here we assume that the stream is a zip file
1378+
if not path.endswith(".zip"):
1379+
path += ".zip"
1380+
file_is_ready = True
1381+
1382+
if not file_is_ready:
13721383
time.sleep(CHECK_TASK_PING_INTERVAL)
13731384
return self.retrieve_bag(path, save_path)
1374-
return self.retrieve_file(path, save_path)
1385+
1386+
self.write_file(path, response.content, save_path)
1387+
1388+
def write_file(self, path, content, save_path=""):
1389+
filename = path.split("/")[-1]
1390+
downloaded_file = os.path.join(save_path, filename)
1391+
with open(downloaded_file, 'wb') as f:
1392+
f.write(content)
1393+
return downloaded_file
13751394

13761395
def check_task(self, task_id):
13771396
response = self.get(f"/hsapi/taskstatus/{task_id}/", status_code=200)

0 commit comments

Comments
 (0)