2021-12-23 16:59:35 +00:00
|
|
|
#!/usr/sbin/env python3
|
|
|
|
# Twisted/Klein imports
|
2021-12-23 14:46:51 +00:00
|
|
|
from twisted.logger import Logger
|
|
|
|
from twisted.internet import reactor
|
|
|
|
from twisted.internet.task import LoopingCall, deferLater
|
|
|
|
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
|
|
|
|
|
2021-12-23 16:59:35 +00:00
|
|
|
# Project imports
|
|
|
|
from settings import settings
|
|
|
|
from revolut import Revolut
|
2021-12-23 18:46:39 +00:00
|
|
|
from transactions import Transactions
|
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
|
|
|
|
2021-12-23 16:59:35 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.revolut = Revolut()
|
2021-12-23 18:46:39 +00:00
|
|
|
self.tx = Transactions()
|
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)
|
|
|
|
return dumps({"success": False})
|
2021-12-23 19:09:16 +00:00
|
|
|
self.log.info("Callback received: {parsed}", parsed=parsed["data"]["id"])
|
2021-12-23 18:46:39 +00:00
|
|
|
self.tx.transaction(parsed)
|
2021-12-23 14:46:51 +00:00
|
|
|
return dumps({"success": True})
|
|
|
|
|
2021-12-23 19:09:16 +00:00
|
|
|
@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))
|
|
|
|
|
2021-12-23 14:46:51 +00:00
|
|
|
|
2021-12-23 18:46:39 +00:00
|
|
|
def start(handler):
|
2021-12-23 16:59:35 +00:00
|
|
|
"""
|
|
|
|
Schedule to refresh the API token once the reactor starts, and create LoopingCapp to refresh it periodically.
|
|
|
|
"""
|
2021-12-23 18:46:39 +00:00
|
|
|
deferLater(reactor, 1, handler.get_new_token)
|
|
|
|
deferLater(reactor, 4, handler.setup_webhook)
|
|
|
|
lc = LoopingCall(handler.get_new_token)
|
2021-12-23 16:59:35 +00:00
|
|
|
lc.start(settings.token_refresh_sec)
|
2021-12-23 14:46:51 +00:00
|
|
|
|
|
|
|
|
2021-12-23 16:59:35 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
webapp = WebApp()
|
2021-12-23 18:46:39 +00:00
|
|
|
start(webapp.revolut)
|
2021-12-23 16:59:35 +00:00
|
|
|
webapp.app.run("127.0.0.1", 8080)
|