From b880e9763f33d9c2c5910001dd4962ec442f1892 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 27 Dec 2021 21:12:54 +0000 Subject: [PATCH] Strip out API parts and pass through objects smarter --- handler/app.py | 102 +++++++++++++++++-------------------------------- 1 file changed, 36 insertions(+), 66 deletions(-) diff --git a/handler/app.py b/handler/app.py index 3330ec7..0109909 100755 --- a/handler/app.py +++ b/handler/app.py @@ -34,18 +34,9 @@ class WebApp(object): app = Klein() - def __init__(self, agora, irc): - self.revolut = Revolut(irc) # Initialise Revolut client and pass IRC - self.agora = agora - self.tx = Transactions(agora, irc) # Initialise Transactions client and pass Agora and IRC + def __init__(self): self.log = Logger("webapp") - @app.route("/newkey") - def newkey(self, request): - self.revolut.create_new_jwt() - self.revolut.get_access_token() - return dumps(True) - @app.route("/callback", methods=["POST"]) def callback(self, request): content = request.content.read() @@ -58,63 +49,42 @@ class WebApp(object): self.tx.transaction(parsed) return dumps(True) - @app.route("/find//") - 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)) - - @app.route("/trades") - def trades(self, request): - trade_list = self.agora.dashboard() - return dumps(trade_list) - - @app.route("/messages/") - def messages(self, request, contact_id): - message_list = self.agora.get_messages(contact_id) - return dumps(message_list) - - @app.route("/dist") - def dist_countries(self, request): - rtrn = self.agora.dist_countries() - return dumps(rtrn) - - @app.route("/ads") - def ads(self, request): - rtrn = self.agora.get_ads() - return dumps(rtrn) - - @app.route("/create//") - def create(self, request, countrycode, currency): - rtrn = self.agora.create_ad(countrycode, currency) - return dumps(rtrn) - - -def start(handler, refresh_sec): - """ - Schedule to refresh the API token once the reactor starts, and create LoopingCapp to refresh it periodically. - """ - if settings.Revolut.SetupToken == "1": - deferLater(reactor, 1, handler.setup_auth) - else: - deferLater(reactor, 1, handler.get_new_token, True) - deferLater(reactor, 4, handler.setup_webhook) - lc = LoopingCall(handler.get_new_token) - lc.start(refresh_sec) - if __name__ == "__main__": - irc = bot() # Initialise IRC client - agora = Agora(irc) # Initialise Agora client and pass IRC + # Define IRC and Agora + irc = bot() + agora = Agora() + + # Pass IRC to Agora and Agora to IRC + # This is to prevent recursive dependencies + agora.set_irc(irc) irc.set_agora(agora) - webapp = WebApp(agora, irc) # Initialise API and pass agora and IRC - start(webapp.revolut, int(settings.Revolut.RefreshSec)) + + # Define Revolut + revolut = Revolut() + # Pass IRC to Revolut + revolut.set_irc(irc) + + # Define Transactions + tx = Transactions() + + # Pass Agora and IRC to Transactions + tx.set_agora(agora) + tx.set_irc(irc) + + # Define WebApp + webapp = WebApp() + # Handle setting up JWT and request_token from an auth code + if settings.Revolut.SetupToken == "1": + deferLater(reactor, 1, revolut.setup_auth) + else: + # Schedule refreshing the access token using the refresh token + deferLater(reactor, 1, revolut.get_new_token, True) + # Check if the webhook is set up and set up if not + deferLater(reactor, 4, revolut.setup_webhook) + # Schedule repeatedly refreshing the access token + lc = LoopingCall(revolut.get_new_token) + lc.start(int(settings.Revolut.RefreshSec)) + + # Run the WebApp webapp.app.run("127.0.0.1", 8080)