Strip out API parts and pass through objects smarter

This commit is contained in:
Mark Veidemanis 2021-12-27 21:12:54 +00:00
parent 83090ff6a5
commit b880e9763f
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 36 additions and 66 deletions

View File

@ -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/<string:reference>/<string:amount>")
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/<string:contact_id>")
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/<string:countrycode>/<string:currency>")
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)