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 14:46:51 +00:00
|
|
|
|
|
|
|
|
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()
|
|
|
|
self.log = Logger("webapp")
|
2021-12-23 14:46:51 +00:00
|
|
|
|
2021-12-23 16:59:35 +00:00
|
|
|
@app.route("/refresh", methods=["GET"])
|
|
|
|
def refresh(self, request):
|
|
|
|
rtrn = self.revolut.get_new_token()
|
|
|
|
return dumps({"success": rtrn})
|
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})
|
|
|
|
self.log.info("Callback received: {parsed}", parsed=parsed)
|
2021-12-23 14:46:51 +00:00
|
|
|
return dumps({"success": True})
|
|
|
|
|
|
|
|
|
2021-12-23 16:59:35 +00:00
|
|
|
def start(webapp):
|
|
|
|
"""
|
|
|
|
Schedule to refresh the API token once the reactor starts, and create LoopingCapp to refresh it periodically.
|
|
|
|
"""
|
|
|
|
deferLater(reactor, 1, webapp.revolut.get_new_token)
|
|
|
|
deferLater(reactor, 4, webapp.revolut.setup_webhook)
|
|
|
|
lc = LoopingCall(webapp.revolut.get_new_token)
|
|
|
|
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()
|
|
|
|
start(webapp)
|
|
|
|
webapp.app.run("127.0.0.1", 8080)
|