From fe643fc407af13e16ce3d1d1365cd4e5439d46d2 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 28 Feb 2022 19:58:46 +0000 Subject: [PATCH] Simplify and refactor initialisation code --- handler/app.py | 58 +++++++++++++++++++++++++++++++++---------------- handler/util.py | 9 ++++++++ 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/handler/app.py b/handler/app.py index 9e74247..645f183 100755 --- a/handler/app.py +++ b/handler/app.py @@ -12,6 +12,7 @@ from signal import signal, SIGINT # Project imports from settings import settings +import util from revolut import Revolut from agora import Agora from transactions import Transactions @@ -20,6 +21,11 @@ from notify import Notify from markets import Markets from money import Money +# from sinks.nordigen import Nordigen +# from sinks.yapily import Yapily +from sinks.truelayer import TrueLayer +from sinks.fidor import Fidor + init_map = None @@ -66,10 +72,22 @@ class WebApp(object): self.log.error("Failed to parse JSON callback: {content}", content=content) return dumps(False) self.log.info("Callback received: {parsed}", parsed=parsed["data"]["id"]) - self.tx.transaction(parsed) + # self.tx.transaction(parsed) return dumps(True) +def setup_call_loops(token_setting, function_init, function_continuous, delay, function_post_start=None): + if token_setting == "1": + deferLater(reactor, 1, function_init) + else: + deferLater(reactor, 1, function_continuous, True) + if function_post_start: + deferLater(reactor, 4, function_post_start) + + lc = LoopingCall(function_continuous) + lc.start(delay) + + if __name__ == "__main__": init_map = { "notify": Notify(), @@ -77,30 +95,32 @@ if __name__ == "__main__": "agora": Agora(), "markets": Markets(), "revolut": Revolut(), + # "nordigen": Nordigen(), + # "yapily": Yapily(), + "truelayer": TrueLayer(), + "fidor": Fidor(), "tx": Transactions(), "webapp": WebApp(), "money": Money(), } - for classname, object_instance in init_map.items(): - # notify, Notify - for classname_inside, object_instance_inside in init_map.items(): - if not classname == classname_inside: - # irc, bot - setattr(object_instance, classname_inside, object_instance_inside) + util.xmerge_attrs(init_map) + + setup_call_loops( + token_setting=settings.Revolut.SetupToken, + function_init=init_map["revolut"].setup_auth, + function_continuous=init_map["revolut"].get_new_token, + delay=int(settings.Revolut.RefreshSec), + function_post_start=init_map["revolut"].setup_webhook, + ) + setup_call_loops( + token_setting=settings.TrueLayer.SetupToken, + function_init=init_map["truelayer"].setup_auth, + function_continuous=init_map["truelayer"].get_new_token, + delay=int(settings.TrueLayer.RefreshSec), + ) - # Handle setting up JWT and request_token from an auth code - if settings.Revolut.SetupToken == "1": - deferLater(reactor, 1, init_map["revolut"].setup_auth) - else: - # Schedule refreshing the access token using the refresh token - deferLater(reactor, 1, init_map["revolut"].get_new_token, True) - # Check if the webhook is set up and set up if not - deferLater(reactor, 4, init_map["revolut"].setup_webhook) - # Schedule repeatedly refreshing the access token - lc = LoopingCall(init_map["revolut"].get_new_token) - lc.start(int(settings.Revolut.RefreshSec)) # Set up the loops to put data in ES init_map["tx"].setup_loops() # Run the WebApp - init_map["webapp"].app.run("127.0.0.1", 8080) + init_map["webapp"].app.run(settings.App.BindHost, 8080) diff --git a/handler/util.py b/handler/util.py index ed19ef1..f2a479b 100644 --- a/handler/util.py +++ b/handler/util.py @@ -8,6 +8,15 @@ from datetime import datetime log = Logger("util.global") +def xmerge_attrs(init_map): + for classname, object_instance in init_map.items(): + # notify, Notify + for classname_inside, object_instance_inside in init_map.items(): + if not classname == classname_inside: + # irc, bot + setattr(object_instance, classname_inside, object_instance_inside) + + def convert(data): """ Recursively convert a dictionary.