diff --git a/src/fiobank/__init__.py b/src/fiobank/__init__.py new file mode 100644 index 0000000..6cdb2f8 --- /dev/null +++ b/src/fiobank/__init__.py @@ -0,0 +1,5 @@ +from .exceptions import ThrottlingError +from .fiobank import FioBank + + +__all__ = ("FioBank", "ThrottlingError") diff --git a/src/fiobank/exceptions.py b/src/fiobank/exceptions.py new file mode 100644 index 0000000..ad4092a --- /dev/null +++ b/src/fiobank/exceptions.py @@ -0,0 +1,8 @@ +from __future__ import annotations + + +class ThrottlingError(Exception): + """Throttling error raised when the API is being used too fast.""" + + def __str__(self) -> str: + return "Token can be used only once per 30s" diff --git a/fiobank.py b/src/fiobank/fiobank.py similarity index 89% rename from fiobank.py rename to src/fiobank/fiobank.py index eb2bf5e..63f9f1f 100644 --- a/fiobank.py +++ b/src/fiobank/fiobank.py @@ -5,7 +5,6 @@ from collections.abc import Generator from datetime import date, datetime from decimal import Decimal -from typing import Any, Callable import requests from tenacity import ( @@ -15,39 +14,8 @@ wait_random_exponential, ) - -__all__ = ("FioBank", "ThrottlingError") - - -def coerce_amount(value: int | float) -> Decimal: - if isinstance(value, int): - return Decimal(value) - if isinstance(value, float): - return Decimal(str(value)) - raise ValueError(value) - - -def coerce_date(value: datetime | date | str) -> date: - if isinstance(value, datetime): - return value.date() - if isinstance(value, date): - return value - return datetime.strptime(value[:10], "%Y-%m-%d").date() - - -def sanitize_value(value: Any, convert: Callable | None = None) -> Any: - if isinstance(value, str): - value = value.strip() or None - if convert and value is not None: - return convert(value) - return value - - -class ThrottlingError(Exception): - """Throttling error raised when the API is being used too fast.""" - - def __str__(self) -> str: - return "Token can be used only once per 30s" +from .exceptions import ThrottlingError +from .utils import coerce_date, sanitize_value class FioBank: diff --git a/src/fiobank/py.typed b/src/fiobank/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/src/fiobank/utils.py b/src/fiobank/utils.py new file mode 100644 index 0000000..2910cf7 --- /dev/null +++ b/src/fiobank/utils.py @@ -0,0 +1,29 @@ +from __future__ import annotations + +from datetime import date, datetime +from decimal import Decimal +from typing import Any, Callable + + +def coerce_amount(value: int | float) -> Decimal: + if isinstance(value, int): + return Decimal(value) + if isinstance(value, float): + return Decimal(str(value)) + raise ValueError(value) + + +def coerce_date(value: datetime | date | str) -> date: + if isinstance(value, datetime): + return value.date() + if isinstance(value, date): + return value + return datetime.strptime(value[:10], "%Y-%m-%d").date() + + +def sanitize_value(value: Any, convert: Callable | None = None) -> Any: + if isinstance(value, str): + value = value.strip() or None + if convert and value is not None: + return convert(value) + return value diff --git a/tests/test_coerce_date.py b/tests/test_coerce_date.py index 70cb4a3..d02a613 100644 --- a/tests/test_coerce_date.py +++ b/tests/test_coerce_date.py @@ -2,7 +2,7 @@ import pytest -from fiobank import coerce_date +from fiobank.utils import coerce_date @pytest.mark.parametrize( diff --git a/tests/test_sanitize_value.py b/tests/test_sanitize_value.py index fe47f36..d157074 100644 --- a/tests/test_sanitize_value.py +++ b/tests/test_sanitize_value.py @@ -2,7 +2,7 @@ import pytest -from fiobank import sanitize_value +from fiobank.utils import sanitize_value @pytest.mark.parametrize(