From 4675c79aebb9bcc2014cb26844ccca32172da144 Mon Sep 17 00:00:00 2001 From: Germano Guerrini Date: Fri, 2 Aug 2024 13:05:28 +0200 Subject: [PATCH] Separated download url computation --- hda/api.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/hda/api.py b/hda/api.py index 4835f4a..973c651 100644 --- a/hda/api.py +++ b/hda/api.py @@ -269,19 +269,38 @@ def __getitem__(self, index): def _download(self, result, download_dir: str = "."): logger.debug(result) self.client.accept_tac(self.dataset) + download_id = self._get_download_id(result) + self.stream( + download_id, + result["properties"]["size"], + download_dir, + ) + def _get_download_id(self, result): query = { "dataset_id": self.dataset, "product_id": result["id"], "location": result["properties"]["location"], } - download_id = DataOrderRequest(self.client).run(query) + return DataOrderRequest(self.client).run(query) - self.stream( - download_id, - result["properties"]["size"], - download_dir, - ) + def get_download_urls(self, limit: int = None): + """Utility function to return the list of final download URLs. + Useful in the context of the Serverless Functions service. + If the list of results is long, it might take a long time. + In that case, either subset the results or set a value for `limit`. + """ + + def build_url(result): + download_id = self._get_download_id(result) + return self.client.full_url(*[f"dataaccess/download/{download_id}"]) + + if limit is not None: + results = self.results[:limit] + else: + results = self.results + + return [build_url(r) for r in results] def download(self, download_dir: str = "."): """Downloads the results into the given download directory.