Guard http response in context manager

This commit is contained in:
I-Al-Istannen 2020-04-20 18:04:56 +02:00
parent 6407190ae0
commit 8181ae5b17

View File

@ -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}"
)