Skip to content

Commit

Permalink
Change: Allow to set request parameters and headers for download
Browse files Browse the repository at this point in the history
Extend the download context manager function to allow setting the http
request parameters and headers. This will be required for downloading a
file from a private GitHub URL.
  • Loading branch information
bjoernricks committed Sep 20, 2022
1 parent cc5cf9d commit 65e74c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pontos/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import subprocess
from contextlib import contextmanager
from pathlib import Path
from typing import Generator, Iterable, Iterator, Optional, Union
from typing import Any, Dict, Generator, Iterable, Iterator, Optional, Union

import httpx

Expand Down Expand Up @@ -88,6 +88,8 @@ def download(
url: str,
destination: Optional[Union[Path, str]] = None,
*,
headers: Dict[str, Any] = None,
params: Dict[str, Any] = None,
chunk_size: int = DEFAULT_CHUNK_SIZE,
timeout: int = DEFAULT_TIMEOUT,
) -> Generator[DownloadProgressIterable, None, None]:
Expand All @@ -97,6 +99,8 @@ def download(
url: The url of the file we want to download
destination: Path of the file to store the download in. If set it will
be derived from the passed URL.
headers: HTTP headers to use for the download
params: HTTP request parameters to use for the download
chunk_size: Download file in chunks of this size
timeout: Connection timeout
Expand All @@ -118,7 +122,12 @@ def download(
)

with httpx.stream(
"GET", url, timeout=timeout, follow_redirects=True
"GET",
url,
timeout=timeout,
follow_redirects=True,
headers=headers,
params=params,
) as response:
response.raise_for_status()

Expand Down
6 changes: 6 additions & 0 deletions tests/github/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ def test_download_release_tarball(
requests_mock.assert_called_once_with(
"GET",
"https://github.com/greenbone/pontos/archive/refs/tags/v21.11.0.tar.gz", # pylint: disable=line-too-long
headers=None,
params=None,
follow_redirects=True,
timeout=DEFAULT_TIMEOUT,
)
Expand Down Expand Up @@ -310,6 +312,8 @@ def test_download_release_tarball_with_content_length(
requests_mock.assert_called_once_with(
"GET",
"https://github.com/greenbone/pontos/archive/refs/tags/v21.11.0.tar.gz", # pylint: disable=line-too-long
params=None,
headers=None,
follow_redirects=True,
timeout=DEFAULT_TIMEOUT,
)
Expand Down Expand Up @@ -351,6 +355,8 @@ def test_download_release_zip(
"GET",
"https://github.com/greenbone/pontos/archive/refs/tags/v21.11.0.zip", # pylint: disable=line-too-long
follow_redirects=True,
headers=None,
params=None,
timeout=DEFAULT_TIMEOUT,
)
response_headers.get.assert_called_once_with("content-length")
Expand Down

0 comments on commit 65e74c8

Please sign in to comment.