From 65b81ea9383e5360f0da4112460c71caa32f15fa Mon Sep 17 00:00:00 2001 From: Silvan Laube Date: Thu, 31 Oct 2024 14:21:09 +0100 Subject: [PATCH 1/2] fix error on downloading URL with 302 redirect --- wn/_download.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wn/_download.py b/wn/_download.py index acc6787..3c7d106 100644 --- a/wn/_download.py +++ b/wn/_download.py @@ -98,7 +98,7 @@ def _download(urls: Sequence[str], progress: ProgressHandler) -> Path: try: with open(path, 'wb') as f: progress.set(status='Requesting', count=0) - with httpx.stream("GET", url, timeout=TIMEOUT) as response: + with httpx.stream("GET", url, timeout=TIMEOUT, follow_redirects=True) as response: response.raise_for_status() total = int(response.headers.get('Content-Length', 0)) count = response.num_bytes_downloaded From c6de241c9591b584a0c151aed83fb480b751e760 Mon Sep 17 00:00:00 2001 From: Michael Wayne Goodman Date: Fri, 1 Nov 2024 16:34:59 -0700 Subject: [PATCH 2/2] Use httpx.Client in wn._download --- wn/_download.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/wn/_download.py b/wn/_download.py index 3c7d106..5942265 100644 --- a/wn/_download.py +++ b/wn/_download.py @@ -90,6 +90,7 @@ def _get_cache_path_and_urls(project_or_url: str) -> tuple[Optional[Path], list[ def _download(urls: Sequence[str], progress: ProgressHandler) -> Path: + client = httpx.Client(timeout=TIMEOUT, follow_redirects=True) try: for i, url in enumerate(urls, 1): path = config.get_cache_path(url) @@ -98,7 +99,7 @@ def _download(urls: Sequence[str], progress: ProgressHandler) -> Path: try: with open(path, 'wb') as f: progress.set(status='Requesting', count=0) - with httpx.stream("GET", url, timeout=TIMEOUT, follow_redirects=True) as response: + with client.stream("GET", url) as response: response.raise_for_status() total = int(response.headers.get('Content-Length', 0)) count = response.num_bytes_downloaded @@ -128,5 +129,7 @@ def _download(urls: Sequence[str], progress: ProgressHandler) -> Path: except Exception: path.unlink(missing_ok=True) raise + finally: + client.close() return path