diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 73117a2..c4ead4a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,6 +11,11 @@ Change Log .. There should always be an "Unreleased" section for changes pending release. +1.2.0 - 2023-08-22 +****************** +* add endpoint authentication +* fix request structure required for endpoint integration + 1.1.0 - 2023-08-09 ****************** * fix for course id to course key conversion diff --git a/learning_assistant/__init__.py b/learning_assistant/__init__.py index 6d29d3f..416c060 100644 --- a/learning_assistant/__init__.py +++ b/learning_assistant/__init__.py @@ -2,6 +2,6 @@ Plugin for a learning assistant backend, intended for use within edx-platform. """ -__version__ = '1.1.0' +__version__ = '1.2.0' default_app_config = 'learning_assistant.apps.LearningAssistantConfig' # pylint: disable=invalid-name diff --git a/learning_assistant/utils.py b/learning_assistant/utils.py index 9b47394..5920cd0 100644 --- a/learning_assistant/utils.py +++ b/learning_assistant/utils.py @@ -1,6 +1,7 @@ """ Utils file for learning-assistant. """ +import json import logging import requests @@ -16,16 +17,18 @@ def get_chat_response(message_list): Pass message list to chat endpoint, as defined by the CHAT_COMPLETION_API setting. """ completion_endpoint = getattr(settings, 'CHAT_COMPLETION_API', None) - if completion_endpoint: - headers = {'Content-Type': 'application/json'} + completion_endpoint_key = getattr(settings, 'CHAT_COMPLETION_API_KEY', None) + if completion_endpoint and completion_endpoint_key: + headers = {'Content-Type': 'application/json', 'x-api-key': completion_endpoint_key} connect_timeout = getattr(settings, 'CHAT_COMPLETION_API_CONNECT_TIMEOUT', 1) - read_timeout = getattr(settings, 'CHAT_COMPLETION_API_READ_TIMEOUT', 10) + read_timeout = getattr(settings, 'CHAT_COMPLETION_API_READ_TIMEOUT', 15) + body = {'message_list': message_list} try: response = requests.post( completion_endpoint, headers=headers, - data=message_list, + data=json.dumps(body), timeout=(connect_timeout, read_timeout) ) chat = response.json() diff --git a/test_settings.py b/test_settings.py index e6a3c04..d3d5955 100644 --- a/test_settings.py +++ b/test_settings.py @@ -61,5 +61,6 @@ def root(*args): }] CHAT_COMPLETION_API = 'https://test.edx.org/' +CHAT_COMPLETION_API_KEY = 'endpoint_key' CHAT_COMPLETION_API_CONNECT_TIMEOUT = 0.5 CHAT_COMPLETION_API_READ_TIMEOUT = 10 diff --git a/tests/test_utils.py b/tests/test_utils.py index d46d18d..fad8966 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -31,6 +31,12 @@ def test_no_endpoint_setting(self): self.assertEqual(status_code, 404) self.assertEqual(message, 'Completion endpoint is not defined.') + @override_settings(CHAT_COMPLETION_API_KEY=None) + def test_no_endpoint_key_setting(self): + status_code, message = get_chat_response(self.message_list) + self.assertEqual(status_code, 404) + self.assertEqual(message, 'Completion endpoint is not defined.') + @responses.activate def test_200_response(self): message_response = {'role': 'assistant', 'content': 'See you later!'} @@ -68,3 +74,21 @@ def test_timeout(self, exception, mock_requests): mock_requests.post = MagicMock(side_effect=exception()) status_code, _ = get_chat_response(self.message_list) self.assertEqual(status_code, 502) + + @patch('learning_assistant.utils.requests') + def test_post_request_structure(self, mock_requests): + mock_requests.post = MagicMock() + + completion_endpoint = settings.CHAT_COMPLETION_API + connect_timeout = settings.CHAT_COMPLETION_API_CONNECT_TIMEOUT + read_timeout = settings.CHAT_COMPLETION_API_READ_TIMEOUT + headers = {'Content-Type': 'application/json', 'x-api-key': settings.CHAT_COMPLETION_API_KEY} + body = json.dumps({'message_list': self.message_list}) + + get_chat_response(self.message_list) + mock_requests.post.assert_called_with( + completion_endpoint, + headers=headers, + data=body, + timeout=(connect_timeout, read_timeout) + )