♻️(backend) generalize YProvider API config

Abstracted base URL and API key under 'y-provider' for
reuse in future endpoints, aligning with microservice naming.

Please note the YProvider API here is internal to the cluster.
In facts, we don't want these endpoints to be exposed by any ingress
This commit is contained in:
lebaudantoine
2024-12-15 18:31:43 +01:00
committed by aleb_the_flash
parent ba63358098
commit c10808b611
4 changed files with 22 additions and 13 deletions

View File

@@ -31,7 +31,7 @@ class YdocConverter:
@property @property
def auth_header(self): def auth_header(self):
"""Build microservice authentication header.""" """Build microservice authentication header."""
return settings.CONVERSION_API_KEY return settings.Y_PROVIDER_API_KEY
def convert_markdown(self, text): def convert_markdown(self, text):
"""Convert a Markdown text into our internal format using an external microservice.""" """Convert a Markdown text into our internal format using an external microservice."""
@@ -41,7 +41,7 @@ class YdocConverter:
try: try:
response = requests.post( response = requests.post(
settings.CONVERSION_API_URL, f"{settings.Y_PROVIDER_API_BASE_URL}{settings.CONVERSION_API_ENDPOINT}/",
json={ json={
"content": text, "content": text,
}, },

View File

@@ -16,7 +16,7 @@ from core.services.converter_services import (
def test_auth_header(settings): def test_auth_header(settings):
"""Test authentication header generation.""" """Test authentication header generation."""
settings.CONVERSION_API_KEY = "test-key" settings.Y_PROVIDER_API_KEY = "test-key"
converter = YdocConverter() converter = YdocConverter()
assert converter.auth_header == "test-key" assert converter.auth_header == "test-key"
@@ -97,8 +97,9 @@ def test_convert_markdown_missing_content_field(mock_post, settings):
def test_convert_markdown_full_integration(mock_post, settings): def test_convert_markdown_full_integration(mock_post, settings):
"""Test full integration with all settings.""" """Test full integration with all settings."""
settings.CONVERSION_API_URL = "http://test.com" settings.Y_PROVIDER_API_BASE_URL = "http://test.com/"
settings.CONVERSION_API_KEY = "test-key" settings.Y_PROVIDER_API_KEY = "test-key"
settings.CONVERSION_API_ENDPOINT = "conversion-endpoint"
settings.CONVERSION_API_TIMEOUT = 5 settings.CONVERSION_API_TIMEOUT = 5
settings.CONVERSION_API_CONTENT_FIELD = "content" settings.CONVERSION_API_CONTENT_FIELD = "content"
@@ -113,7 +114,7 @@ def test_convert_markdown_full_integration(mock_post, settings):
assert result == expected_content assert result == expected_content
mock_post.assert_called_once_with( mock_post.assert_called_once_with(
"http://test.com", "http://test.com/conversion-endpoint/",
json={"content": "test markdown"}, json={"content": "test markdown"},
headers={ headers={
"Authorization": "test-key", "Authorization": "test-key",

View File

@@ -505,13 +505,21 @@ class Base(Configuration):
"day": 200, "day": 200,
} }
# Conversion microservice # Y provider microservice
CONVERSION_API_KEY = values.Value( # Note: Be careful, this value is currently the same as in the collaboration service.
environ_name="CONVERSION_API_KEY", Y_PROVIDER_API_KEY = values.Value(
environ_name="Y_PROVIDER_API_KEY",
environ_prefix=None, environ_prefix=None,
) )
CONVERSION_API_URL = values.Value( Y_PROVIDER_API_BASE_URL = values.Value(
environ_name="CONVERSION_API_URL", environ_name="Y_PROVIDER_API_BASE_URL",
environ_prefix=None,
)
# Conversion endpoint
CONVERSION_API_ENDPOINT = values.Value(
default="convert-markdown",
environ_name="CONVERSION_API_ENDPOINT",
environ_prefix=None, environ_prefix=None,
) )
CONVERSION_API_CONTENT_FIELD = values.Value( CONVERSION_API_CONTENT_FIELD = values.Value(

View File

@@ -51,8 +51,8 @@ backend:
AWS_S3_SECRET_ACCESS_KEY: password AWS_S3_SECRET_ACCESS_KEY: password
AWS_STORAGE_BUCKET_NAME: impress-media-storage AWS_STORAGE_BUCKET_NAME: impress-media-storage
STORAGES_STATICFILES_BACKEND: django.contrib.staticfiles.storage.StaticFilesStorage STORAGES_STATICFILES_BACKEND: django.contrib.staticfiles.storage.StaticFilesStorage
CONVERSION_API_URL: http://impress-y-provider:443/api/convert-markdown/ Y_PROVIDER_API_BASE_URL: http://impress-y-provider:443/api/
CONVERSION_API_KEY: my-secret Y_PROVIDER_API_KEY: my-secret
migrate: migrate:
command: command: