Skip to content

Commit 2b02b29

Browse files
authored
Add IBAN validation service (#12)
1 parent b2cbbcc commit 2b02b29

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

src/abstract_api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
from .avatars import Avatars
44
from .email_validation import EmailValidation
5+
from .iban_validation import IBANValidation
56
from .ip_geolocation import IPGeolocation
67
from .phone_validation import PhoneValidation
78
from .vat import VAT
89

910
__all__: Final[list[str]] = [
1011
"Avatars",
1112
"EmailValidation",
13+
"IBANValidation",
1214
"IPGeolocation",
1315
"PhoneValidation",
1416
"VAT"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import Final
2+
3+
from .iban_validation import IBANValidation
4+
from .iban_validation_response import IBANValidationResponse
5+
6+
__all__: Final[list[str]] = [
7+
"IBANValidation",
8+
"IBANValidationResponse"
9+
]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from abstract_api.bases import BaseService
2+
from abstract_api.exceptions import ResponseParseError
3+
4+
from .iban_validation_response import IBANValidationResponse
5+
6+
7+
class IBANValidation(BaseService):
8+
"""AbstractAPI IBAN validation and verification service.
9+
10+
Used to validate and verify a IBAN number.
11+
12+
Attributes:
13+
_subdomain: IBAN validation service subdomain.
14+
"""
15+
_subdomain: str = "ibanvalidation"
16+
17+
def check(self, iban: str) -> IBANValidationResponse:
18+
"""Validates an IBAN.
19+
20+
Args:
21+
iban: The IBAN to validate. Note that the API will accept white
22+
spaces, so BE71 0961 2345 6769 is considered as valid
23+
as BE71096123456769.
24+
25+
Returns:
26+
IBANValidationResponse representing API call response.
27+
"""
28+
response = self._service_request(iban=iban)
29+
30+
# TODO: Move to parent
31+
try:
32+
iban_validation_response = IBANValidationResponse(
33+
response=response
34+
)
35+
except Exception as e:
36+
raise ResponseParseError(
37+
"Failed to parse response as IBANValidationResponse"
38+
) from e
39+
40+
return iban_validation_response
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import TYPE_CHECKING
2+
3+
import requests
4+
5+
from abstract_api.bases import JSONResponse
6+
7+
from .response_fields import RESPONSE_FIELDS
8+
9+
10+
class IBANValidationResponse(JSONResponse):
11+
"""IBAN validation service response."""
12+
13+
def __init__(self, response: requests.models.Response) -> None:
14+
"""Initializes a new IBANValidationResponse."""
15+
super().__init__(response)
16+
self._response_fields = RESPONSE_FIELDS
17+
for field in RESPONSE_FIELDS:
18+
if TYPE_CHECKING:
19+
assert isinstance(self.meta.body_json, dict)
20+
value = self.meta.body_json.get(field)
21+
# TODO: Move to parent class
22+
setattr(self, f"_{field}", value)
23+
24+
@property
25+
def iban(self) -> str:
26+
"""The IBAN submitted for validation."""
27+
return self._get_response_field("iban")
28+
29+
@property
30+
def is_valid(self) -> bool:
31+
"""Whether the IBAN submitted is valid."""
32+
return self._get_response_field("is_valid")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""Response fields of IBAN validation service endpoint."""
2+
3+
4+
RESPONSE_FIELDS: frozenset[str] = frozenset({
5+
"iban",
6+
"is_valid"
7+
})

0 commit comments

Comments
 (0)