Skip to content

Commit 8e13882

Browse files
committed
feat: add getArchivefileUrls and getArchivefileUrl to return the download link only
1 parent ca0ccc2 commit 8e13882

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

src/onc/modules/_OncArchive.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ def getArchivefile(self, filters: dict, allPages: bool):
4141
allPages=allPages,
4242
)
4343

44+
def getArchivefileUrls(self, filters: dict, allPages: bool) -> list[str]:
45+
file_list: list[str] = self.getArchivefile(filters, allPages)["files"]
46+
return list(map(self.getArchivefileUrl, file_list))
47+
48+
def getArchivefileUrl(self, filename: str) -> str:
49+
"""
50+
Return an archivefile absolute download URL for a filename
51+
"""
52+
url = self._serviceUrl("archivefile")
53+
token = self._config("token")
54+
return f"{url}/download?filename={filename}&token={token}"
55+
4456
def downloadArchivefile(self, filename: str = "", overwrite: bool = False):
4557
url = self._serviceUrl("archivefiles")
4658

@@ -123,7 +135,7 @@ def downloadDirectArchivefile(
123135
else:
124136
print(f' Skipping "{filename}": File already exists.')
125137
downInfo = {
126-
"url": self._getDownloadUrl(filename),
138+
"url": self.getArchivefileUrl(filename),
127139
"status": "skipped",
128140
"size": 0,
129141
"downloadTime": 0,
@@ -139,15 +151,6 @@ def downloadDirectArchivefile(
139151
"stats": {"totalSize": size, "downloadTime": time, "fileCount": successes},
140152
}
141153

142-
def _getDownloadUrl(self, filename: str):
143-
"""
144-
Returns an archivefile absolute download URL for a filename
145-
"""
146-
url = self._serviceUrl("archivefiles")
147-
return "{:s}?method=getFile&filename={:s}&token={:s}".format(
148-
url, filename, self._config("token")
149-
)
150-
151154
def _getList(self, filters: dict, by: str = "location", allPages: bool = False):
152155
"""
153156
Wraps archivefiles getArchivefileByLocation and getArchivefileByDevice methods

src/onc/modules/_OncService.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def _serviceUrl(self, service: str):
7777
"properties",
7878
"dataProducts",
7979
"archivefiles",
80+
"archivefile",
8081
"scalardata",
8182
"rawdata",
8283
]:

src/onc/onc.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,59 @@ def getArchivefile(self, filters: dict = None, allPages: bool = False):
12991299
""" # noqa: E501
13001300
return self.archive.getArchivefile(filters, allPages)
13011301

1302+
def getArchivefileUrls(
1303+
self,
1304+
filters: dict = None,
1305+
allPages: bool = False,
1306+
joinedWithNewline: bool = False,
1307+
) -> list[str] | str:
1308+
"""
1309+
Return a list of file URLs (or joined with a newline) available in Oceans 3.0 Archiving System by given query parameters.
1310+
1311+
A helper method for getting a list of archive files URLs without downloading them.
1312+
It can also return a single string that concatenates all the URLs in the list together with a newline,
1313+
which can be useful if you are using a download manager and it supports batch downloading multiple URLs
1314+
that expects all the URLs on a separate line from either a file or the clipboard.
1315+
1316+
Parameters
1317+
----------
1318+
filters : dict, optional
1319+
Query string parameters in the API request.
1320+
See ``getArchivefileByLocation`` and ``getArchivefileByDevice`` for more information.
1321+
allPages : bool, default False
1322+
Whether the response concatenates data on all pages if there are more than one page due to rowLimit.
1323+
joinedWithNewline: bool, default False
1324+
Whether it returns a list of URLs or a single string that concatenates the list with a newline.
1325+
1326+
Returns
1327+
-------
1328+
list[str] | str
1329+
A list of file URLs or a single joined string.
1330+
""" # noqa: E501
1331+
file_urls = self.archive.getArchivefileUrls(filters, allPages)
1332+
if joinedWithNewline:
1333+
return "\n".join(file_urls)
1334+
else:
1335+
return file_urls
1336+
1337+
def getArchivefileUrl(self, filename: str = "") -> str:
1338+
"""
1339+
Return a file URL from Oceans 3.0 Archiving System by specifying the file name.
1340+
1341+
A helper method for obtaining the archive file URL without actually downloading the file.
1342+
1343+
Parameters
1344+
----------
1345+
filename : str, default ""
1346+
A valid name of a file in DMAS Archiving System.
1347+
1348+
Returns
1349+
-------
1350+
str:
1351+
A download URL for the given archive filename.
1352+
""" # noqa: E501
1353+
return self.archive.getArchivefileUrl(filename)
1354+
13021355
def downloadArchivefile(self, filename: str = "", overwrite: bool = False):
13031356
"""
13041357
Download a file from Oceans 3.0 Archiving System by specifying the file name.

0 commit comments

Comments
 (0)