Skip to content

Commit

Permalink
feat: add endpoint authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
alangsto committed Aug 22, 2023
1 parent b401572 commit 60cc329
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion learning_assistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 7 additions & 4 deletions learning_assistant/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Utils file for learning-assistant.
"""
import json
import logging

import requests
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!'}
Expand Down Expand Up @@ -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)
)

0 comments on commit 60cc329

Please sign in to comment.