pluto/handler/app.py

118 lines
3.1 KiB
Python
Raw Normal View History

2021-12-28 12:52:19 +00:00
#!/usr/bin/env python3
2021-12-23 16:59:35 +00:00
# Twisted/Klein imports
2021-12-23 14:46:51 +00:00
from twisted.logger import Logger
from twisted.internet import reactor
from klein import Klein
2021-12-23 16:59:35 +00:00
# Other library imports
2021-12-23 14:46:51 +00:00
from json import dumps, loads
from json.decoder import JSONDecodeError
from signal import signal, SIGINT
2021-12-23 14:46:51 +00:00
2021-12-23 16:59:35 +00:00
# Project imports
2021-12-24 17:27:36 +00:00
from settings import settings
import util
# Old style classes
2021-12-24 02:23:38 +00:00
from agora import Agora
from transactions import Transactions
2022-02-07 13:24:09 +00:00
from markets import Markets
from money import Money
2021-12-23 14:46:51 +00:00
# New style classes
2022-03-04 22:31:07 +00:00
import sinks
import ux
init_map = None
2022-03-04 21:05:52 +00:00
# TODO: extend this with more
2022-02-24 22:27:27 +00:00
def cleanup(sig, frame):
if init_map:
try:
init_map["tx"].lc_es_checks.stop()
init_map["agora"].lc_dash.stop()
init_map["agora"].lc_cheat.stop()
except: # noqa
pass # noqa
reactor.stop()
2022-02-24 22:27:27 +00:00
signal(SIGINT, cleanup) # Handle Ctrl-C and run the cleanup routine
2021-12-23 14:46:51 +00:00
2021-12-23 19:09:16 +00:00
def convert(data):
if isinstance(data, bytes):
return data.decode("ascii")
if isinstance(data, dict):
return dict(map(convert, data.items()))
if isinstance(data, tuple):
return map(convert, data)
return data
2021-12-23 16:59:35 +00:00
class WebApp(object):
"""
Our Klein webapp.
"""
2021-12-23 14:46:51 +00:00
2021-12-23 16:59:35 +00:00
app = Klein()
2021-12-23 14:46:51 +00:00
def __init__(self):
2021-12-23 16:59:35 +00:00
self.log = Logger("webapp")
2021-12-23 14:46:51 +00:00
2021-12-23 16:59:35 +00:00
@app.route("/callback", methods=["POST"])
def callback(self, request):
content = request.content.read()
try:
parsed = loads(content)
except JSONDecodeError:
self.log.error("Failed to parse JSON callback: {content}", content=content)
2021-12-24 17:27:36 +00:00
return dumps(False)
2021-12-23 19:09:16 +00:00
self.log.info("Callback received: {parsed}", parsed=parsed["data"]["id"])
# self.tx.transaction(parsed)
2021-12-24 17:27:36 +00:00
return dumps(True)
2021-12-23 14:46:51 +00:00
2022-03-04 21:05:52 +00:00
# set up another connection to a bank
@app.route("/signin", methods=["GET"])
def signin(self, request):
auth_url = self.truelayer.create_auth_url()
return f'Please sign in <a href="{auth_url}" target="_blank">here.</a>'
2022-03-04 21:05:52 +00:00
# endpoint called after we finish setting up a connection above
@app.route("/callback-truelayer", methods=["POST"])
def signin_callback(self, request):
code = request.args[b"code"]
self.truelayer.handle_authcode_received(code)
return dumps(True)
@app.route("/accounts", methods=["GET"])
def balance(self, request):
accounts = self.truelayer.get_accounts()
2022-03-04 21:05:52 +00:00
return dumps(accounts, indent=2)
2021-12-23 14:46:51 +00:00
2021-12-23 16:59:35 +00:00
if __name__ == "__main__":
2022-02-07 13:24:09 +00:00
init_map = {
"ux": ux.UX(),
2022-02-07 13:24:09 +00:00
"agora": Agora(),
"markets": Markets(),
2022-03-04 22:31:07 +00:00
"sinks": sinks.Sinks(),
2022-02-07 13:24:09 +00:00
"tx": Transactions(),
"webapp": WebApp(),
"money": Money(),
2022-02-07 13:24:09 +00:00
}
# Merge all classes into each other
util.xmerge_attrs(init_map)
# Let the classes know they have been merged
for class_name, class_instance in init_map.items():
if hasattr(class_instance, "__xmerged__"):
class_instance.__xmerged__()
# Set up the loops to put data in ES
init_map["tx"].setup_loops()
# Run the WebApp
init_map["webapp"].app.run(settings.App.BindHost, 8080)