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

Commit

Permalink
Improve error handling from api like subscription or quota
Browse files Browse the repository at this point in the history
refs #4
  • Loading branch information
Mounir Messelmeni committed Sep 28, 2021
1 parent 65b8401 commit 1295fdf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
1 change: 0 additions & 1 deletion ibancom/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
from .ibancom import IBAN, IBANClient, IBANException, IBANValidationException

__version__ = "0.6.2"
Expand Down
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 exc_info.value.message == "Subscription expired"

0 comments on commit 1295fdf

Please sign in to comment.