From be66be4fe3d67a4e642c84e3bd114766879a9347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Fri, 15 Nov 2024 08:48:39 +0100 Subject: [PATCH] ADD: override fetch function to add `User-Agent`-header --- open_radar_data/dataset.py | 26 ++++++++++++++++++++++++++ open_radar_data/tests/test_dataset.py | 15 +++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/open_radar_data/dataset.py b/open_radar_data/dataset.py index c016ee7..5073bfc 100644 --- a/open_radar_data/dataset.py +++ b/open_radar_data/dataset.py @@ -1,6 +1,10 @@ import importlib.resources +from functools import wraps + import pooch +import open_radar_data + DATASETS = pooch.create( path=pooch.os_cache('open-radar-data'), base_url='https://github.com/openradar/open-radar-data/raw/main/data/', @@ -25,3 +29,25 @@ def locate(): The local data storage location. """ return str(DATASETS.abspath) + + +def open_radar_downloader(url, output_file, mypooch): + """Create Downloader which adds request-headers""" + headers = {'User-Agent': f'open-radar-data {open_radar_data.__version__}'} + https = pooch.HTTPDownloader(headers=headers) + https(url, output_file, mypooch) + + +# preserve current fetch +DATASETS._fetch = DATASETS.fetch + + +# wrap new fetch +@wraps(DATASETS._fetch) +def fetch(*args, **kwargs): + kwargs.setdefault('downloader', open_radar_downloader) + return DATASETS._fetch(*args, **kwargs) + + +# override original fetch with overridden fetch +DATASETS.fetch = fetch diff --git a/open_radar_data/tests/test_dataset.py b/open_radar_data/tests/test_dataset.py index 5292764..80fa887 100644 --- a/open_radar_data/tests/test_dataset.py +++ b/open_radar_data/tests/test_dataset.py @@ -1,5 +1,7 @@ import pathlib +import pytest + from open_radar_data import DATASETS, locate @@ -12,3 +14,16 @@ def test_locate(): p = locate() assert 'open-radar-data' in p assert pathlib.Path(p) + + +def test_fetch(): + fi = '2013051000000600dBZ.vol' + fo = DATASETS.fetch(fi) + assert fi == pathlib.Path(fo).name + + +@pytest.mark.xfail(strict=False, reason='possible 403') +def test_original_fetch(): + fi = 'T_PAGZ35_C_ENMI_20170421090837.hdf' + fo = DATASETS._fetch(fi) + assert fi == pathlib.Path(fo).name