pluto/handler/app.py

57 lines
1.5 KiB
Python
Executable File

#!/usr/sbin/env python3
# Twisted/Klein imports
from twisted.logger import Logger
from twisted.internet import reactor
from twisted.internet.task import LoopingCall, deferLater
from klein import Klein
# Other library imports
from json import dumps, loads
from json.decoder import JSONDecodeError
# Project imports
from settings import settings
from revolut import Revolut
from transactions import Transactions
class WebApp(object):
"""
Our Klein webapp.
"""
app = Klein()
def __init__(self):
self.revolut = Revolut()
self.tx = Transactions()
self.log = Logger("webapp")
@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)
return dumps({"success": False})
self.log.info("Callback received: {parsed}", parsed=parsed)
self.tx.transaction(parsed)
return dumps({"success": True})
def start(handler):
"""
Schedule to refresh the API token once the reactor starts, and create LoopingCapp to refresh it periodically.
"""
deferLater(reactor, 1, handler.get_new_token)
deferLater(reactor, 4, handler.setup_webhook)
lc = LoopingCall(handler.get_new_token)
lc.start(settings.token_refresh_sec)
if __name__ == "__main__":
webapp = WebApp()
start(webapp.revolut)
webapp.app.run("127.0.0.1", 8080)