Skip to content

Commit 9343356

Browse files
feat(multiple-auth): improvements in multiple auth implementation (#48)
This commit adds improvements in the implementation of multiple authentication schemes. The improvements include the addition of auth-validation exception type and early returning in case of OR authentication group. closes #47
1 parent b6a84b8 commit 9343356

13 files changed

Lines changed: 101 additions & 53 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ pip install apimatic-core
4646
|--------------------------------------------------------------|--------------------------------------------------------------------------------------|
4747
| [`LazyProperty`](apimatic_core/decorators/lazy_property.py) | A decorator class for lazy instantiation |
4848

49+
## Exceptions
50+
| Name | Description |
51+
|--------------------------------------------------------------------------------------|--------------------------------------------------------------------------|
52+
| [`OneOfValidationException`](apimatic_core/exceptions/oneof_validation_exception.py) | An exception class for the failed validation of oneOf (union-type) cases |
53+
| [`AnyOfValidationException`](apimatic_core/exceptions/anyof_validation_exception.py) | An exception class for the failed validation of anyOf (union-type) cases |
54+
| [`AuthValidationException`](apimatic_core/exceptions/auth_validation_exception.py) | An exception class for the failed validation of authentication schemes |
55+
4956
## Factories
5057
| Name | Description |
5158
|---------------------------------------------------------------------------|-----------------------------------------------------------------------------|

apimatic_core/authentication/multiple/or_auth_group.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ def is_valid(self):
1616
return False
1717

1818
for participant in self.mapped_group:
19-
if participant.is_valid():
20-
self._is_valid_group = True
21-
else:
22-
self.error_messages.append(participant.error_message)
19+
self._is_valid_group = participant.is_valid()
20+
# returning as we encounter the first participant as valid
21+
if self._is_valid_group:
22+
return self._is_valid_group
2323

24-
return self.is_valid_group
24+
self.error_messages.append(participant.error_message)
25+
26+
return self._is_valid_group
2527

apimatic_core/authentication/multiple/single_auth.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __init__(self, auth_participant):
1212
self._auth_participant = auth_participant
1313
self._mapped_auth = None
1414
self._error_message = None
15-
self._is_valid = True
15+
self._is_valid = False
1616

1717
def with_auth_managers(self, auth_managers):
1818
if not auth_managers.get(self._auth_participant):
@@ -23,9 +23,9 @@ def with_auth_managers(self, auth_managers):
2323
return self
2424

2525
def is_valid(self):
26-
if not self._mapped_auth.is_valid():
26+
self._is_valid = self._mapped_auth.is_valid()
27+
if not self._is_valid:
2728
self._error_message = self._mapped_auth.error_message
28-
self._is_valid = False
2929

3030
return self._is_valid
3131

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
__all__ = [
22
'oneof_validation_exception',
3-
'anyof_validation_exception'
3+
'anyof_validation_exception',
4+
'auth_validation_exception'
45
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
"""
2+
This is an exception class which will be raised when union type validation fails.
3+
"""
4+
15

26
class AnyOfValidationException(Exception):
7+
38
def __init__(self, message):
49
self.message = message
510
super().__init__(self.message)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
This is an exception class which will be raised when auth validation fails.
3+
"""
4+
5+
6+
class AuthValidationException(Exception):
7+
8+
def __init__(self, message):
9+
self.message = message
10+
super().__init__(self.message)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
"""
2+
This is an exception class which will be raised when union type validation fails.
3+
"""
4+
15

26
class OneOfValidationException(Exception):
7+
38
def __init__(self, message):
49
self.message = message
510
super().__init__(self.message)

apimatic_core/request_builder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from apimatic_core.exceptions.auth_validation_exception import AuthValidationException
12
from apimatic_core.http.request.http_request import HttpRequest
23
from apimatic_core.types.array_serialization_format import SerializationFormats
34
from apimatic_core.utilities.api_helper import ApiHelper
@@ -229,4 +230,4 @@ def apply_auth(self, auth_managers, http_request):
229230
if self._auth.with_auth_managers(auth_managers).is_valid():
230231
self._auth.apply(http_request)
231232
else:
232-
raise PermissionError(self._auth.error_message)
233+
raise AuthValidationException(self._auth.error_message)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name='apimatic-core',
15-
version='0.2.5',
15+
version='0.2.6',
1616
description='A library that contains core logic and utilities for '
1717
'consuming REST APIs using Python SDKs generated by APIMatic.',
1818
long_description=long_description,

tests/apimatic_core/api_logger_tests/test_api_logger.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ def test_end_to_end_success_case(self):
3030
'Calling the on_before_request method of http_call_back for end-to-end-test.',
3131
'Raw request for end-to-end-test is: {\'http_method\': \'POST\', \'query_url\': '
3232
'\'http://localhost:3000/body/model\', \'headers\': {\'Content-Type\': '
33-
'\'application/json\', \'accept\': \'application/json\', \'Authorization\': '
33+
'\'application/json\', \'accept\': \'application/json\', \'Basic-Authorization\': '
3434
'\'Basic dGVzdF91c2VybmFtZTp0ZXN0X3Bhc3N3b3Jk\'}, \'query_parameters\': None, '
3535
'\'parameters\': \'{"Key": "Value"}\', \'files\': {}}',
3636
'Raw response for end-to-end-test is: {\'status_code\': 200, \'reason_phrase\': None, '
3737
'\'headers\': {\'Content-Type\': \'application/json\', \'accept\': \'application/json\', '
38-
'\'Authorization\': \'Basic dGVzdF91c2VybmFtZTp0ZXN0X3Bhc3N3b3Jk\'}, \'text\': '
38+
'\'Basic-Authorization\': \'Basic dGVzdF91c2VybmFtZTp0ZXN0X3Bhc3N3b3Jk\'}, \'text\': '
3939
'\'{"Key": "Value"}\', \'request\': {\'http_method\': \'POST\', \'query_url\': '
4040
'\'http://localhost:3000/body/model\', \'headers\': {\'Content-Type\': '
41-
'\'application/json\', \'accept\': \'application/json\', \'Authorization\': '
41+
'\'application/json\', \'accept\': \'application/json\', \'Basic-Authorization\': '
4242
'\'Basic dGVzdF91c2VybmFtZTp0ZXN0X3Bhc3N3b3Jk\'}, \'query_parameters\': None, '
4343
'\'parameters\': \'{"Key": "Value"}\', \'files\': {}}}',
4444
'Calling on_after_response method of http_call_back for end-to-end-test.',

0 commit comments

Comments
 (0)