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

129 lines
4 KiB
Python
Raw Normal View History

from http import HTTPStatus
2021-10-18 10:58:09 +01:00
2023-01-04 18:27:39 +01:00
from fastapi import Depends, HTTPException, Query, Request
2023-01-04 16:29:47 +01:00
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl
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:
2023-01-04 16:29:47 +01:00
user = await get_user(wallet.wallet.user)
wallet_ids = user.wallet_ids if user else []
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(
2023-01-04 16:29:47 +01:00
link_id: str, request: Request, wallet: WalletTypeInfo = Depends(get_key_type)
2021-11-12 04:14:55 +00:00
):
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
)
2022-07-05 20:40:05 +01:00
return {**link.dict(), **{"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,
2023-01-04 18:27:39 +01:00
link_id: str = Query(None),
2021-10-20 12:03:11 +01:00
wallet: WalletTypeInfo = Depends(require_admin_key),
2021-10-17 18:33:29 +01:00
):
2022-05-17 22:02:54 +01:00
if data.uses > 250:
2022-06-01 14:53:05 +02:00
raise HTTPException(detail="250 uses max.", status_code=HTTPStatus.BAD_REQUEST)
if data.min_withdrawable < 1:
raise HTTPException(
detail="Min must be more than 1.", status_code=HTTPStatus.BAD_REQUEST
)
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
)
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
)
2023-01-04 16:29:47 +01:00
link = await update_withdraw_link(link_id, **data.dict())
else:
2023-01-04 16:29:47 +01:00
link = await create_withdraw_link(wallet_id=wallet.wallet.id, data=data)
assert link
2021-10-06 14:40:12 +01:00
return {**link.dict(), **{"lnurl": link.lnurl(req)}}
@withdraw_ext.delete("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
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)
return {"success": True}
2023-01-04 16:29:47 +01:00
@withdraw_ext.get(
"/api/v1/links/{the_hash}/{lnurl_id}",
status_code=HTTPStatus.OK,
dependencies=[Depends(get_key_type)],
)
async def api_hash_retrieve(the_hash, lnurl_id):
hashCheck = await get_hash_check(the_hash, lnurl_id)
2021-08-25 15:44:18 +01:00
return hashCheck