You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.3 KiB
Python

#!/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
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
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["data"]["id"])
self.tx.transaction(parsed)
return dumps({"success": True})
@app.route("/find/<string:reference>/<string:amount>")
def find(self, request, reference, amount):
try:
int(amount)
except ValueError:
return dumps({"success": False, "msg": "Amount is not an integer"})
rtrn = self.tx.find_tx(reference, amount)
if rtrn == "AMOUNT_INVALID":
return dumps({"success": False, "msg": "Reference found but amount invalid"})
elif not rtrn:
return dumps({"success": False, "msg": "Reference not found"})
else:
return dumps(convert(rtrn))
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)