Skip to content

Commit d266b4d

Browse files
authored
Merge pull request #89 from openzim/dup
Added (missing) headers to stream_file
2 parents e494db4 + 12b30d8 commit d266b4d

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project are documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) (as of version 1.5.0).
77

8+
## [1.6.3] - 2022-08-02
9+
10+
### Added
11+
12+
- `download.stream_file()` supports passing `headers` (scrapers were already using it)
13+
814
## [1.6.2] - 2022-07-29
915

1016
### Changed

src/zimscraperlib/VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.6.2
1+
1.6.3

src/zimscraperlib/download.py

+2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ def stream_file(
165165
proxies: Optional[dict] = None,
166166
only_first_block: Optional[bool] = False,
167167
max_retries: Optional[int] = 5,
168+
headers: Optional[Dict[str, str]] = None,
168169
) -> Union[int, requests.structures.CaseInsensitiveDict]:
169170
"""Stream data from a URL to either a BytesIO object or a file
170171
Arguments -
@@ -188,6 +189,7 @@ def stream_file(
188189
url,
189190
stream=True,
190191
proxies=proxies,
192+
headers=headers,
191193
)
192194
resp.raise_for_status()
193195

tests/download/test_download.py

+12
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ def test_first_block_download(valid_http_url):
6565
assert len(byte_stream.read()) <= expected
6666

6767

68+
@pytest.mark.slow
69+
def test_user_agent():
70+
ua = "zimscraperlib-test"
71+
byte_stream = io.BytesIO()
72+
stream_file(
73+
url="http://useragentstring.com/",
74+
byte_stream=byte_stream,
75+
headers={"User-Agent": "zimscraperlib-test"},
76+
)
77+
assert ua in byte_stream.read().decode("utf-8")
78+
79+
6880
@pytest.mark.slow
6981
def test_save_http(tmp_path, valid_http_url):
7082
dest_file = tmp_path / "favicon.ico"

0 commit comments

Comments
 (0)