♻️(backend) raw payloads on convert endpoint

Handle the raw payloads in requests and responses to convert-endpoint.

This change replaces Base64-encoded I/O with direct binary streaming,
yielding several benefits:
- **Network efficiency**: Eliminates the ~33% size inflation of Base64,
cutting bandwidth and latency.
- **Memory savings**: Enables piping DOCX (already compressed) buffers
straight to DocSpec API without holding, encoding and decoding multi-MB
payload in RAM.

Signed-off-by: Stephan Meijer <me@stephanmeijer.com>
This commit is contained in:
Stephan Meijer
2025-07-04 13:29:21 +02:00
parent fde520a6f3
commit 78a6772bab
2 changed files with 12 additions and 72 deletions

View File

@@ -1,13 +1,12 @@
"""Test converter services."""
from base64 import b64decode
from unittest.mock import MagicMock, patch
import pytest
import requests
from core.services.converter_services import (
InvalidResponseError,
MissingContentError,
ServiceUnavailableError,
ValidationError,
YdocConverter,
@@ -58,41 +57,6 @@ def test_convert_http_error(mock_post):
converter.convert("test text")
@patch("requests.post")
def test_convert_invalid_json_response(mock_post):
"""Should raise InvalidResponseError when response is not valid JSON."""
converter = YdocConverter()
mock_response = MagicMock()
mock_response.json.side_effect = ValueError("Invalid JSON")
mock_post.return_value = mock_response
with pytest.raises(
InvalidResponseError,
match="Could not parse conversion service response",
):
converter.convert("test text")
@patch("requests.post")
def test_convert_missing_content_field(mock_post, settings):
"""Should raise MissingContentError when response is missing required field."""
settings.CONVERSION_API_CONTENT_FIELD = "expected_field"
converter = YdocConverter()
mock_response = MagicMock()
mock_response.json.return_value = {"wrong_field": "content"}
mock_post.return_value = mock_response
with pytest.raises(
MissingContentError,
match="Response missing required field: expected_field",
):
converter.convert("test text")
@patch("requests.post")
def test_convert_full_integration(mock_post, settings):
"""Test full integration with all settings."""
@@ -105,20 +69,21 @@ def test_convert_full_integration(mock_post, settings):
converter = YdocConverter()
expected_content = {"converted": "content"}
expected_content = b"converted content"
mock_response = MagicMock()
mock_response.json.return_value = {"content": expected_content}
mock_response.content = expected_content
mock_post.return_value = mock_response
result = converter.convert("test markdown")
assert result == expected_content
assert b64decode(result) == expected_content
mock_post.assert_called_once_with(
"http://test.com/conversion-endpoint/",
json={"content": "test markdown"},
data="test markdown",
headers={
"Authorization": "test-key",
"Content-Type": "application/json",
"Content-Type": "text/markdown",
},
timeout=5,
verify=False,