lnbits-legend/lnbits/extensions/twitchalerts/views_api.py

110 lines
3.7 KiB
Python
Raw Normal View History

from quart import g, redirect, request, jsonify
2021-06-20 06:34:01 +02:00
from http import HTTPStatus
from lnbits.decorators import api_validate_post_request, api_check_wallet_key
2021-06-22 15:46:46 +02:00
from . import twitchalerts_ext
2021-06-22 13:19:45 +02:00
from .crud import (
get_charge_details,
create_donation,
post_donation,
create_service,
authenticate_service
)
from ..satspay.crud import create_charge, get_charge
2021-06-22 15:46:46 +02:00
@twitchalerts_ext.route("/api/v1/createservice", methods=["POST"])
2021-06-22 13:19:45 +02:00
@api_check_wallet_key("invoice")
@api_validate_post_request(
schema={
"twitchuser": {"type": "string", "required": True},
"client_id": {"type": "string", "required": True},
"client_secret": {"type": "string", "required": True},
"wallet": {"type": "string", "required": True},
"servicename": {"type": "string", "required": True},
"onchain": {"type": "string"}
}
)
async def api_create_service():
"""Create a service, which holds data about how/where to post donations"""
service = await create_service(**g.data)
redirect_url = request.scheme + "://" + request.headers["Host"]
2021-06-22 15:46:46 +02:00
redirect_url += f"/twitchalerts/?created={str(service.id)}"
2021-06-22 13:19:45 +02:00
return redirect(redirect_url)
2021-06-22 15:46:46 +02:00
@twitchalerts_ext.route("/api/v1/authenticate/<service_id>", methods=["GET"])
2021-06-22 13:19:45 +02:00
async def api_authenticate_service(service_id):
code = request.args.get('code')
redirect_uri = request.scheme + "://" + request.headers["Host"]
2021-06-22 15:46:46 +02:00
redirect_uri += f"/twitchalerts/api/v1/authenticate/{service_id}"
2021-06-22 13:19:45 +02:00
url = await authenticate_service(service_id, code, redirect_uri)
return redirect(url)
2021-06-22 15:46:46 +02:00
@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},
2021-06-22 13:19:45 +02:00
"service": {"type": "integer", "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"])
2021-06-22 13:19:45 +02:00
name = g.data.get("name", "Anonymous")
charge = await create_charge(
2021-06-22 13:19:45 +02:00
amount=g.data["sats"],
completelink="https://twitch.tv/Fitti",
completelinktext="Back to Stream!",
2021-06-22 15:46:46 +02:00
webhook=webhook_base + "/twitchalerts/api/v1/postdonation",
**charge_details)
2021-06-22 13:19:45 +02:00
await create_donation(
id=charge.id,
name=name,
cur_code=g.data["cur_code"],
sats=g.data["sats"],
amount=g.data["amount"],
service=g.data["service"],
)
return redirect(f"/satspay/{charge.id}")
2021-06-20 06:34:01 +02:00
2021-06-22 15:46:46 +02:00
@twitchalerts_ext.route("/api/v1/postdonation", methods=["POST"])
2021-06-22 17:06:56 +02:00
@api_validate_post_request(
schema={
"id": {"type": "string", "required": True},
}
)
async def api_post_donation():
"""Posts a paid donation to Stremalabs/StreamElements.
2021-06-20 06:34:01 +02:00
This endpoint acts as a webhook for the SatsPayServer extension."""
data = await request.get_json(force=True)
2021-06-22 13:19:45 +02:00
donation_id = data.get("id", "No ID")
charge = await get_charge(donation_id)
print(charge)
if charge and charge.paid:
print("This endpoint works!")
2021-06-22 13:19:45 +02:00
if await post_donation(donation_id):
return (
jsonify({"message": "Posted!"}),
HTTPStatus.OK
)
2021-06-22 13:19:45 +02:00
else:
return (
jsonify({"message": "Already posted!"}),
HTTPStatus.BAD_REQUEST
)
else:
return (
jsonify({"message": "Not a paid charge!"}),
HTTPStatus.BAD_REQUEST
)