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 from json.decoder import JSONDecodeError
# Project imports # Project imports
from settings import settings
from revolut import Revolut from revolut import Revolut
from agora import Agora from agora import Agora
from transactions import Transactions from transactions import Transactions
from irc import bot
def convert(data): def convert(data):
@ -32,12 +34,18 @@ class WebApp(object):
app = Klein() app = Klein()
def __init__(self, agora): def __init__(self, agora, irc):
self.revolut = Revolut() self.revolut = Revolut(irc) # Initialise Revolut client and pass IRC
self.agora = agora self.agora = agora
self.tx = Transactions() self.tx = Transactions(agora, irc) # Initialise Transactions client and pass Agora and IRC
self.log = Logger("webapp") 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"]) @app.route("/callback", methods=["POST"])
def callback(self, request): def callback(self, request):
content = request.content.read() content = request.content.read()
@ -45,10 +53,10 @@ class WebApp(object):
parsed = loads(content) parsed = loads(content)
except JSONDecodeError: except JSONDecodeError:
self.log.error("Failed to parse JSON callback: {content}", content=content) 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.log.info("Callback received: {parsed}", parsed=parsed["data"]["id"])
self.tx.transaction(parsed) self.tx.transaction(parsed)
return dumps({"success": True}) return dumps(True)
@app.route("/find/<string:reference>/<string:amount>") @app.route("/find/<string:reference>/<string:amount>")
def find(self, request, reference, amount): def find(self, request, reference, amount):
@ -84,9 +92,9 @@ class WebApp(object):
rtrn = self.agora.get_ads() rtrn = self.agora.get_ads()
return dumps(rtrn) return dumps(rtrn)
@app.route("/create/<string:countrycode>/<string:currency>/<int:price>") @app.route("/create/<string:countrycode>/<string:currency>")
def create(self, request, countrycode, currency, price): def create(self, request, countrycode, currency):
rtrn = self.agora.create_ad(countrycode, currency, price) rtrn = self.agora.create_ad(countrycode, currency)
return dumps(rtrn) return dumps(rtrn)
@ -94,6 +102,9 @@ def start(handler, refresh_sec):
""" """
Schedule to refresh the API token once the reactor starts, and create LoopingCapp to refresh it periodically. 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) deferLater(reactor, 1, handler.get_new_token)
deferLater(reactor, 4, handler.setup_webhook) deferLater(reactor, 4, handler.setup_webhook)
lc = LoopingCall(handler.get_new_token) lc = LoopingCall(handler.get_new_token)
@ -101,7 +112,8 @@ def start(handler, refresh_sec):
if __name__ == "__main__": if __name__ == "__main__":
agora = Agora() irc = bot() # Initialise IRC client
webapp = WebApp(agora) agora = Agora(irc) # Initialise Agora client and pass IRC
# start(webapp.revolut, int(settings.Revolut.RefreshSec)) 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) webapp.app.run("127.0.0.1", 8080)

View File

@ -5,7 +5,6 @@ from twisted.logger import Logger
from json import dumps from json import dumps
# Project imports # Project imports
from agora import Agora
from db import r from db import r
@ -14,9 +13,10 @@ class Transactions(object):
Handler class for incoming Revolut transactions. Handler class for incoming Revolut transactions.
""" """
def __init__(self): def __init__(self, agora, irc):
self.log = Logger("transactions") self.log = Logger("transactions")
self.agora = Agora() self.agora = agora
self.irc = irc
def transaction(self, data): def transaction(self, data):
""" """