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.internet import reactor
|
|
|
|
from klein import Klein
|
2022-05-02 17:20:14 +00:00
|
|
|
from twisted.internet.protocol import Factory
|
|
|
|
|
2021-12-23 14:46:51 +00:00
|
|
|
|
2021-12-23 16:59:35 +00:00
|
|
|
# Other library imports
|
2022-03-06 18:51:16 +00:00
|
|
|
from json import dumps
|
2022-02-18 08:41:17 +00:00
|
|
|
from signal import signal, SIGINT
|
2022-05-02 15:32:01 +00:00
|
|
|
from sys import argv
|
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
|
2022-02-28 19:58:46 +00:00
|
|
|
import util
|
2022-03-04 22:47:06 +00:00
|
|
|
|
|
|
|
# Old style classes
|
2021-12-23 18:46:39 +00:00
|
|
|
from transactions import Transactions
|
2022-02-07 13:24:09 +00:00
|
|
|
from markets import Markets
|
2022-02-22 20:06:56 +00:00
|
|
|
from money import Money
|
2021-12-23 14:46:51 +00:00
|
|
|
|
2022-03-04 22:47:06 +00:00
|
|
|
# New style classes
|
2022-03-04 22:31:07 +00:00
|
|
|
import sinks
|
2022-03-14 21:19:26 +00:00
|
|
|
import sources
|
2022-03-04 22:47:06 +00:00
|
|
|
import ux
|
2022-02-28 19:58:46 +00:00
|
|
|
|
2022-02-18 08:41:17 +00:00
|
|
|
init_map = None
|
2022-05-02 17:20:14 +00:00
|
|
|
Factory.noisy = False
|
2022-02-18 08:41:17 +00:00
|
|
|
|
|
|
|
|
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):
|
2022-02-18 08:41:17 +00:00
|
|
|
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
|
2022-02-18 08:41:17 +00:00
|
|
|
|
2021-12-23 14:46:51 +00:00
|
|
|
|
2022-03-05 21:52:31 +00:00
|
|
|
class WebApp(util.Base):
|
2021-12-23 16:59:35 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
2022-03-06 18:51:16 +00:00
|
|
|
# @app.route("/callback", methods=["POST"])
|
|
|
|
# def callback(self, request):
|
|
|
|
# content = request.content.read()
|
|
|
|
# try:
|
|
|
|
# parsed = loads(content)
|
|
|
|
# except JSONDecodeError:
|
|
|
|
# self.log.error(f"Failed to parse JSON callback: {content}")
|
|
|
|
# return dumps(False)
|
|
|
|
# self.log.info("Callback received: {parsed}", parsed=parsed["data"]["id"])
|
|
|
|
# # self.tx.transaction(parsed)
|
|
|
|
# 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
|
2022-03-06 18:51:16 +00:00
|
|
|
@app.route("/signin/<string:account>", methods=["GET"])
|
|
|
|
def signin(self, request, account):
|
|
|
|
auth_url = self.sinks.truelayer.create_auth_url(account)
|
|
|
|
return f'Please sign in to {account} <a href="{auth_url}" target="_blank">here.</a>'
|
2022-03-01 20:39:22 +00:00
|
|
|
|
2022-03-04 21:05:52 +00:00
|
|
|
# endpoint called after we finish setting up a connection above
|
2022-03-01 20:39:22 +00:00
|
|
|
@app.route("/callback-truelayer", methods=["POST"])
|
2022-03-22 21:56:35 +00:00
|
|
|
def signin_callback_truelayer(self, request):
|
2022-03-01 20:39:22 +00:00
|
|
|
code = request.args[b"code"]
|
2022-03-05 21:52:31 +00:00
|
|
|
self.sinks.truelayer.handle_authcode_received(code)
|
2022-03-01 20:39:22 +00:00
|
|
|
return dumps(True)
|
|
|
|
|
2022-03-22 21:56:35 +00:00
|
|
|
@app.route("/callback-nordigen", methods=["GET"])
|
|
|
|
def signin_callback_nordigen(self, request):
|
|
|
|
# code = request.args[b"code"]
|
|
|
|
# self.sinks.truelayer.handle_authcode_received(code)
|
|
|
|
return dumps(True)
|
|
|
|
|
2022-04-20 18:07:07 +00:00
|
|
|
@app.route("/callback-verify", methods=["POST"])
|
|
|
|
def callback_verify(self, request):
|
|
|
|
# code = request.args[b"code"]
|
|
|
|
rtrn = self.ux.verify.handle_callback(request)
|
|
|
|
return dumps(rtrn)
|
|
|
|
|
2022-03-06 18:51:16 +00:00
|
|
|
@app.route("/accounts/<string:account>", methods=["GET"])
|
|
|
|
def balance(self, request, account):
|
|
|
|
accounts = self.sinks.truelayer.get_accounts(account)
|
2022-03-04 21:05:52 +00:00
|
|
|
return dumps(accounts, indent=2)
|
2022-03-01 20:39:22 +00:00
|
|
|
|
2021-12-23 14:46:51 +00:00
|
|
|
|
2021-12-23 16:59:35 +00:00
|
|
|
if __name__ == "__main__":
|
2022-05-02 15:32:01 +00:00
|
|
|
if "--debug" in argv:
|
|
|
|
util.debug = True
|
2022-02-07 13:24:09 +00:00
|
|
|
init_map = {
|
2022-03-04 22:47:06 +00:00
|
|
|
"ux": ux.UX(),
|
2022-02-07 13:24:09 +00:00
|
|
|
"markets": Markets(),
|
2022-03-14 21:19:26 +00:00
|
|
|
"sources": sources.Sources(),
|
2022-03-04 22:31:07 +00:00
|
|
|
"sinks": sinks.Sinks(),
|
2022-02-07 13:24:09 +00:00
|
|
|
"tx": Transactions(),
|
|
|
|
"webapp": WebApp(),
|
2022-02-22 20:06:56 +00:00
|
|
|
"money": Money(),
|
2022-02-07 13:24:09 +00:00
|
|
|
}
|
2022-02-28 20:18:42 +00:00
|
|
|
# Merge all classes into each other
|
2022-02-28 19:58:46 +00:00
|
|
|
util.xmerge_attrs(init_map)
|
|
|
|
|
2022-03-04 22:47:06 +00:00
|
|
|
# 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__()
|
|
|
|
|
2022-02-18 08:41:17 +00:00
|
|
|
# Set up the loops to put data in ES
|
|
|
|
init_map["tx"].setup_loops()
|
2021-12-27 21:12:54 +00:00
|
|
|
|
|
|
|
# Run the WebApp
|
2022-02-28 19:58:46 +00:00
|
|
|
init_map["webapp"].app.run(settings.App.BindHost, 8080)
|