diff --git a/lnbits/extensions/withdraw/crud.py b/lnbits/extensions/withdraw/crud.py index 14178a0c2..c97534e8f 100644 --- a/lnbits/extensions/withdraw/crud.py +++ b/lnbits/extensions/withdraw/crud.py @@ -3,18 +3,12 @@ from typing import List, Optional, Union from lnbits.helpers import urlsafe_short_hash from . import db -from .models import WithdrawLink, HashCheck +from .models import WithdrawLink, HashCheck, CreateWithdrawData async def create_withdraw_link( - *, + data: CreateWithdrawData, wallet_id: str, - title: str, - min_withdrawable: int, - max_withdrawable: int, - uses: int, - wait_time: int, - is_unique: bool, usescsv: str, ) -> WithdrawLink: link_id = urlsafe_short_hash() @@ -39,15 +33,15 @@ async def create_withdraw_link( ( link_id, wallet_id, - title, - min_withdrawable, - max_withdrawable, - uses, - wait_time, - int(is_unique), + data.title, + data.min_withdrawable, + data.max_withdrawable, + data.uses, + data.wait_time, + int(data.is_unique), urlsafe_short_hash(), urlsafe_short_hash(), - int(datetime.now().timestamp()) + wait_time, + int(datetime.now().timestamp()) + data.wait_time, usescsv, ), ) diff --git a/lnbits/extensions/withdraw/models.py b/lnbits/extensions/withdraw/models.py index aaa8b04e1..86e11c150 100644 --- a/lnbits/extensions/withdraw/models.py +++ b/lnbits/extensions/withdraw/models.py @@ -1,9 +1,18 @@ -from fastapi import Request +from starlette.requests import Request +from fastapi.param_functions import Query from lnurl import Lnurl, LnurlWithdrawResponse, encode as lnurl_encode # type: ignore from sqlite3 import Row from pydantic import BaseModel import shortuuid # type: ignore +class CreateWithdrawData(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 + class WithdrawLink(BaseModel): id: str @@ -32,7 +41,6 @@ class WithdrawLink(BaseModel): def is_spent(self) -> bool: return self.used >= self.uses - @property def lnurl(self, req: Request) -> Lnurl: if self.is_unique: usescssv = self.usescsv.split(",") diff --git a/lnbits/extensions/withdraw/static/js/index.js b/lnbits/extensions/withdraw/static/js/index.js index 2237d52be..f7d07f8ab 100644 --- a/lnbits/extensions/withdraw/static/js/index.js +++ b/lnbits/extensions/withdraw/static/js/index.js @@ -90,7 +90,7 @@ new Vue({ LNbits.api .request( 'GET', - '/withdraw/api/v1/links?all_wallets', + '/withdraw/api/v1/links?all_wallets=true', this.g.user.wallets[0].inkey ) .then(function (response) { diff --git a/lnbits/extensions/withdraw/templates/withdraw/_api_docs.html b/lnbits/extensions/withdraw/templates/withdraw/_api_docs.html index 484464baf..ca141c1da 100644 --- a/lnbits/extensions/withdraw/templates/withdraw/_api_docs.html +++ b/lnbits/extensions/withdraw/templates/withdraw/_api_docs.html @@ -23,7 +23,7 @@
Curl example
curl -X GET {{ request.url_root }}api/v1/links -H "X-Api-Key: {{ - g.user.wallets[0].inkey }}" + user.wallets[0].inkey }}" @@ -50,7 +50,7 @@
Curl example
curl -X GET {{ request.url_root }}api/v1/links/<withdraw_id> -H - "X-Api-Key: {{ g.user.wallets[0].inkey }}" + "X-Api-Key: {{ user.wallets[0].inkey }}" @@ -83,7 +83,7 @@ "max_withdrawable": <integer>, "uses": <integer>, "wait_time": <integer>, "is_unique": <boolean>}' -H "Content-type: application/json" -H "X-Api-Key: {{ - g.user.wallets[0].adminkey }}" + user.wallets[0].adminkey }}" @@ -119,7 +119,7 @@ "max_withdrawable": <integer>, "uses": <integer>, "wait_time": <integer>, "is_unique": <boolean>}' -H "Content-type: application/json" -H "X-Api-Key: {{ - g.user.wallets[0].adminkey }}" + user.wallets[0].adminkey }}" @@ -143,7 +143,7 @@
Curl example
curl -X DELETE {{ request.url_root }}api/v1/links/<withdraw_id> - -H "X-Api-Key: {{ g.user.wallets[0].adminkey }}" + -H "X-Api-Key: {{ user.wallets[0].adminkey }}" @@ -171,7 +171,7 @@ curl -X GET {{ request.url_root }}api/v1/links/<the_hash>/<lnurl_id> -H "X-Api-Key: {{ - g.user.wallets[0].inkey }}" + user.wallets[0].inkey }}" diff --git a/lnbits/extensions/withdraw/views_api.py b/lnbits/extensions/withdraw/views_api.py index 54aee937d..53150f46a 100644 --- a/lnbits/extensions/withdraw/views_api.py +++ b/lnbits/extensions/withdraw/views_api.py @@ -10,6 +10,7 @@ from starlette.responses import HTMLResponse, JSONResponse # type: ignore from lnbits.core.crud import get_user from lnbits.decorators import WalletTypeInfo, get_key_type +from .models import CreateWithdrawData # from fastapi import FastAPI, Query, Response @@ -27,7 +28,7 @@ from .crud import ( @withdraw_ext.get("/api/v1/links", status_code=HTTPStatus.OK) # @api_check_wallet_key("invoice") -async def api_links(wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)): +async def api_links(req: Request, wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)): wallet_ids = [wallet.wallet.id] if all_wallets: @@ -35,8 +36,8 @@ async def api_links(wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: try: return [ { - **link._asdict(), - **{"lnurl": link.lnurl}, + **link.dict(), + **{"lnurl": link.lnurl(req)}, } for link in await get_withdraw_links(wallet_ids) ] @@ -73,18 +74,18 @@ async def api_link_retrieve(link_id, wallet: WalletTypeInfo = Depends(get_key_ty return {**link, **{"lnurl": link.lnurl}} -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 +# 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 @withdraw_ext.post("/api/v1/links", status_code=HTTPStatus.CREATED) @withdraw_ext.put("/api/v1/links/{link_id}", status_code=HTTPStatus.OK) # @api_check_wallet_key("admin") -async def api_link_create_or_update(data: CreateData, link_id: str = None): +async def api_link_create_or_update(req: Request, data: CreateWithdrawData, link_id: str = None, wallet: WalletTypeInfo = Depends(get_key_type)): if data.max_withdrawable < data.min_withdrawable: raise HTTPException( detail="`max_withdrawable` needs to be at least `min_withdrawable`.", @@ -122,11 +123,11 @@ async def api_link_create_or_update(data: CreateData, link_id: str = None): link = await update_withdraw_link(link_id, **data, usescsv=usescsv, used=0) else: link = await create_withdraw_link( - wallet_id=wallet.wallet.id, **data, usescsv=usescsv + wallet_id=wallet.wallet.id, data=data, usescsv=usescsv ) # if link_id: # response.status_code = HTTPStatus.OK - return {**link, **{"lnurl": link.lnurl}} + return {**link.dict(), **{"lnurl": link.lnurl(req)}} @withdraw_ext.delete("/api/v1/links/{link_id}")