Implement looking up transactions

This commit is contained in:
Mark Veidemanis 2021-12-23 19:09:16 +00:00
parent a584154c2f
commit 04c20b6b69
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 38 additions and 3 deletions

View File

@ -15,6 +15,16 @@ from revolut import Revolut
from transactions import Transactions
def convert(data):
if isinstance(data, bytes):
return data.decode("ascii")
if isinstance(data, dict):
return dict(map(convert, data.items()))
if isinstance(data, tuple):
return map(convert, data)
return data
class WebApp(object):
"""
Our Klein webapp.
@ -35,10 +45,24 @@ class WebApp(object):
except JSONDecodeError:
self.log.error("Failed to parse JSON callback: {content}", content=content)
return dumps({"success": False})
self.log.info("Callback received: {parsed}", parsed=parsed)
self.log.info("Callback received: {parsed}", parsed=parsed["data"]["id"])
self.tx.transaction(parsed)
return dumps({"success": 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))
def start(handler):
"""

View File

@ -55,5 +55,16 @@ class Transactions(object):
self.log.info("Transaction processed: {formatted}", formatted=dumps(to_store, indent=2))
r.hmset(f"tx.{txid}", to_store)
def find_tx(self, amount, reference):
pass
def find_tx(self, reference, amount):
all_transactions = r.scan(0, match="tx.*")
print(f"ALL {all_transactions}")
for tx_iter in all_transactions[1]:
tx_obj = r.hgetall(tx_iter)
print(f"ITER {tx_iter}")
print(f"OBJ {tx_obj}")
if tx_obj[b"reference"] == str.encode(reference):
if tx_obj[b"amount"] == str.encode(amount):
return tx_obj
else:
return "AMOUNT_INVALID"
return False