From 7b59a28c4d36d063d312bffabca400eba3d173bb Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 24 Dec 2021 17:27:36 +0000 Subject: [PATCH] Add some API endpoints --- handler/app.py | 36 ++++++++++++++++++++++++------------ handler/transactions.py | 6 +++--- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/handler/app.py b/handler/app.py index d98776a..250d96e 100755 --- a/handler/app.py +++ b/handler/app.py @@ -10,9 +10,11 @@ from json import dumps, loads from json.decoder import JSONDecodeError # Project imports +from settings import settings from revolut import Revolut from agora import Agora from transactions import Transactions +from irc import bot def convert(data): @@ -32,12 +34,18 @@ class WebApp(object): app = Klein() - def __init__(self, agora): - self.revolut = Revolut() + def __init__(self, agora, irc): + self.revolut = Revolut(irc) # Initialise Revolut client and pass IRC self.agora = agora - self.tx = Transactions() + self.tx = Transactions(agora, irc) # Initialise Transactions client and pass Agora and IRC 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() @@ -45,10 +53,10 @@ class WebApp(object): parsed = loads(content) except JSONDecodeError: self.log.error("Failed to parse JSON callback: {content}", content=content) - return dumps({"success": False}) + return dumps(False) self.log.info("Callback received: {parsed}", parsed=parsed["data"]["id"]) self.tx.transaction(parsed) - return dumps({"success": True}) + return dumps(True) @app.route("/find//") def find(self, request, reference, amount): @@ -84,9 +92,9 @@ class WebApp(object): rtrn = self.agora.get_ads() return dumps(rtrn) - @app.route("/create///") - def create(self, request, countrycode, currency, price): - rtrn = self.agora.create_ad(countrycode, currency, price) + @app.route("/create//") + def create(self, request, countrycode, currency): + rtrn = self.agora.create_ad(countrycode, currency) return dumps(rtrn) @@ -94,14 +102,18 @@ def start(handler, refresh_sec): """ Schedule to refresh the API token once the reactor starts, and create LoopingCapp to refresh it periodically. """ - deferLater(reactor, 1, handler.get_new_token) + if settings.Revolut.SetupToken == "1": + deferLater(reactor, 1, handler.setup_auth) + else: + deferLater(reactor, 1, handler.get_new_token) deferLater(reactor, 4, handler.setup_webhook) lc = LoopingCall(handler.get_new_token) lc.start(refresh_sec) if __name__ == "__main__": - agora = Agora() - webapp = WebApp(agora) - # start(webapp.revolut, int(settings.Revolut.RefreshSec)) + irc = bot() # Initialise IRC client + agora = Agora(irc) # Initialise Agora client and pass IRC + webapp = WebApp(agora, irc) # Initialise API and pass agora and IRC + start(webapp.revolut, int(settings.Revolut.RefreshSec)) webapp.app.run("127.0.0.1", 8080) diff --git a/handler/transactions.py b/handler/transactions.py index 892bced..d528b0e 100644 --- a/handler/transactions.py +++ b/handler/transactions.py @@ -5,7 +5,6 @@ from twisted.logger import Logger from json import dumps # Project imports -from agora import Agora from db import r @@ -14,9 +13,10 @@ class Transactions(object): Handler class for incoming Revolut transactions. """ - def __init__(self): + def __init__(self, agora, irc): self.log = Logger("transactions") - self.agora = Agora() + self.agora = agora + self.irc = irc def transaction(self, data): """