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

208 lines
6.7 KiB
Python
Raw Normal View History

2021-09-28 21:18:15 +02:00
from lnbits.extensions.lnticket.models import CreateFormData, CreateTicketData
import re
from http import HTTPStatus
2021-09-28 21:18:15 +02:00
from typing import List
2021-09-28 21:18:15 +02:00
from fastapi import Query
2021-09-27 15:52:08 +01:00
from fastapi.params import Depends
2021-08-22 11:31:04 +01:00
from pydantic import BaseModel
2021-09-27 15:52:08 +01:00
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import HTMLResponse, JSONResponse # type: ignore
2021-08-22 11:31:04 +01:00
from lnbits.core.crud import get_user, get_wallet
from lnbits.core.services import create_invoice, check_invoice_status
2021-09-28 21:18:15 +02:00
from lnbits.decorators import WalletTypeInfo, get_key_type
from . import lnticket_ext
from .crud import (
create_ticket,
set_ticket_paid,
get_ticket,
get_tickets,
delete_ticket,
create_form,
update_form,
get_form,
get_forms,
delete_form,
)
# FORMS
2021-09-28 21:18:15 +02:00
@lnticket_ext.get("/api/v1/forms")
2021-10-17 18:33:29 +01:00
async def api_forms_get(
r: Request,
all_wallets: bool = Query(False),
wallet: WalletTypeInfo = Depends(get_key_type),
):
2021-09-27 15:52:08 +01:00
wallet_ids = [wallet.wallet.id]
2021-09-28 21:18:15 +02:00
if all_wallets:
2021-09-27 15:52:08 +01:00
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
return [form.dict() for form in await get_forms(wallet_ids)]
2021-10-17 18:33:29 +01:00
2021-09-27 15:52:08 +01:00
@lnticket_ext.post("/api/v1/forms", status_code=HTTPStatus.CREATED)
2021-08-22 11:31:04 +01:00
@lnticket_ext.put("/api/v1/forms/{form_id}")
2021-09-27 15:52:08 +01:00
# @api_check_wallet_key("invoice")
2021-08-22 11:31:04 +01:00
# @api_validate_post_request(
# schema={
# "wallet": {"type": "string", "empty": False, "required": True},
# "name": {"type": "string", "empty": False, "required": True},
# "webhook": {"type": "string", "required": False},
# "description": {"type": "string", "min": 0, "required": True},
# "amount": {"type": "integer", "min": 0, "required": True},
# "flatrate": {"type": "integer", "required": True},
# }
# )
2021-10-17 18:33:29 +01:00
async def api_form_create(
data: CreateFormData, form_id=None, wallet: WalletTypeInfo = Depends(get_key_type)
):
if form_id:
form = await get_form(form_id)
if not form:
2021-09-27 15:52:08 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail=f"Form does not exist."
2021-09-27 15:52:08 +01:00
)
# return {"message": "Form does not exist."}, HTTPStatus.NOT_FOUND
if form.wallet != wallet.wallet.id:
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.FORBIDDEN, detail=f"Not your form."
2021-09-27 15:52:08 +01:00
)
# return {"message": "Not your form."}, HTTPStatus.FORBIDDEN
2021-08-22 11:31:04 +01:00
form = await update_form(form_id, **data)
else:
2021-09-28 21:18:15 +02:00
form = await create_form(data, wallet.wallet)
return form.dict()
2021-08-22 11:31:04 +01:00
@lnticket_ext.delete("/api/v1/forms/{form_id}")
2021-09-27 15:52:08 +01:00
# @api_check_wallet_key("invoice")
async def api_form_delete(form_id, wallet: WalletTypeInfo = Depends(get_key_type)):
form = await get_form(form_id)
if not form:
2021-09-27 15:52:08 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail=f"Form does not exist."
2021-09-27 15:52:08 +01:00
)
# return {"message": "Form does not exist."}, HTTPStatus.NOT_FOUND
2021-09-27 15:52:08 +01:00
if form.wallet != wallet.wallet.id:
2021-10-17 18:33:29 +01:00
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail=f"Not your form.")
2021-09-27 15:52:08 +01:00
# return {"message": "Not your form."}, HTTPStatus.FORBIDDEN
await delete_form(form_id)
2021-09-27 15:52:08 +01:00
# return "", HTTPStatus.NO_CONTENT
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
#########tickets##########
2021-09-28 21:18:15 +02:00
@lnticket_ext.get("/api/v1/tickets")
2021-09-27 15:52:08 +01:00
# @api_check_wallet_key("invoice")
2021-10-17 18:33:29 +01:00
async def api_tickets(
all_wallets: bool = Query(False), wallet: WalletTypeInfo = Depends(get_key_type)
):
2021-09-27 15:52:08 +01:00
wallet_ids = [wallet.wallet.id]
2021-08-22 11:31:04 +01:00
if all_wallets:
2021-09-27 15:52:08 +01:00
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
return [form.dict() for form in await get_tickets(wallet_ids)]
2021-10-17 18:33:29 +01:00
2021-09-27 15:52:08 +01:00
@lnticket_ext.post("/api/v1/tickets/{form_id}", status_code=HTTPStatus.CREATED)
2021-08-22 11:31:04 +01:00
# @api_validate_post_request(
# schema={
# "form": {"type": "string", "empty": False, "required": True},
# "name": {"type": "string", "empty": False, "required": True},
# "email": {"type": "string", "empty": True, "required": True},
# "ltext": {"type": "string", "empty": False, "required": True},
# "sats": {"type": "integer", "min": 0, "required": True},
# }
# )
async def api_ticket_make_ticket(data: CreateTicketData, form_id):
form = await get_form(form_id)
if not form:
2021-09-27 15:52:08 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail=f"LNTicket does not exist."
2021-09-27 15:52:08 +01:00
)
# return {"message": "LNTicket does not exist."}, HTTPStatus.NOT_FOUND
2021-09-29 20:57:35 +02:00
nwords = len(re.split(r"\s+", data.ltext))
try:
payment_hash, payment_request = await create_invoice(
wallet_id=form.wallet,
2021-09-29 20:57:35 +02:00
amount=data.sats,
memo=f"ticket with {nwords} words on {form_id}",
extra={"tag": "lnticket"},
)
except Exception as e:
2021-10-17 18:33:29 +01:00
raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e))
2021-09-27 15:52:08 +01:00
# return {"message": str(e)}, HTTPStatus.INTERNAL_SERVER_ERROR
ticket = await create_ticket(
2021-09-29 20:57:35 +02:00
payment_hash=payment_hash, wallet=form.wallet, data=data
)
if not ticket:
2021-09-27 15:52:08 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="LNTicket could not be fetched."
)
2021-09-27 15:52:08 +01:00
# return (
# {"message": "LNTicket could not be fetched."},
# HTTPStatus.NOT_FOUND,
# )
2021-10-17 18:33:29 +01:00
return {"payment_hash": payment_hash, "payment_request": payment_request}
2021-09-27 15:52:08 +01:00
@lnticket_ext.get("/api/v1/tickets/{payment_hash}", status_code=HTTPStatus.OK)
async def api_ticket_send_ticket(payment_hash):
ticket = await get_ticket(payment_hash)
try:
status = await check_invoice_status(ticket.wallet, payment_hash)
is_paid = not status.pending
except Exception:
2021-09-27 15:52:08 +01:00
return {"paid": False}
if is_paid:
wallet = await get_wallet(ticket.wallet)
payment = await wallet.get_payment(payment_hash)
await payment.set_pending(False)
ticket = await set_ticket_paid(payment_hash=payment_hash)
2021-09-27 15:52:08 +01:00
return {"paid": True}
2021-09-27 15:52:08 +01:00
return {"paid": False}
2021-08-22 11:31:04 +01:00
@lnticket_ext.delete("/api/v1/tickets/{ticket_id}")
2021-09-27 15:52:08 +01:00
# @api_check_wallet_key("invoice")
async def api_ticket_delete(ticket_id, wallet: WalletTypeInfo = Depends(get_key_type)):
ticket = await get_ticket(ticket_id)
if not ticket:
2021-09-27 15:52:08 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail=f"LNTicket does not exist."
2021-09-27 15:52:08 +01:00
)
# return {"message": "Paywall does not exist."}, HTTPStatus.NOT_FOUND
2021-09-27 15:52:08 +01:00
if ticket.wallet != wallet.wallet.id:
2021-10-17 18:33:29 +01:00
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not your ticket.")
2021-09-27 15:52:08 +01:00
# return {"message": "Not your ticket."}, HTTPStatus.FORBIDDEN
await delete_ticket(ticket_id)
2021-09-27 15:52:08 +01:00
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
# return ""