Implement redis and store transaction data

This commit is contained in:
Mark Veidemanis 2021-12-23 18:46:39 +00:00
parent 57e5616531
commit a584154c2f
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
4 changed files with 75 additions and 10 deletions

View File

@ -12,6 +12,7 @@ from json.decoder import JSONDecodeError
# Project imports # Project imports
from settings import settings from settings import settings
from revolut import Revolut from revolut import Revolut
from transactions import Transactions
class WebApp(object): class WebApp(object):
@ -23,13 +24,9 @@ class WebApp(object):
def __init__(self): def __init__(self):
self.revolut = Revolut() self.revolut = Revolut()
self.tx = Transactions()
self.log = Logger("webapp") self.log = Logger("webapp")
@app.route("/refresh", methods=["GET"])
def refresh(self, request):
rtrn = self.revolut.get_new_token()
return dumps({"success": rtrn})
@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()
@ -39,20 +36,21 @@ class WebApp(object):
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({"success": False})
self.log.info("Callback received: {parsed}", parsed=parsed) self.log.info("Callback received: {parsed}", parsed=parsed)
self.tx.transaction(parsed)
return dumps({"success": True}) return dumps({"success": True})
def start(webapp): def start(handler):
""" """
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.
""" """
deferLater(reactor, 1, webapp.revolut.get_new_token) deferLater(reactor, 1, handler.get_new_token)
deferLater(reactor, 4, webapp.revolut.setup_webhook) deferLater(reactor, 4, handler.setup_webhook)
lc = LoopingCall(webapp.revolut.get_new_token) lc = LoopingCall(handler.get_new_token)
lc.start(settings.token_refresh_sec) lc.start(settings.token_refresh_sec)
if __name__ == "__main__": if __name__ == "__main__":
webapp = WebApp() webapp = WebApp()
start(webapp) start(webapp.revolut)
webapp.app.run("127.0.0.1", 8080) webapp.app.run("127.0.0.1", 8080)

7
handler/db.py Normal file
View File

@ -0,0 +1,7 @@
# Other library imports
from redis import StrictRedis
# Project imports
from settings import settings
r = StrictRedis(unix_socket_path=settings.redis_socket, db=0)

View File

@ -4,6 +4,7 @@ pre_settings = {
"token_refresh_sec": 100, "token_refresh_sec": 100,
"api_base": "https://sandbox-b2b.revolut.com/api/1.0", "api_base": "https://sandbox-b2b.revolut.com/api/1.0",
"webhook_url": "https://callback-sandbox.pathogen.is/callback", "webhook_url": "https://callback-sandbox.pathogen.is/callback",
"redis_socket": "/var/run/redis/redis.sock",
"refresh_token": "", "refresh_token": "",
"access_token": "", "access_token": "",
"client_id": "", "client_id": "",

59
handler/transactions.py Normal file
View File

@ -0,0 +1,59 @@
# Twisted/Klein imports
from twisted.logger import Logger
# Other library imports
from json import dumps
# Project imports
from db import r
class Transactions(object):
"""
Handler class for incoming Revolut transactions.
"""
def __init__(self):
self.log = Logger("transactions")
def transaction(self, data):
"""
Store details of transaction.
"""
event = data["event"]
ts = data["timestamp"]
inside = data["data"]
txid = inside["id"]
txtype = inside["type"]
state = inside["state"]
reference = inside["reference"]
leg = inside["legs"][0]
account_type = leg["counterparty"]["account_type"]
account_id = leg["counterparty"]["account_id"]
amount = leg["amount"]
currency = leg["currency"]
description = leg["description"]
to_store = {
"event": event,
"ts": ts,
"txid": txid,
"txtype": txtype,
"state": state,
"reference": reference,
"account_type": account_type,
"account_id": account_id,
"amount": amount,
"currency": currency,
"description": description,
}
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