Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD: override fetch function to add User-Agent-header #70

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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