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

132 lines
4.1 KiB
Python
Raw Normal View History

from http import HTTPStatus
2021-10-18 10:58:09 +01:00
from fastapi.param_functions import Query
from fastapi.params import Depends
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl # type: ignore
2021-09-29 11:04:37 +01:00
from starlette.exceptions import HTTPException
from starlette.requests import Request
from lnbits.core.crud import get_user
2021-10-20 12:03:11 +01:00
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
2021-10-18 10:58:09 +01:00
from . import withdraw_ext
2021-10-20 12:03:11 +01:00
from .crud import (
create_withdraw_link,
delete_withdraw_link,
get_hash_check,
get_withdraw_link,
get_withdraw_links,
update_withdraw_link,
)
2021-10-06 14:40:12 +01:00
from .models import CreateWithdrawData
2021-09-29 11:04:37 +01:00
2021-09-29 11:04:37 +01:00
@withdraw_ext.get("/api/v1/links", status_code=HTTPStatus.OK)
2021-10-17 18:33:29 +01:00
async def api_links(
req: Request,
wallet: WalletTypeInfo = Depends(get_key_type),
all_wallets: bool = Query(False),
):
2021-09-29 11:04:37 +01:00
wallet_ids = [wallet.wallet.id]
2021-09-29 11:04:37 +01:00
if all_wallets:
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
2021-10-08 17:10:25 +01:00
try:
2021-08-25 15:44:18 +01:00
return [
2021-10-17 18:33:29 +01:00
{**link.dict(), **{"lnurl": link.lnurl(req)}}
for link in await get_withdraw_links(wallet_ids)
]
2021-09-29 11:04:37 +01:00
except LnurlInvalidUrl:
2021-09-29 11:04:37 +01:00
raise HTTPException(
status_code=HTTPStatus.UPGRADE_REQUIRED,
detail="LNURLs need to be delivered over a publically accessible `https` domain or Tor.",
)
2021-09-29 11:04:37 +01:00
@withdraw_ext.get("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
2021-11-12 04:14:55 +00:00
async def api_link_retrieve(
link_id, request: Request, wallet: WalletTypeInfo = Depends(get_key_type)
):
link = await get_withdraw_link(link_id, 0)
if not link:
2021-09-29 11:04:37 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
detail="Withdraw link does not exist.", status_code=HTTPStatus.NOT_FOUND
2021-09-29 11:04:37 +01:00
)
2021-09-29 11:04:37 +01:00
if link.wallet != wallet.wallet.id:
raise HTTPException(
2021-10-17 18:33:29 +01:00
detail="Not your withdraw link.", status_code=HTTPStatus.FORBIDDEN
2021-09-29 11:04:37 +01:00
)
2021-10-08 17:10:25 +01:00
return {**link, **{"lnurl": link.lnurl(request)}}
2021-10-17 18:33:29 +01:00
2021-08-25 15:44:18 +01:00
@withdraw_ext.post("/api/v1/links", status_code=HTTPStatus.CREATED)
2021-09-29 11:04:37 +01:00
@withdraw_ext.put("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
2021-10-17 18:33:29 +01:00
async def api_link_create_or_update(
req: Request,
data: CreateWithdrawData,
link_id: str = None,
2021-10-20 12:03:11 +01:00
wallet: WalletTypeInfo = Depends(require_admin_key),
2021-10-17 18:33:29 +01:00
):
2021-08-20 16:35:24 +01:00
if data.max_withdrawable < data.min_withdrawable:
2021-09-29 11:04:37 +01:00
raise HTTPException(
detail="`max_withdrawable` needs to be at least `min_withdrawable`.",
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.BAD_REQUEST,
2021-09-29 11:04:37 +01:00
)
usescsv = ""
2021-08-20 16:35:24 +01:00
for i in range(data.uses):
if data.is_unique:
usescsv += "," + str(i + 1)
else:
usescsv += "," + str(1)
usescsv = usescsv[1:]
if link_id:
link = await get_withdraw_link(link_id, 0)
if not link:
2021-09-29 11:04:37 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
detail="Withdraw link does not exist.", status_code=HTTPStatus.NOT_FOUND
2021-09-29 11:04:37 +01:00
)
if link.wallet != wallet.wallet.id:
raise HTTPException(
2021-10-17 18:33:29 +01:00
detail="Not your withdraw link.", status_code=HTTPStatus.FORBIDDEN
2021-09-29 11:04:37 +01:00
)
2021-11-12 04:14:55 +00:00
link = await update_withdraw_link(
link_id, **data.dict(), usescsv=usescsv, used=0
)
else:
link = await create_withdraw_link(
2021-10-06 14:40:12 +01:00
wallet_id=wallet.wallet.id, data=data, usescsv=usescsv
)
2021-10-06 14:40:12 +01:00
return {**link.dict(), **{"lnurl": link.lnurl(req)}}
2021-09-29 11:04:37 +01:00
@withdraw_ext.delete("/api/v1/links/{link_id}")
2021-10-20 12:03:11 +01:00
async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(require_admin_key)):
link = await get_withdraw_link(link_id)
if not link:
2021-09-29 11:04:37 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
detail="Withdraw link does not exist.", status_code=HTTPStatus.NOT_FOUND
2021-09-29 11:04:37 +01:00
)
2021-09-29 11:04:37 +01:00
if link.wallet != wallet.wallet.id:
raise HTTPException(
2021-10-17 18:33:29 +01:00
detail="Not your withdraw link.", status_code=HTTPStatus.FORBIDDEN
2021-09-29 11:04:37 +01:00
)
await delete_withdraw_link(link_id)
2021-09-29 11:04:37 +01:00
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
2021-08-25 15:44:18 +01:00
@withdraw_ext.get("/api/v1/links/{the_hash}/{lnurl_id}", status_code=HTTPStatus.OK)
2021-10-17 18:33:29 +01:00
async def api_hash_retrieve(
the_hash, lnurl_id, wallet: WalletTypeInfo = Depends(get_key_type)
):
hashCheck = await get_hash_check(the_hash, lnurl_id)
2021-08-25 15:44:18 +01:00
return hashCheck