mirror of
https://github.com/lnbits/lnbits.git
synced 2025-10-03 18:04:36 +02:00
url_for issues
This commit is contained in:
@@ -3,18 +3,12 @@ from typing import List, Optional, Union
|
|||||||
from lnbits.helpers import urlsafe_short_hash
|
from lnbits.helpers import urlsafe_short_hash
|
||||||
|
|
||||||
from . import db
|
from . import db
|
||||||
from .models import WithdrawLink, HashCheck
|
from .models import WithdrawLink, HashCheck, CreateWithdrawData
|
||||||
|
|
||||||
|
|
||||||
async def create_withdraw_link(
|
async def create_withdraw_link(
|
||||||
*,
|
data: CreateWithdrawData,
|
||||||
wallet_id: str,
|
wallet_id: str,
|
||||||
title: str,
|
|
||||||
min_withdrawable: int,
|
|
||||||
max_withdrawable: int,
|
|
||||||
uses: int,
|
|
||||||
wait_time: int,
|
|
||||||
is_unique: bool,
|
|
||||||
usescsv: str,
|
usescsv: str,
|
||||||
) -> WithdrawLink:
|
) -> WithdrawLink:
|
||||||
link_id = urlsafe_short_hash()
|
link_id = urlsafe_short_hash()
|
||||||
@@ -39,15 +33,15 @@ async def create_withdraw_link(
|
|||||||
(
|
(
|
||||||
link_id,
|
link_id,
|
||||||
wallet_id,
|
wallet_id,
|
||||||
title,
|
data.title,
|
||||||
min_withdrawable,
|
data.min_withdrawable,
|
||||||
max_withdrawable,
|
data.max_withdrawable,
|
||||||
uses,
|
data.uses,
|
||||||
wait_time,
|
data.wait_time,
|
||||||
int(is_unique),
|
int(data.is_unique),
|
||||||
urlsafe_short_hash(),
|
urlsafe_short_hash(),
|
||||||
urlsafe_short_hash(),
|
urlsafe_short_hash(),
|
||||||
int(datetime.now().timestamp()) + wait_time,
|
int(datetime.now().timestamp()) + data.wait_time,
|
||||||
usescsv,
|
usescsv,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@@ -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 lnurl import Lnurl, LnurlWithdrawResponse, encode as lnurl_encode # type: ignore
|
||||||
from sqlite3 import Row
|
from sqlite3 import Row
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
import shortuuid # type: ignore
|
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):
|
class WithdrawLink(BaseModel):
|
||||||
id: str
|
id: str
|
||||||
@@ -32,7 +41,6 @@ class WithdrawLink(BaseModel):
|
|||||||
def is_spent(self) -> bool:
|
def is_spent(self) -> bool:
|
||||||
return self.used >= self.uses
|
return self.used >= self.uses
|
||||||
|
|
||||||
@property
|
|
||||||
def lnurl(self, req: Request) -> Lnurl:
|
def lnurl(self, req: Request) -> Lnurl:
|
||||||
if self.is_unique:
|
if self.is_unique:
|
||||||
usescssv = self.usescsv.split(",")
|
usescssv = self.usescsv.split(",")
|
||||||
|
@@ -90,7 +90,7 @@ new Vue({
|
|||||||
LNbits.api
|
LNbits.api
|
||||||
.request(
|
.request(
|
||||||
'GET',
|
'GET',
|
||||||
'/withdraw/api/v1/links?all_wallets',
|
'/withdraw/api/v1/links?all_wallets=true',
|
||||||
this.g.user.wallets[0].inkey
|
this.g.user.wallets[0].inkey
|
||||||
)
|
)
|
||||||
.then(function (response) {
|
.then(function (response) {
|
||||||
|
@@ -23,7 +23,7 @@
|
|||||||
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
|
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
|
||||||
<code
|
<code
|
||||||
>curl -X GET {{ request.url_root }}api/v1/links -H "X-Api-Key: {{
|
>curl -X GET {{ request.url_root }}api/v1/links -H "X-Api-Key: {{
|
||||||
g.user.wallets[0].inkey }}"
|
user.wallets[0].inkey }}"
|
||||||
</code>
|
</code>
|
||||||
</q-card-section>
|
</q-card-section>
|
||||||
</q-card>
|
</q-card>
|
||||||
@@ -50,7 +50,7 @@
|
|||||||
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
|
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
|
||||||
<code
|
<code
|
||||||
>curl -X GET {{ request.url_root }}api/v1/links/<withdraw_id> -H
|
>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 }}"
|
||||||
</code>
|
</code>
|
||||||
</q-card-section>
|
</q-card-section>
|
||||||
</q-card>
|
</q-card>
|
||||||
@@ -83,7 +83,7 @@
|
|||||||
"max_withdrawable": <integer>, "uses": <integer>,
|
"max_withdrawable": <integer>, "uses": <integer>,
|
||||||
"wait_time": <integer>, "is_unique": <boolean>}' -H
|
"wait_time": <integer>, "is_unique": <boolean>}' -H
|
||||||
"Content-type: application/json" -H "X-Api-Key: {{
|
"Content-type: application/json" -H "X-Api-Key: {{
|
||||||
g.user.wallets[0].adminkey }}"
|
user.wallets[0].adminkey }}"
|
||||||
</code>
|
</code>
|
||||||
</q-card-section>
|
</q-card-section>
|
||||||
</q-card>
|
</q-card>
|
||||||
@@ -119,7 +119,7 @@
|
|||||||
"max_withdrawable": <integer>, "uses": <integer>,
|
"max_withdrawable": <integer>, "uses": <integer>,
|
||||||
"wait_time": <integer>, "is_unique": <boolean>}' -H
|
"wait_time": <integer>, "is_unique": <boolean>}' -H
|
||||||
"Content-type: application/json" -H "X-Api-Key: {{
|
"Content-type: application/json" -H "X-Api-Key: {{
|
||||||
g.user.wallets[0].adminkey }}"
|
user.wallets[0].adminkey }}"
|
||||||
</code>
|
</code>
|
||||||
</q-card-section>
|
</q-card-section>
|
||||||
</q-card>
|
</q-card>
|
||||||
@@ -143,7 +143,7 @@
|
|||||||
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
|
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
|
||||||
<code
|
<code
|
||||||
>curl -X DELETE {{ request.url_root }}api/v1/links/<withdraw_id>
|
>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 }}"
|
||||||
</code>
|
</code>
|
||||||
</q-card-section>
|
</q-card-section>
|
||||||
</q-card>
|
</q-card>
|
||||||
@@ -171,7 +171,7 @@
|
|||||||
<code
|
<code
|
||||||
>curl -X GET {{ request.url_root
|
>curl -X GET {{ request.url_root
|
||||||
}}api/v1/links/<the_hash>/<lnurl_id> -H "X-Api-Key: {{
|
}}api/v1/links/<the_hash>/<lnurl_id> -H "X-Api-Key: {{
|
||||||
g.user.wallets[0].inkey }}"
|
user.wallets[0].inkey }}"
|
||||||
</code>
|
</code>
|
||||||
</q-card-section>
|
</q-card-section>
|
||||||
</q-card>
|
</q-card>
|
||||||
|
@@ -10,6 +10,7 @@ from starlette.responses import HTMLResponse, JSONResponse # type: ignore
|
|||||||
|
|
||||||
from lnbits.core.crud import get_user
|
from lnbits.core.crud import get_user
|
||||||
from lnbits.decorators import WalletTypeInfo, get_key_type
|
from lnbits.decorators import WalletTypeInfo, get_key_type
|
||||||
|
from .models import CreateWithdrawData
|
||||||
|
|
||||||
# from fastapi import FastAPI, Query, Response
|
# from fastapi import FastAPI, Query, Response
|
||||||
|
|
||||||
@@ -27,7 +28,7 @@ from .crud import (
|
|||||||
|
|
||||||
@withdraw_ext.get("/api/v1/links", status_code=HTTPStatus.OK)
|
@withdraw_ext.get("/api/v1/links", status_code=HTTPStatus.OK)
|
||||||
# @api_check_wallet_key("invoice")
|
# @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]
|
wallet_ids = [wallet.wallet.id]
|
||||||
|
|
||||||
if all_wallets:
|
if all_wallets:
|
||||||
@@ -35,8 +36,8 @@ async def api_links(wallet: WalletTypeInfo = Depends(get_key_type), all_wallets:
|
|||||||
try:
|
try:
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
**link._asdict(),
|
**link.dict(),
|
||||||
**{"lnurl": link.lnurl},
|
**{"lnurl": link.lnurl(req)},
|
||||||
}
|
}
|
||||||
for link in await get_withdraw_links(wallet_ids)
|
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}}
|
return {**link, **{"lnurl": link.lnurl}}
|
||||||
|
|
||||||
class CreateData(BaseModel):
|
# class CreateData(BaseModel):
|
||||||
title: str = Query(...)
|
# title: str = Query(...)
|
||||||
min_withdrawable: int = Query(..., ge=1)
|
# min_withdrawable: int = Query(..., ge=1)
|
||||||
max_withdrawable: int = Query(..., ge=1)
|
# max_withdrawable: int = Query(..., ge=1)
|
||||||
uses: int = Query(..., ge=1)
|
# uses: int = Query(..., ge=1)
|
||||||
wait_time: int = Query(..., ge=1)
|
# wait_time: int = Query(..., ge=1)
|
||||||
is_unique: bool
|
# is_unique: bool
|
||||||
|
|
||||||
@withdraw_ext.post("/api/v1/links", status_code=HTTPStatus.CREATED)
|
@withdraw_ext.post("/api/v1/links", status_code=HTTPStatus.CREATED)
|
||||||
@withdraw_ext.put("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
|
@withdraw_ext.put("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
|
||||||
# @api_check_wallet_key("admin")
|
# @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:
|
if data.max_withdrawable < data.min_withdrawable:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
detail="`max_withdrawable` needs to be at least `min_withdrawable`.",
|
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)
|
link = await update_withdraw_link(link_id, **data, usescsv=usescsv, used=0)
|
||||||
else:
|
else:
|
||||||
link = await create_withdraw_link(
|
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:
|
# if link_id:
|
||||||
# response.status_code = HTTPStatus.OK
|
# 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}")
|
@withdraw_ext.delete("/api/v1/links/{link_id}")
|
||||||
|
Reference in New Issue
Block a user