Skip to content

Commit

Permalink
fix: avoid throttling issues with Fio API
Browse files Browse the repository at this point in the history
We cannot do info and last requests in 30 seconds, so instead introduce
new API to fiobank module to return both info at once.

The customization can be removed once honzajavorek/fiobank#38 is released.

Fixes WEBSITE-1Q
  • Loading branch information
nijel committed Oct 25, 2024
1 parent 5b15446 commit c5fe284
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
45 changes: 42 additions & 3 deletions weblate_web/payments/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
from .utils import send_notification

if TYPE_CHECKING:
from collections.abc import Generator
from datetime import date, datetime

from django.http import HttpRequest, HttpResponseRedirect
from django_stubs_ext import StrOrPromise
from fakturace.invoices import Invoice
Expand Down Expand Up @@ -464,6 +467,41 @@ class ThePayBitcoin(ThePayCard):
thepay_method = 29


class FioBankAPI(fiobank.FioBank):
"""
Fio API wrapper.
Backported API from
https://github.com/honzajavorek/fiobank/pull/38.
TODO: Remove this wrapper once it is released.
"""

def _fetch_last(
self, from_id: str | None = None, from_date: str | date | datetime | None = None
) -> dict:
if from_id and from_date:
raise ValueError("Only one constraint is allowed.")

if from_id:
self._request("set-last-id", from_id=from_id)
elif from_date:
self._request("set-last-date", from_date=fiobank.coerce_date(from_date))

return self._request("last")

def last(
self, from_id: str | None = None, from_date: str | date | datetime | None = None
) -> Generator[dict]:
return self._parse_transactions(self._fetch_last(from_id, from_date))

def last_transactions(
self, from_id: str | None = None, from_date: str | date | datetime | None = None
) -> tuple[dict, Generator[dict]]:
data = self._fetch_last(from_id, from_date)
return (self._parse_info(data), self._parse_transactions(data))


@register_backend
class FioBank(LegacyBackend):
# TODO: migrate from legacy backend
Expand Down Expand Up @@ -519,10 +557,11 @@ def fetch_payments(cls, from_date: str | None = None) -> None:
else:
tokens = settings.FIO_TOKEN
for token in tokens:
client = fiobank.FioBank(token=token)
info = client.info()
# TODO: change to fiobank.FioBank(token=token, decimal=True) with fiobank 4.0
client = FioBankAPI(token=token)
info, transactions = client.last_transactions(from_date=from_date)
currency = info["currency"]
for entry in client.last(from_date=from_date):
for entry in transactions:
matches = []
# Extract from message
if entry["recipient_message"]:
Expand Down
10 changes: 0 additions & 10 deletions weblate_web/payments/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@


FIO_API = "https://fioapi.fio.cz/v1/rest/last/test-token/transactions.json"
FIO_INFO_API = (
"https://fioapi.fio.cz/v1/rest/periods/test-token/{0}/{0}/transactions.json"
)
FIO_TRASACTIONS = {
"accountStatement": {
"info": {
Expand Down Expand Up @@ -266,13 +263,6 @@ def test_proforma(self):
self.assertFalse(backend.complete(None))
self.check_payment(Payment.PENDING)
responses.add(responses.GET, FIO_API, body=json.dumps(FIO_TRASACTIONS))
responses.add(
responses.GET,
FIO_INFO_API.format(
date.today().isoformat() # noqa: DTZ011
),
body=json.dumps(FIO_TRASACTIONS),
)
FioBank.fetch_payments()
self.check_payment(Payment.PENDING)
self.assertEqual(len(mail.outbox), 1)
Expand Down
2 changes: 1 addition & 1 deletion weblate_web/test-data/fakturace

0 comments on commit c5fe284

Please sign in to comment.