From e8e8bbc02bc2c4d21eda07554fdb067834df48d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Fri, 15 Nov 2024 09:13:37 +0100 Subject: [PATCH 1/5] add 'User-Agent'-header to pooch.retrieve --- xarray/tutorial.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/xarray/tutorial.py b/xarray/tutorial.py index ccdb0d8e031..ad701ddc729 100644 --- a/xarray/tutorial.py +++ b/xarray/tutorial.py @@ -14,6 +14,7 @@ import numpy as np +from xarray import __version__ from xarray.backends.api import open_dataset as _open_dataset from xarray.core.dataarray import DataArray from xarray.core.dataset import Dataset @@ -157,8 +158,16 @@ def open_dataset( url = f"{base_url}/raw/{version}/{path.name}" + def _xarray_downloader(url, output_file, xrpooch): + """Create Downloader which adds request-headers""" + headers = {"User-Agent": f"xarray {__version__}"} + https = pooch.HTTPDownloader(headers=headers) + https(url, output_file, xrpooch) + # 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=_xarray_downloader + ) ds = _open_dataset(filepath, engine=engine, **kws) if not cache: ds = ds.load() From 61af01a81b2d5448ca3725d694f417ccf1ac4204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Fri, 15 Nov 2024 09:24:01 +0100 Subject: [PATCH 2/5] try sys.modules --- xarray/tutorial.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/tutorial.py b/xarray/tutorial.py index ad701ddc729..6e2fdd6c520 100644 --- a/xarray/tutorial.py +++ b/xarray/tutorial.py @@ -10,11 +10,11 @@ import os import pathlib +import sys from typing import TYPE_CHECKING import numpy as np -from xarray import __version__ from xarray.backends.api import open_dataset as _open_dataset from xarray.core.dataarray import DataArray from xarray.core.dataset import Dataset @@ -160,7 +160,7 @@ def open_dataset( def _xarray_downloader(url, output_file, xrpooch): """Create Downloader which adds request-headers""" - headers = {"User-Agent": f"xarray {__version__}"} + headers = {"User-Agent": f"xarray {sys.modules['xarray'].__version__}"} https = pooch.HTTPDownloader(headers=headers) https(url, output_file, xrpooch) From 41f310d9a6cd3aadfb482ac17aafc6280ab8beca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Fri, 15 Nov 2024 09:29:43 +0100 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Mathias Hauser --- xarray/tutorial.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/xarray/tutorial.py b/xarray/tutorial.py index 6e2fdd6c520..a416da46562 100644 --- a/xarray/tutorial.py +++ b/xarray/tutorial.py @@ -158,15 +158,13 @@ def open_dataset( url = f"{base_url}/raw/{version}/{path.name}" - def _xarray_downloader(url, output_file, xrpooch): - """Create Downloader which adds request-headers""" - headers = {"User-Agent": f"xarray {sys.modules['xarray'].__version__}"} - https = pooch.HTTPDownloader(headers=headers) - https(url, output_file, xrpooch) + + 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, downloader=_xarray_downloader + url=url, known_hash=None, path=cache_dir, downloader=downloader ) ds = _open_dataset(filepath, engine=engine, **kws) if not cache: From a7b6c57b306fdfd43f54d27bde55384f07c7f16b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 15 Nov 2024 08:29:55 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/tutorial.py | 1 - 1 file changed, 1 deletion(-) diff --git a/xarray/tutorial.py b/xarray/tutorial.py index a416da46562..9a5d52ed285 100644 --- a/xarray/tutorial.py +++ b/xarray/tutorial.py @@ -158,7 +158,6 @@ 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) From 24b4ff68743b299d1690305bd218476b7d86132f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Fri, 15 Nov 2024 09:51:33 +0100 Subject: [PATCH 5/5] add whats-new.rst entry --- doc/whats-new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index b74b0fb84de..ee826e6e56f 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -64,6 +64,8 @@ Bug fixes By `Pascal Bourgault `_. - Fix CF decoding of ``grid_mapping`` to allow all possible formats, add tests (:issue:`9761`, :pull:`9765`). By `Kai Mühlbauer `_. +- Add `User-Agent` to request-headers when retrieving tutorial data (:issue:`9774`, :pull:`9782`) + By `Kai Mühlbauer `_. Documentation ~~~~~~~~~~~~~