🐛(backend) fix issues with conversion microservice integration

Minor adjustments were needed after working in parallel on two PRs.
The microservice now accepts an API key without requiring it as a Bearer token.

A mistake in reading the microservice response was corrected after refactoring
the serializer to delegate logic to the converter microservice.
This commit is contained in:
lebaudantoine
2024-12-13 12:21:55 +01:00
committed by aleb_the_flash
parent dc9b375ff5
commit 52534db3e1
5 changed files with 13 additions and 6 deletions

View File

@@ -275,7 +275,7 @@ class ServerCreateDocumentSerializer(serializers.Serializer):
language = user.language or language
try:
converter_response = YdocConverter().convert_markdown(
document_content = YdocConverter().convert_markdown(
validated_data["content"]
)
except ConversionError as err:
@@ -283,7 +283,7 @@ class ServerCreateDocumentSerializer(serializers.Serializer):
document = models.Document.objects.create(
title=validated_data["title"],
content=converter_response["content"],
content=document_content,
creator=user,
)

View File

@@ -31,7 +31,7 @@ class YdocConverter:
@property
def auth_header(self):
"""Build microservice authentication header."""
return f"Bearer {settings.CONVERSION_API_KEY}"
return settings.CONVERSION_API_KEY
def convert_markdown(self, text):
"""Convert a Markdown text into our internal format using an external microservice."""
@@ -50,6 +50,7 @@ class YdocConverter:
"Content-Type": "application/json",
},
timeout=settings.CONVERSION_API_TIMEOUT,
verify=settings.CONVERSION_API_SECURE,
)
response.raise_for_status()
conversion_response = response.json()

View File

@@ -25,7 +25,7 @@ def mock_convert_markdown():
with patch.object(
YdocConverter,
"convert_markdown",
return_value={"content": "Converted document content"},
return_value="Converted document content",
) as mock:
yield mock

View File

@@ -18,7 +18,7 @@ def test_auth_header(settings):
"""Test authentication header generation."""
settings.CONVERSION_API_KEY = "test-key"
converter = YdocConverter()
assert converter.auth_header == "Bearer test-key"
assert converter.auth_header == "test-key"
def test_convert_markdown_empty_text():
@@ -116,10 +116,11 @@ def test_convert_markdown_full_integration(mock_post, settings):
"http://test.com",
json={"content": "test markdown"},
headers={
"Authorization": "Bearer test-key",
"Authorization": "test-key",
"Content-Type": "application/json",
},
timeout=5,
verify=False,
)

View File

@@ -524,6 +524,11 @@ class Base(Configuration):
environ_name="CONVERSION_API_TIMEOUT",
environ_prefix=None,
)
CONVERSION_API_SECURE = values.Value(
default=False,
environ_name="CONVERSION_API_SECURE",
environ_prefix=None,
)
# Logging
# We want to make it easy to log to console but by default we log production