From 67ed68ac5eec5a9c823047b1c79755becc3a0b44 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 1 Jun 2022 09:09:10 +0100 Subject: [PATCH] Make commands async and improve error handling --- handler/sinks/nordigen.py | 9 ++++++--- handler/ux/commands.py | 14 +++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/handler/sinks/nordigen.py b/handler/sinks/nordigen.py index 5d43086..46b71b2 100644 --- a/handler/sinks/nordigen.py +++ b/handler/sinks/nordigen.py @@ -316,6 +316,7 @@ class Nordigen(util.Base): existing_entry = loads(settings.Nordigen.Maps) self.banks = existing_entry + @inlineCallbacks def map_account(self, account_id): # TODO: inlineCallbacks? """ 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], "revolut": [account, ids, here]} """ - account_data = self.get_account(account_id) + account_data = yield self.get_account(account_id) currency = account_data["currency"] existing_entry = loads(settings.Nordigen.Maps) @@ -445,7 +446,7 @@ class Nordigen(util.Base): obj = AccountBalancesRoot.from_json(r.content) except ValidationError as err: self.log.error(f"Validation error: {err}") - return + return (False, False) parsed = obj.to_dict()["balances"] total = 0 currency = None @@ -453,7 +454,7 @@ class Nordigen(util.Base): if currency: if not currency == entry["balanceAmount"]["currency"]: self.log.error("Different currencies in balance query.") - return + return (False, False) if not entry["balanceType"] == "interimBooked": continue total += float(entry["balanceAmount"]["amount"]) @@ -471,6 +472,8 @@ class Nordigen(util.Base): currency, amount = self.get_balance(account_id) if not amount: continue + if not currency: + continue if currency in totals: totals[currency] += amount else: diff --git a/handler/ux/commands.py b/handler/ux/commands.py index 97294ed..21f4553 100644 --- a/handler/ux/commands.py +++ b/handler/ux/commands.py @@ -887,15 +887,19 @@ class IRCCommands(object): authed = True helptext = "Enable an account_id at a bank for use in Nordigen. Usage: nmapaccount " + @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 def run(cmd, spl, length, authed, msg, agora, tx, ux): if length == 2: account_id = spl[1] - account_name = tx.sinks.nordigen.map_account(account_id) - if not account_name: - msg(f"Failed to map the account") - return - msg(f"Mapped account ID {account_id} to {account_name}") + c = tx.sinks.nordigen.map_account(account_id) + c.addCallback(IRCCommands.nmapaccount.got_map_account, account_id, msg) class nunmapaccount(object): name = "nunmapaccount"