Add some API endpoints

This commit is contained in:
Mark Veidemanis 2021-12-24 17:27:36 +00:00
parent 9ba5721872
commit 7b59a28c4d
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 27 additions and 15 deletions

View File

@ -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/<string:reference>/<string:amount>")
def find(self, request, reference, amount):
@ -84,9 +92,9 @@ class WebApp(object):
rtrn = self.agora.get_ads()
return dumps(rtrn)
@app.route("/create/<string:countrycode>/<string:currency>/<int:price>")
def create(self, request, countrycode, currency, price):
rtrn = self.agora.create_ad(countrycode, currency, price)
@app.route("/create/<string:countrycode>/<string:currency>")
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)

View File

@ -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):
"""