Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding extra headers feature. #79

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
johanseto marked this conversation as resolved.
Show resolved Hide resolved

@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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the use of extra_headers_key. Needs the change of the empty dict to request.Session()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have been modified in the previous refactor since the _authenticate method returns a Session instead of a dict

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
Loading