Simplify and refactor initialisation code

This commit is contained in:
Mark Veidemanis 2022-02-28 19:58:46 +00:00
parent f1cb463682
commit fe643fc407
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 48 additions and 19 deletions

View File

@ -12,6 +12,7 @@ from signal import signal, SIGINT
# Project imports # Project imports
from settings import settings from settings import settings
import util
from revolut import Revolut from revolut import Revolut
from agora import Agora from agora import Agora
from transactions import Transactions from transactions import Transactions
@ -20,6 +21,11 @@ from notify import Notify
from markets import Markets from markets import Markets
from money import Money 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 init_map = None
@ -66,10 +72,22 @@ class WebApp(object):
self.log.error("Failed to parse JSON callback: {content}", content=content) self.log.error("Failed to parse JSON callback: {content}", content=content)
return dumps(False) return dumps(False)
self.log.info("Callback received: {parsed}", parsed=parsed["data"]["id"]) self.log.info("Callback received: {parsed}", parsed=parsed["data"]["id"])
self.tx.transaction(parsed) # self.tx.transaction(parsed)
return dumps(True) 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__": if __name__ == "__main__":
init_map = { init_map = {
"notify": Notify(), "notify": Notify(),
@ -77,30 +95,32 @@ if __name__ == "__main__":
"agora": Agora(), "agora": Agora(),
"markets": Markets(), "markets": Markets(),
"revolut": Revolut(), "revolut": Revolut(),
# "nordigen": Nordigen(),
# "yapily": Yapily(),
"truelayer": TrueLayer(),
"fidor": Fidor(),
"tx": Transactions(), "tx": Transactions(),
"webapp": WebApp(), "webapp": WebApp(),
"money": Money(), "money": Money(),
} }
for classname, object_instance in init_map.items(): util.xmerge_attrs(init_map)
# notify, Notify
for classname_inside, object_instance_inside in init_map.items(): setup_call_loops(
if not classname == classname_inside: token_setting=settings.Revolut.SetupToken,
# irc, bot function_init=init_map["revolut"].setup_auth,
setattr(object_instance, classname_inside, object_instance_inside) 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 # Set up the loops to put data in ES
init_map["tx"].setup_loops() init_map["tx"].setup_loops()
# Run the WebApp # Run the WebApp
init_map["webapp"].app.run("127.0.0.1", 8080) init_map["webapp"].app.run(settings.App.BindHost, 8080)

View File

@ -8,6 +8,15 @@ from datetime import datetime
log = Logger("util.global") 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): def convert(data):
""" """
Recursively convert a dictionary. Recursively convert a dictionary.