fixup!
This commit is contained in:
parent
2095f86618
commit
134016312f
9 changed files with 384 additions and 428 deletions
40
views_api.py
40
views_api.py
|
|
@ -4,7 +4,8 @@ from typing import Optional
|
|||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||
from lnbits.core.crud import get_user
|
||||
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
|
||||
from lnbits.core.models import WalletTypeInfo
|
||||
from lnbits.decorators import require_admin_key, require_invoice_key
|
||||
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl
|
||||
|
||||
from .crud import (
|
||||
|
|
@ -23,15 +24,15 @@ withdraw_ext_api = APIRouter(prefix="/api/v1")
|
|||
@withdraw_ext_api.get("/links", status_code=HTTPStatus.OK)
|
||||
async def api_links(
|
||||
req: Request,
|
||||
wallet: WalletTypeInfo = Depends(get_key_type),
|
||||
key_info: WalletTypeInfo = Depends(require_invoice_key),
|
||||
all_wallets: bool = Query(False),
|
||||
offset: int = Query(0),
|
||||
limit: int = Query(0),
|
||||
):
|
||||
wallet_ids = [wallet.wallet.id]
|
||||
wallet_ids = [key_info.wallet.id]
|
||||
|
||||
if all_wallets:
|
||||
user = await get_user(wallet.wallet.user)
|
||||
user = await get_user(key_info.wallet.user)
|
||||
wallet_ids = user.wallet_ids if user else []
|
||||
|
||||
try:
|
||||
|
|
@ -53,7 +54,9 @@ async def api_links(
|
|||
|
||||
@withdraw_ext_api.get("/links/{link_id}", status_code=HTTPStatus.OK)
|
||||
async def api_link_retrieve(
|
||||
link_id: str, request: Request, wallet: WalletTypeInfo = Depends(get_key_type)
|
||||
link_id: str,
|
||||
request: Request,
|
||||
key_info: WalletTypeInfo = Depends(require_invoice_key),
|
||||
):
|
||||
link = await get_withdraw_link(link_id, 0)
|
||||
|
||||
|
|
@ -62,7 +65,7 @@ async def api_link_retrieve(
|
|||
detail="Withdraw link does not exist.", status_code=HTTPStatus.NOT_FOUND
|
||||
)
|
||||
|
||||
if link.wallet != wallet.wallet.id:
|
||||
if link.wallet != key_info.wallet.id:
|
||||
raise HTTPException(
|
||||
detail="Not your withdraw link.", status_code=HTTPStatus.FORBIDDEN
|
||||
)
|
||||
|
|
@ -75,7 +78,7 @@ async def api_link_create_or_update(
|
|||
req: Request,
|
||||
data: CreateWithdrawData,
|
||||
link_id: Optional[str] = None,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
key_info: WalletTypeInfo = Depends(require_admin_key),
|
||||
):
|
||||
if data.uses > 250:
|
||||
raise HTTPException(detail="250 uses max.", status_code=HTTPStatus.BAD_REQUEST)
|
||||
|
|
@ -115,12 +118,11 @@ async def api_link_create_or_update(
|
|||
raise HTTPException(
|
||||
detail="Withdraw link does not exist.", status_code=HTTPStatus.NOT_FOUND
|
||||
)
|
||||
if link.wallet != wallet.wallet.id:
|
||||
if link.wallet != key_info.wallet.id:
|
||||
raise HTTPException(
|
||||
detail="Not your withdraw link.", status_code=HTTPStatus.FORBIDDEN
|
||||
)
|
||||
|
||||
data_dict = data.dict()
|
||||
if link.uses > data.uses:
|
||||
if data.uses - link.used <= 0:
|
||||
raise HTTPException(
|
||||
|
|
@ -128,33 +130,31 @@ async def api_link_create_or_update(
|
|||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
)
|
||||
numbers = link.usescsv.split(",")
|
||||
usescsv = ",".join(numbers[: data.uses - link.used])
|
||||
data_dict["usescsv"] = usescsv
|
||||
link.usescsv = ",".join(numbers[: data.uses - link.used])
|
||||
|
||||
if link.uses < data.uses:
|
||||
numbers = link.usescsv.split(",")
|
||||
|
||||
if numbers[-1] == "":
|
||||
current_number = int(link.uses)
|
||||
numbers[-1] = str(link.uses)
|
||||
else:
|
||||
current_number = int(numbers[-1])
|
||||
|
||||
while len(numbers) < (data.uses - link.used):
|
||||
current_number += 1
|
||||
numbers.append(str(current_number))
|
||||
usescsv = ",".join(numbers)
|
||||
data_dict["usescsv"] = usescsv
|
||||
link.usescsv = ",".join(numbers)
|
||||
|
||||
link = await update_withdraw_link(link_id, **data_dict)
|
||||
link = await update_withdraw_link(link)
|
||||
else:
|
||||
link = await create_withdraw_link(wallet_id=wallet.wallet.id, data=data)
|
||||
link = await create_withdraw_link(wallet_id=key_info.wallet.id, data=data)
|
||||
assert link
|
||||
return {**link.dict(), **{"lnurl": link.lnurl(req)}}
|
||||
|
||||
|
||||
@withdraw_ext_api.delete("/links/{link_id}", status_code=HTTPStatus.OK)
|
||||
async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(require_admin_key)):
|
||||
async def api_link_delete(
|
||||
link_id: str, key_info: WalletTypeInfo = Depends(require_admin_key)
|
||||
):
|
||||
link = await get_withdraw_link(link_id)
|
||||
|
||||
if not link:
|
||||
|
|
@ -162,7 +162,7 @@ async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(require_admi
|
|||
detail="Withdraw link does not exist.", status_code=HTTPStatus.NOT_FOUND
|
||||
)
|
||||
|
||||
if link.wallet != wallet.wallet.id:
|
||||
if link.wallet != key_info.wallet.id:
|
||||
raise HTTPException(
|
||||
detail="Not your withdraw link.", status_code=HTTPStatus.FORBIDDEN
|
||||
)
|
||||
|
|
@ -174,7 +174,7 @@ async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(require_admi
|
|||
@withdraw_ext_api.get(
|
||||
"/links/{the_hash}/{lnurl_id}",
|
||||
status_code=HTTPStatus.OK,
|
||||
dependencies=[Depends(get_key_type)],
|
||||
dependencies=[Depends(require_invoice_key)],
|
||||
)
|
||||
async def api_hash_retrieve(the_hash, lnurl_id):
|
||||
hash_check = await get_hash_check(the_hash, lnurl_id)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue