Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ All notable changes to this project will be documented in this file.
### Changed
- File downloads now identify as `databus-python-client/<version> (https://github.com/dbpedia/databus-python-client)` instead of the Requests library default.

### Fixed
- Deployments that infer a remote file's checksum and size now stream it in chunks instead of buffering the full response.

## [0.15] - 2025-12-31

### Added
Expand Down
16 changes: 9 additions & 7 deletions databusclient/api/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,15 @@ def _load_file_stats(url: str) -> Tuple[str, int]:
information in the CLI or metadata file.
"""

resp = requests.get(url, timeout=30)
if resp.status_code >= 400:
raise requests.exceptions.RequestException(response=resp)

sha256sum = hashlib.sha256(bytes(resp.content)).hexdigest()
content_length = len(resp.content)
return sha256sum, content_length
checksum = hashlib.sha256()
content_length = 0
with requests.get(url, timeout=30, stream=True) as response:
if response.status_code >= 400:
raise requests.exceptions.RequestException(response=response)
for chunk in response.iter_content(chunk_size=64 * 1024):
checksum.update(chunk)
content_length += len(chunk)
return checksum.hexdigest(), content_length


def get_file_info(distribution_str: str) -> Tuple[Dict[str, str], str, str, str, int]:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "databusclient"
version = "1.3.2"
version = "1.3.3"
description = "A simple client for submitting, downloading, and deleting data on the DBpedia Databus"
authors = ["DBpedia Association"]
license = "Apache-2.0 License"
Expand Down
70 changes: 70 additions & 0 deletions tests/test_deploy_streaming_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""Offline coverage for metadata inferred from remote deployment files."""

from hashlib import sha256
from unittest.mock import MagicMock, PropertyMock

import pytest
import requests

from databusclient.api.deploy import get_file_info


URL = "https://example.org/file.ttl.gz"


@pytest.fixture
def remote_file(monkeypatch):
response = MagicMock(spec=requests.Response)
type(response).content = PropertyMock(
side_effect=AssertionError("deployment metadata must not buffer the response")
)
response.status_code = 200
response.__enter__.return_value = response
get = MagicMock(return_value=response)
monkeypatch.setattr(requests, "get", get)
return get, response


@pytest.mark.parametrize("chunks", [[], [b"first chunk", b"second chunk"]])
def test_inferred_file_metadata_uses_streamed_bytes(remote_file, chunks):
get, response = remote_file
response.iter_content.return_value = iter(chunks)
payload = b"".join(chunks)

assert get_file_info(URL) == (
{},
"ttl",
"gz",
sha256(payload).hexdigest(),
len(payload),
)

get.assert_called_once_with(URL, stream=True, timeout=30)
response.__exit__.assert_called_once_with(None, None, None)


def test_failed_metadata_request_closes_response_without_reading_body(remote_file):
_, response = remote_file
response.status_code = 404

with pytest.raises(requests.exceptions.RequestException) as error:
get_file_info(URL)

assert error.value.response is response
response.iter_content.assert_not_called()
response.__exit__.assert_called_once()


def test_interrupted_metadata_stream_closes_response(remote_file):
_, response = remote_file

def interrupted_chunks(*args, **kwargs):
yield b"partial body"
raise requests.exceptions.ChunkedEncodingError("connection interrupted")

response.iter_content.side_effect = interrupted_chunks

with pytest.raises(requests.exceptions.ChunkedEncodingError, match="interrupted"):
get_file_info(URL)

response.__exit__.assert_called_once()