Skip to content

Commit

Permalink
add 'User-Agent'-header to pooch.retrieve (pydata#9782)
Browse files Browse the repository at this point in the history
* add 'User-Agent'-header to pooch.retrieve

* try sys.modules

* Apply suggestions from code review

Co-authored-by: Mathias Hauser <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add whats-new.rst entry

---------

Co-authored-by: Mathias Hauser <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 15, 2024
1 parent 4c8b03b commit 864b35a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Bug fixes
By `Pascal Bourgault <https://github.com/aulemahal>`_.
- Fix CF decoding of ``grid_mapping`` to allow all possible formats, add tests (:issue:`9761`, :pull:`9765`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
- Add `User-Agent` to request-headers when retrieving tutorial data (:issue:`9774`, :pull:`9782`)
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
8 changes: 7 additions & 1 deletion xarray/tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import os
import pathlib
import sys
from typing import TYPE_CHECKING

import numpy as np
Expand Down Expand Up @@ -157,8 +158,13 @@ def open_dataset(

url = f"{base_url}/raw/{version}/{path.name}"

headers = {"User-Agent": f"xarray {sys.modules['xarray'].__version__}"}
downloader = pooch.HTTPDownloader(headers=headers)

# retrieve the file
filepath = pooch.retrieve(url=url, known_hash=None, path=cache_dir)
filepath = pooch.retrieve(
url=url, known_hash=None, path=cache_dir, downloader=downloader
)
ds = _open_dataset(filepath, engine=engine, **kws)
if not cache:
ds = ds.load()
Expand Down

0 comments on commit 864b35a

Please sign in to comment.