Skip to content

Commit ddfd465

Browse files
committed
MOD: Refine batch download API
1 parent 05a096b commit ddfd465

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

databento/historical/api/batch.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def download(
216216
- Creates the directories if any do not already exist
217217
- Partially downloaded files will be retried using a range request
218218
219-
Makes one or many `GET /batch.download` HTTP request(s).
219+
Makes one or many `GET /batch/download/{job_id}/{filename}` HTTP request(s).
220220
221221
Parameters
222222
----------
@@ -233,7 +233,7 @@ def download(
233233
("job_id", job_id),
234234
]
235235

236-
job_files: List[Dict[str, Union[str, int]]] = self._get(
236+
job_files: List[Dict[str, Any]] = self._get(
237237
url=self._base_url + ".list_files",
238238
params=params,
239239
basic_auth=True,
@@ -267,28 +267,34 @@ def download(
267267
filename = str(details["filename"])
268268
output_path = os.path.join(job_dir, filename)
269269
log_info(
270-
f"Downloading batch job file {filename} to {output_path} ...",
270+
f"Downloading batch job file to {output_path} ...",
271271
)
272272

273+
urls = details.get("urls")
274+
if urls:
275+
url = urls.get("https")
276+
if not url:
277+
raise ValueError(
278+
f"Cannot download {filename} over HTTPS "
279+
"('download' delivery is not available for this job).",
280+
)
281+
else:
282+
# Handle legacy manifest.json without the 'urls' field
283+
base_url = "https://api.databento.com/v0/batch/download/"
284+
url = f"{base_url}/{job_id}/{filename}"
285+
273286
self._download_file(
274-
job_id=job_id,
275-
filename=filename,
287+
url=url,
276288
filesize=int(details["size"]),
277289
output_path=output_path,
278290
)
279291

280292
def _download_file(
281293
self,
282-
job_id: str,
283-
filename: str,
294+
url: str,
284295
filesize: int,
285296
output_path: str,
286297
) -> None:
287-
params: List[Tuple[str, str]] = [
288-
("job_id", job_id),
289-
("filename", filename),
290-
]
291-
292298
headers: Dict[str, str] = self._headers.copy()
293299
mode = "wb"
294300

@@ -302,8 +308,7 @@ def _download_file(
302308
mode = "ab"
303309

304310
with requests.get(
305-
url=self._base_url + ".download",
306-
params=params,
311+
url=url,
307312
headers=headers,
308313
auth=HTTPBasicAuth(username=self._key, password=None),
309314
allow_redirects=True,

0 commit comments

Comments
 (0)