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

161 lines
5.6 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-05 09:19:21 +01:00
from lnbits.decorators import WalletTypeInfo, get_key_type
2021-10-18 10:58:09 +01:00
from . import withdraw_ext
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
# from fastapi import FastAPI, Query, Response
2021-09-29 11:04:37 +01:00
@withdraw_ext.get("/api/v1/links", status_code=HTTPStatus.OK)
# @api_check_wallet_key("invoice")
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.",
)
# response.status_code = HTTPStatus.UPGRADE_REQUIRED
# return { "message": "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)
# @api_check_wallet_key("invoice")
async def api_link_retrieve(link_id, 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
)
# response.status_code = HTTPStatus.NOT_FOUND
# return {"message": "Withdraw link does not exist."}
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
)
# response.status_code = HTTPStatus.FORBIDDEN
# return {"message": "Not your withdraw link."}
2021-10-08 17:10:25 +01:00
return {**link, **{"lnurl": link.lnurl(request)}}
2021-10-17 18:33:29 +01:00
2021-10-06 14:40:12 +01:00
# class CreateData(BaseModel):
# title: str = Query(...)
# min_withdrawable: int = Query(..., ge=1)
# max_withdrawable: int = Query(..., ge=1)
# uses: int = Query(..., ge=1)
# wait_time: int = Query(..., ge=1)
# is_unique: bool
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-05 09:19:21 +01:00
# @api_check_wallet_key("admin")
2021-10-17 18:33:29 +01:00
async def api_link_create_or_update(
req: Request,
data: CreateWithdrawData,
link_id: str = None,
wallet: WalletTypeInfo = Depends(get_key_type),
):
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
)
# response.status_code = HTTPStatus.BAD_REQUEST
# return {
# "message": "`max_withdrawable` needs to be at least `min_withdrawable`."
# }
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
)
# response.status_code = HTTPStatus.NOT_FOUND
# return {"message": "Withdraw link does not exist."}
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
)
# response.status_code = HTTPStatus.FORBIDDEN
# return {"message": "Not your withdraw link."}
2021-10-08 17:10:25 +01:00
link = await update_withdraw_link(link_id, data=data, 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-09-29 11:04:37 +01:00
# if link_id:
# response.status_code = HTTPStatus.OK
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}")
# @api_check_wallet_key("admin")
async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(get_key_type)):
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
)
# response.status_code = HTTPStatus.NOT_FOUND
# return {"message": "Withdraw link does not exist."}
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
)
# response.status_code = HTTPStatus.FORBIDDEN
# return {"message": "Not your withdraw link."}
await delete_withdraw_link(link_id)
2021-09-29 11:04:37 +01:00
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
# return ""
2021-08-25 15:44:18 +01:00
@withdraw_ext.get("/api/v1/links/{the_hash}/{lnurl_id}", status_code=HTTPStatus.OK)
2021-09-29 11:04:37 +01:00
# @api_check_wallet_key("invoice")
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