Skip to content
This repository has been archived by the owner on Mar 12, 2022. It is now read-only.

Improve error handling from api like subscription or quota #5

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
10 changes: 8 additions & 2 deletions ibancom/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# -*- coding: utf-8 -*-
from .ibancom import IBAN, IBANClient, IBANException, IBANValidationException
from .ibancom import (
IBAN,
IBANClient,
IBANException,
IBANValidationException,
IBANApiException,
)

__version__ = "0.6.2"

Expand All @@ -9,4 +14,5 @@
"IBAN",
"IBANValidationException",
"IBANException",
"IBANApiException",
]
8 changes: 6 additions & 2 deletions ibancom/ibancom.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

import requests
from requests.exceptions import ConnectionError, HTTPError

Expand All @@ -12,6 +10,10 @@ class IBANValidationException(Exception):
pass


class IBANApiException(Exception):
pass


class IBAN(object):
def __init__(self, validation_errors, sepa_data, **data):
self.validation_errors = validation_errors
Expand Down Expand Up @@ -40,6 +42,8 @@ def __init__(self, api_key, api_url=None):

def get(self, iban):
data = self._fetch_data(iban)
if "errors" in data and "validations" not in data:
raise IBANApiException(data["errors"][0]["message"])
validation_errors = [
x for x in data["validations"] if not x["code"].startswith("00")
]
Expand Down
26 changes: 26 additions & 0 deletions tests/test_ibancom.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ def mocked_failing_requests_get(*args, **kwargs):
raise ConnectionError


def mocked_requests_subscription_error(*args, **kwargs):
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code

def json(self):
return self.json_data

data = copy.deepcopy(TEST_IBAN_DATA)
del data["validations"]
data["errors"] = [
{"code": 302, "message": "Subscription expired"}
]

return MockResponse(data, 200)


@mock.patch("requests.get", side_effect=mocked_requests_get)
def test_get_bic(request_get):
client = ibancom.IBANClient(api_key="FAKE_KEY")
Expand Down Expand Up @@ -130,3 +148,11 @@ def test_iban_object_raises_attr_error():
iban = ibancom.IBAN(None, None)
with pytest.raises(AttributeError):
iban.some_magical_attributes


@mock.patch("requests.get", side_effect=mocked_requests_subscription_error)
def test_subscription_errpr(request_get):
client = ibancom.IBANClient(api_key="FAKE_KEY")
with pytest.raises(ibancom.IBANApiException) as exc_info:
client.get(iban=TEST_IBAN)
assert str(exc_info.value) == "Subscription expired"