From 2dded1b58500c99f231aad94c627b9cb18c52f99 Mon Sep 17 00:00:00 2001 From: Fitti Date: Tue, 22 Jun 2021 09:55:58 +0200 Subject: [PATCH] Get basic working endpoints This is a setup that works specifically with a hardcoded test wallet --- lnbits/extensions/TwitchAlerts/crud.py | 9 +++ lnbits/extensions/TwitchAlerts/migrations.py | 20 ++--- lnbits/extensions/TwitchAlerts/views_api.py | 79 +++++++++++++------- 3 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 lnbits/extensions/TwitchAlerts/crud.py diff --git a/lnbits/extensions/TwitchAlerts/crud.py b/lnbits/extensions/TwitchAlerts/crud.py new file mode 100644 index 000000000..6027e557e --- /dev/null +++ b/lnbits/extensions/TwitchAlerts/crud.py @@ -0,0 +1,9 @@ +async def get_charge_details(service): + details = { + "user": "8ee4d2d2b788467ebbab172ed3d50aaa", + "description": "Testing", + "lnbitswallet": "e1e7c4f4f86a4905ab413f96fb78aabc", + "time": 1440, + "amount": 1 + } + return details diff --git a/lnbits/extensions/TwitchAlerts/migrations.py b/lnbits/extensions/TwitchAlerts/migrations.py index 2d107d196..db0a9c7cb 100644 --- a/lnbits/extensions/TwitchAlerts/migrations.py +++ b/lnbits/extensions/TwitchAlerts/migrations.py @@ -1,11 +1,11 @@ -# async def m001_initial(db): +async def m001_initial(db): -# await db.execute( -# """ -# CREATE TABLE IF NOT EXISTS TwitchAlerts ( -# id TEXT PRIMARY KEY, -# wallet TEXT NOT NULL, -# time TIMESTAMP NOT NULL DEFAULT (strftime('%s', 'now')) -# ); -# """ -# ) + await db.execute( + """ + CREATE TABLE IF NOT EXISTS TwitchAlerts ( + id TEXT PRIMARY KEY, + wallet TEXT NOT NULL, + time TIMESTAMP NOT NULL DEFAULT (strftime('%s', 'now')) + ); + """ + ) diff --git a/lnbits/extensions/TwitchAlerts/views_api.py b/lnbits/extensions/TwitchAlerts/views_api.py index 1641d5eb8..f4656d456 100644 --- a/lnbits/extensions/TwitchAlerts/views_api.py +++ b/lnbits/extensions/TwitchAlerts/views_api.py @@ -1,40 +1,65 @@ -# views_api.py is for you API endpoints that could be hit by another service - -# add your dependencies here - # import json # import httpx # (use httpx just like requests, except instead of response.ok there's only the # response.is_error that is its inverse) -from quart import jsonify +from quart import g, redirect, request # jsonify from http import HTTPStatus +from lnbits.decorators import api_validate_post_request, api_check_wallet_key + from . import TwitchAlerts_ext +from .crud import get_charge_details +from ..satspay.crud import create_charge, get_charge, delete_charge -# add your endpoints here +@TwitchAlerts_ext.route("/api/v1/createdonation", methods=["POST"]) +@api_check_wallet_key("invoice") +@api_validate_post_request( + schema={ + "name": {"type": "string"}, + "sats": {"type": "integer", "required": True}, + "service": {"type": "string", "required": True}, + "cur_code": {"type": "string", "required": True}, + "amount": {"type": "float", "required": True} + } +) +async def api_create_donation(): + """Takes data from donation form and creates+returns SatsPay charge""" + webhook_base = request.scheme + "://" + request.headers["Host"] + charge_details = await get_charge_details(g.data["service"]) + charge = await create_charge( + webhook=webhook_base + "/TwitchAlerts/api/v1/postdonation", + **charge_details) + return redirect(f"/satspay/{charge.id}") -@TwitchAlerts_ext.route("/api/v1/tools", methods=["GET"]) -async def api_TwitchAlerts(): - """Try to add descriptions for others.""" - tools = [ - { - "name": "Quart", - "url": "https://pgjones.gitlab.io/quart/", - "language": "Python", - }, - { - "name": "Vue.js", - "url": "https://vuejs.org/", - "language": "JavaScript", - }, - { - "name": "Quasar Framework", - "url": "https://quasar.dev/", - "language": "JavaScript", - }, - ] +@TwitchAlerts_ext.route("/api/v1/postdonation", methods=["POST"]) +# @api_validate_post_request( +# schema={ +# "id": {"type": "string", "required": True}, +# "description": {"type": "string", "allow_unknown": True}, +# "onchainaddress": {"type": "string", "allow_unknown": True}, +# "payment_request": {"type": "string", "allow_unknown": True}, +# "payment_hash": {"type": "string", "allow_unknown": True}, +# "time": {"type": "integer", "allow_unknown": True}, +# "amount": {"type": "integer", "allow_unknown": True}, +# "paid": {"type": "boolean", "allow_unknown": True}, +# "timestamp": {"type": "integer", "allow_unknown": True}, +# "completelink": {"type": "string", "allow_unknown": True}, +# } +# ) +async def api_post_donation(): + """Posts a paid donation to Stremalabs/StreamElements. - return jsonify(tools), HTTPStatus.OK + This endpoint acts as a webhook for the SatsPayServer extension.""" + data = await request.get_json(force=True) + charge_id = data["id"] + charge = await get_charge(charge_id) + print(charge) + if charge and charge.paid: + await delete_charge(charge_id) + print("This endpoint works!") + return "", HTTPStatus.OK + else: + return "", HTTPStatus.OK