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

108 lines
3.6 KiB
Python
Raw Normal View History

2021-10-06 14:49:00 +01:00
from http import HTTPStatus
2021-10-06 19:43:57 +01:00
from fastapi import Query
from fastapi.params import Depends
from pydantic import BaseModel
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import HTMLResponse, JSONResponse # type: ignore
2021-10-06 14:49:00 +01:00
from lnbits.core.crud import get_user, get_wallet
from lnbits.core.services import create_invoice, check_invoice_status
2021-10-06 19:43:57 +01:00
from lnbits.decorators import WalletTypeInfo, get_key_type
2021-10-06 14:49:00 +01:00
from . import tpos_ext
from .crud import create_tpos, get_tpos, get_tposs, delete_tpos
2021-10-06 19:43:57 +01:00
from .models import TPoS, CreateTposData
2021-10-06 14:49:00 +01:00
2021-10-06 19:43:57 +01:00
@tpos_ext.get("/api/v1/tposs", status_code=HTTPStatus.OK)
async def api_tposs(
2021-10-17 18:33:29 +01:00
all_wallets: bool = Query(None), wallet: WalletTypeInfo = Depends(get_key_type)
):
2021-10-06 19:43:57 +01:00
wallet_ids = [wallet.wallet.id]
if all_wallets:
2021-10-17 18:33:29 +01:00
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
2021-10-06 14:49:00 +01:00
2021-10-06 19:43:57 +01:00
return [tpos.dict() for tpos in await get_tposs(wallet_ids)]
2021-10-06 14:49:00 +01:00
2021-10-06 19:43:57 +01:00
@tpos_ext.post("/api/v1/tposs", status_code=HTTPStatus.CREATED)
2021-10-17 18:33:29 +01:00
async def api_tpos_create(
data: CreateTposData, wallet: WalletTypeInfo = Depends(get_key_type)
):
2021-10-06 19:43:57 +01:00
tpos = await create_tpos(wallet_id=wallet.wallet.id, data=data)
return tpos.dict()
2021-10-06 14:49:00 +01:00
2021-10-06 19:43:57 +01:00
@tpos_ext.delete("/api/v1/tposs/{tpos_id}")
async def api_tpos_delete(tpos_id: str, wallet: WalletTypeInfo = Depends(get_key_type)):
2021-10-06 14:49:00 +01:00
tpos = await get_tpos(tpos_id)
if not tpos:
2021-10-06 19:43:57 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="TPoS does not exist."
2021-10-06 19:43:57 +01:00
)
# return {"message": "TPoS does not exist."}, HTTPStatus.NOT_FOUND
2021-10-06 14:49:00 +01:00
2021-10-06 19:43:57 +01:00
if tpos.wallet != wallet.wallet.id:
2021-10-17 18:33:29 +01:00
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not your TPoS.")
2021-10-06 19:43:57 +01:00
# return {"message": "Not your TPoS."}, HTTPStatus.FORBIDDEN
2021-10-06 14:49:00 +01:00
await delete_tpos(tpos_id)
2021-10-06 19:43:57 +01:00
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
# return "", HTTPStatus.NO_CONTENT
2021-10-06 14:49:00 +01:00
2021-10-17 18:33:29 +01:00
2021-10-06 19:43:57 +01:00
@tpos_ext.post("/api/v1/tposs/{tpos_id}/invoices", status_code=HTTPStatus.CREATED)
async def api_tpos_create_invoice(amount: int = Query(..., ge=1), tpos_id: str = None):
2021-10-06 14:49:00 +01:00
tpos = await get_tpos(tpos_id)
if not tpos:
2021-10-06 19:43:57 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="TPoS does not exist."
2021-10-06 19:43:57 +01:00
)
# return {"message": "TPoS does not exist."}, HTTPStatus.NOT_FOUND
2021-10-06 14:49:00 +01:00
try:
payment_hash, payment_request = await create_invoice(
wallet_id=tpos.wallet,
2021-10-06 19:43:57 +01:00
amount=amount,
2021-10-06 14:49:00 +01:00
memo=f"{tpos.name}",
extra={"tag": "tpos"},
)
except Exception as e:
2021-10-17 18:33:29 +01:00
raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e))
2021-10-06 19:43:57 +01:00
# return {"message": str(e)}, HTTPStatus.INTERNAL_SERVER_ERROR
2021-10-08 11:03:55 +01:00
2021-10-06 19:43:57 +01:00
return {"payment_hash": payment_hash, "payment_request": payment_request}
2021-10-06 14:49:00 +01:00
2021-10-17 18:33:29 +01:00
@tpos_ext.get(
"/api/v1/tposs/{tpos_id}/invoices/{payment_hash}", status_code=HTTPStatus.OK
)
2021-10-06 19:43:57 +01:00
async def api_tpos_check_invoice(tpos_id: str, payment_hash: str):
2021-10-06 14:49:00 +01:00
tpos = await get_tpos(tpos_id)
if not tpos:
2021-10-06 19:43:57 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="TPoS does not exist."
2021-10-06 19:43:57 +01:00
)
# return {"message": "TPoS does not exist."}, HTTPStatus.NOT_FOUND
2021-10-06 14:49:00 +01:00
try:
status = await check_invoice_status(tpos.wallet, payment_hash)
is_paid = not status.pending
except Exception as exc:
print(exc)
2021-10-06 19:43:57 +01:00
return {"paid": False}
2021-10-06 14:49:00 +01:00
if is_paid:
wallet = await get_wallet(tpos.wallet)
payment = await wallet.get_payment(payment_hash)
await payment.set_pending(False)
2021-10-06 19:43:57 +01:00
return {"paid": True}
2021-10-06 14:49:00 +01:00
2021-10-06 19:43:57 +01:00
return {"paid": False}