Simplify and refactor initialisation code

This commit is contained in:
Mark Veidemanis 2022-02-28 19:58:46 +00:00
parent f7603f5a13
commit c09bad9cc8
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
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)

View File

@ -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.