Skip to content

Commit

Permalink
Merge pull request #70 from kmuehlbauer/fix-fetch
Browse files Browse the repository at this point in the history
ADD: override fetch function to add `User-Agent`-header
  • Loading branch information
kmuehlbauer authored Nov 15, 2024
2 parents 94260c8 + be66be4 commit 794e4f7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
26 changes: 26 additions & 0 deletions open_radar_data/dataset.py
Original file line number Diff line number Diff line change
@@ -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/',
Expand All @@ -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
15 changes: 15 additions & 0 deletions open_radar_data/tests/test_dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pathlib

import pytest

from open_radar_data import DATASETS, locate


Expand All @@ -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

0 comments on commit 794e4f7

Please sign in to comment.