Skip to content

Commit

Permalink
feat: adding extra headers feature
Browse files Browse the repository at this point in the history
This allows to include header from the django settings in the api _client session
https://edunext.atlassian.net/browse/FUTUREX-490
  • Loading branch information
andrey-canon committed Aug 8, 2023
1 parent 0d182c4 commit e39e580
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
14 changes: 14 additions & 0 deletions eox_nelp/api_clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
class AbstractApiClient(ABC):
"""Abstract api client class, this implement a basic authentication method and defines methods POST and GET"""

extra_headers_key = None

@property
@abstractmethod
def base_url(self):
Expand All @@ -31,12 +33,24 @@ def __init__(self):
Abstract ApiClient creator, this will set the session based on the authenticate result.
"""
self.session = self._authenticate()
self.session.headers.update(self._get_extra_headers())

@abstractmethod
def _authenticate(self):
"""Abstract method that should return a requests Session instance in its implementation."""
raise NotImplementedError

def _get_extra_headers(self):
"""This verify the extra_headers_key attribute and returns its value from the django settings.
Returns
Dict: The extra_headers_key must be set a dictionary.
"""
if self.extra_headers_key:
return getattr(settings, self.extra_headers_key, {})

return {}

def make_post(self, path, data):
"""This method uses the session attribute to perform a POST request based on the
base_url attribute and the given path, if the response has a status code 200
Expand Down
2 changes: 2 additions & 0 deletions eox_nelp/api_clients/certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
class ExternalCertificatesApiClient(AbstractBasicAuthApiClient):
"""Allow to perform multiple external certificates operations."""

extra_headers_key = "EXTERNAL_CERTIFICATES_EXTRA_HEADERS"

def __init__(self):
self.user = getattr(settings, "EXTERNAL_CERTIFICATES_USER")
self.password = getattr(settings, "EXTERNAL_CERTIFICATES_PASSWORD")
Expand Down
14 changes: 13 additions & 1 deletion eox_nelp/api_clients/tests/tests_certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
import unittest

from django.test import override_settings
from django.utils import timezone
from mock import Mock, patch

Expand Down Expand Up @@ -59,6 +60,17 @@ def test_failed_create_certificate(self):
- Raise KeyError exception.
"""
data = {}
api_client = ExternalCertificatesApiClient()
api_client = self.api_class()

self.assertRaises(KeyError, api_client.create_external_certificate, data)

@override_settings(EXTERNAL_CERTIFICATES_EXTRA_HEADERS={"HTTP_CUSTOM_HEADER": "ABC123"})
def test_extra_headers(self):
"""Test if the extra header has been added to the session
Expected behavior:
- Custom Header in session headers.
"""
api_client = self.api_class()

self.assertEqual(api_client.session.headers["HTTP_CUSTOM_HEADER"], "ABC123")
5 changes: 3 additions & 2 deletions eox_nelp/api_clients/tests/tests_futurex.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
import unittest

import requests
from mock import patch

from eox_nelp.api_clients.futurex import FuturexApiClient, FuturexMissingArguments
Expand All @@ -27,7 +28,7 @@ def test_enrollment_progress(self, auth_mock, post_mock):
- Response is the expected value
- make_post was called with the right values.
"""
auth_mock.return_value = {}
auth_mock.return_value = requests.Session()
expected_value = {
"status": {"success": True, "message": "successful", "code": 1}
}
Expand Down Expand Up @@ -55,7 +56,7 @@ def test_failed_enrollment_progress(self, auth_mock):
Expected behavior:
- Raise FuturexMissingArguments exception
"""
auth_mock.return_value = {}
auth_mock.return_value = requests.Session()
data = {
"courseId": "course-v1:lms152",
"userId": 52,
Expand Down

0 comments on commit e39e580

Please sign in to comment.