Skip to content

Commit 0ebf702

Browse files
committed
Added exceptions
1 parent 7d81e26 commit 0ebf702

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "simplefin4py"
3-
version = "0.0.7"
3+
version = "0.0.8"
44
description = ""
55
authors = ["Jeef <[email protected]>"]
66
readme = "README.md"

simplefin4py/exceptions.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,18 @@ def __init__(self) -> None:
3232
super().__init__(self.message)
3333

3434

35-
class SimpleFinPaymentError(Exception):
35+
class SimpleFinPaymentRequiredError(Exception):
3636
"""A 402 error will raise a Payment Required."""
3737

3838
def __init__(self) -> None:
3939
"""Initialize the exception."""
4040
self.message = "Payment Required"
4141
super().__init__(self.message)
42+
43+
44+
class SimpleFinInvalidAccountURLError(Exception):
45+
"""Invalid authorization URL."""
46+
47+
def __init__(self) -> None:
48+
"""Initialize the exception."""
49+
self.message = "There was an issue parsing the Account URL."

simplefin4py/simplefin.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88

99
from aiohttp import BasicAuth, ClientConnectorError, ClientConnectorSSLError
1010

11-
from .exceptions import SimpleFinClaimError, SimpleFinInvalidClaimTokenError
11+
from .exceptions import (
12+
SimpleFinClaimError,
13+
SimpleFinInvalidClaimTokenError,
14+
SimpleFinInvalidAccountURLError,
15+
SimpleFinPaymentRequiredError,
16+
SimpleFinAuthError,
17+
)
1218
from .model import FinancialData
1319
from .const import LOGGER
1420

@@ -62,16 +68,12 @@ def __init__(
6268
scheme, rest = access_url.split("//", 1)
6369
try:
6470
self.auth, rest = rest.split("@", 1)
65-
except ValueError as e:
66-
raise e
71+
except ValueError as err:
72+
raise SimpleFinInvalidAccountURLError from err
6773
self.url = scheme + "//" + rest + "/accounts"
6874
self.username, self.password = self.auth.split(":", 1)
6975
self.proxy: str | None = proxy
7076

71-
# self.auth already is this i think..??
72-
# self.auth = BasicAuth(self.username, self.password)
73-
# self.session = self._create_session(auth, verify_ssl)
74-
7577
async def fetch_data(self) -> FinancialData:
7678
"""Fetch financial data from SimpleFin and return as FinancialData object."""
7779
LOGGER.debug("Starting fetch_data in SimpleFin")
@@ -90,14 +92,19 @@ async def fetch_data(self) -> FinancialData:
9092
response = await session.get(
9193
self.access_url + "/accounts", **request_params
9294
)
93-
response.raise_for_status()
95+
96+
if response.status == 402:
97+
raise SimpleFinPaymentRequiredError()
98+
if response.status == 403:
99+
raise SimpleFinAuthError()
100+
94101
data = await response.json()
95102
LOGGER.debug(f"Received data: {data}")
96103
financial_data: FinancialData = FinancialData.from_dict(data) # type: ignore[attr-defined]
97104
LOGGER.debug(f"Parsed FinancialData: {financial_data}")
98105
return financial_data
99-
except (ClientConnectorError, ClientConnectorSSLError) as e:
100-
raise e
106+
except (ClientConnectorError, ClientConnectorSSLError) as err:
107+
raise err
101108
except Exception as e:
102109
print(e)
103110
raise e

tests/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,12 @@ def mock_get_data_403(data_403: str) -> Generator[aioresponses, None, None]:
9797
# Mock a failed login attempt due to bad credentials.
9898
m.get(MOCK_ACCESS_URL + "/accounts", status=403, body=data_403)
9999
yield m
100+
101+
102+
@pytest.fixture() # type: ignore
103+
def mock_get_data_generic_error() -> Generator[aioresponses, None, None]:
104+
"""Fixture for mocking a fech process with bad credentials/url."""
105+
with aioresponses() as m:
106+
# Mock a failed login attempt due to bad credentials.
107+
m.get(MOCK_ACCESS_URL + "/accounts", status=200, body="<,asduB*J(_8asdji>")
108+
yield m

tests/test_simplefin.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
"""Tests for stuff."""
22

3-
import aiohttp
43
import pytest
54
from aioresponses import aioresponses
65

76
from simplefin4py import SimpleFin
8-
from simplefin4py.exceptions import SimpleFinInvalidClaimTokenError, SimpleFinClaimError
7+
from simplefin4py.exceptions import (
8+
SimpleFinInvalidClaimTokenError,
9+
SimpleFinClaimError,
10+
SimpleFinPaymentRequiredError,
11+
SimpleFinAuthError,
12+
)
913
from tests.conftest import MOCK_ACCESS_URL
1014

1115

@@ -40,15 +44,15 @@ async def test_claim_token_403(mock_claim_token_403, mock_claim_token):
4044
async def test_access_402(mock_get_data_402) -> None:
4145
"""Test a failed access token."""
4246
sf = SimpleFin(MOCK_ACCESS_URL)
43-
with pytest.raises(aiohttp.client_exceptions.ClientResponseError):
47+
with pytest.raises(SimpleFinPaymentRequiredError):
4448
await sf.fetch_data()
4549

4650

4751
@pytest.mark.asyncio # type: ignore
4852
async def test_access_403(mock_get_data_403) -> None:
4953
"""Test a failed access token."""
5054
sf = SimpleFin(MOCK_ACCESS_URL)
51-
with pytest.raises(aiohttp.client_exceptions.ClientResponseError):
55+
with pytest.raises(SimpleFinAuthError):
5256
await sf.fetch_data()
5357

5458

0 commit comments

Comments
 (0)