Make commands async and improve error handling

This commit is contained in:
Mark Veidemanis 2022-06-01 09:09:10 +01:00
parent 6aec0c1078
commit 67ed68ac5e
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 15 additions and 8 deletions

View File

@ -316,6 +316,7 @@ class Nordigen(util.Base):
existing_entry = loads(settings.Nordigen.Maps) existing_entry = loads(settings.Nordigen.Maps)
self.banks = existing_entry self.banks = existing_entry
@inlineCallbacks
def map_account(self, account_id): # TODO: inlineCallbacks? def map_account(self, account_id): # TODO: inlineCallbacks?
""" """
Map an account_id at a bank to an account_name. Map an account_id at a bank to an account_name.
@ -323,7 +324,7 @@ class Nordigen(util.Base):
Data type: {"monzo": [account, ids, here], Data type: {"monzo": [account, ids, here],
"revolut": [account, ids, here]} "revolut": [account, ids, here]}
""" """
account_data = self.get_account(account_id) account_data = yield self.get_account(account_id)
currency = account_data["currency"] currency = account_data["currency"]
existing_entry = loads(settings.Nordigen.Maps) existing_entry = loads(settings.Nordigen.Maps)
@ -445,7 +446,7 @@ class Nordigen(util.Base):
obj = AccountBalancesRoot.from_json(r.content) obj = AccountBalancesRoot.from_json(r.content)
except ValidationError as err: except ValidationError as err:
self.log.error(f"Validation error: {err}") self.log.error(f"Validation error: {err}")
return return (False, False)
parsed = obj.to_dict()["balances"] parsed = obj.to_dict()["balances"]
total = 0 total = 0
currency = None currency = None
@ -453,7 +454,7 @@ class Nordigen(util.Base):
if currency: if currency:
if not currency == entry["balanceAmount"]["currency"]: if not currency == entry["balanceAmount"]["currency"]:
self.log.error("Different currencies in balance query.") self.log.error("Different currencies in balance query.")
return return (False, False)
if not entry["balanceType"] == "interimBooked": if not entry["balanceType"] == "interimBooked":
continue continue
total += float(entry["balanceAmount"]["amount"]) total += float(entry["balanceAmount"]["amount"])
@ -471,6 +472,8 @@ class Nordigen(util.Base):
currency, amount = self.get_balance(account_id) currency, amount = self.get_balance(account_id)
if not amount: if not amount:
continue continue
if not currency:
continue
if currency in totals: if currency in totals:
totals[currency] += amount totals[currency] += amount
else: else:

View File

@ -887,15 +887,19 @@ class IRCCommands(object):
authed = True authed = True
helptext = "Enable an account_id at a bank for use in Nordigen. Usage: nmapaccount <account_id>" helptext = "Enable an account_id at a bank for use in Nordigen. Usage: nmapaccount <account_id>"
@staticmethod
def got_map_account(account_name, account_id, msg):
if not account_name:
msg(f"Failed to map the account")
return
msg(f"Mapped account ID {account_id} to {account_name}")
@staticmethod @staticmethod
def run(cmd, spl, length, authed, msg, agora, tx, ux): def run(cmd, spl, length, authed, msg, agora, tx, ux):
if length == 2: if length == 2:
account_id = spl[1] account_id = spl[1]
account_name = tx.sinks.nordigen.map_account(account_id) c = tx.sinks.nordigen.map_account(account_id)
if not account_name: c.addCallback(IRCCommands.nmapaccount.got_map_account, account_id, msg)
msg(f"Failed to map the account")
return
msg(f"Mapped account ID {account_id} to {account_name}")
class nunmapaccount(object): class nunmapaccount(object):
name = "nunmapaccount" name = "nunmapaccount"