From 8181ae5b17dec951bf17f271ce34f223ab8e97b3 Mon Sep 17 00:00:00 2001 From: I-Al-Istannen Date: Mon, 20 Apr 2020 18:04:56 +0200 Subject: [PATCH] Guard http response in context manager --- PFERD/http_downloader.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/PFERD/http_downloader.py b/PFERD/http_downloader.py index 5feac8c..3c5d531 100644 --- a/PFERD/http_downloader.py +++ b/PFERD/http_downloader.py @@ -33,13 +33,12 @@ class HttpDownloader(): def download(self, url: str, target_path: Path, parameters: Dict[str, Any] = {}) -> None: """Download a given url to a given path, optionally with some get parameters.""" - response = self._session.get(url, params=parameters) - - if response.status_code == 200: - tmp_file = self._tmp_dir.new_file() - stream_to_path(response, tmp_file) - self._organizer.accept_file(tmp_file, target_path) - else: - raise Exception( - f"Could not download file, got response {response.status_code}" - ) + with self._session.get(url, params=parameters) as response: + if response.status_code == 200: + tmp_file = self._tmp_dir.new_file() + stream_to_path(response, tmp_file) + self._organizer.accept_file(tmp_file, target_path) + else: + raise Exception( + f"Could not download file, got response {response.status_code}" + )