url_for issues

This commit is contained in:
Tiago vasconcelos 2021-10-06 14:40:12 +01:00
parent 7646bbefd5
commit 46ab9960ad
5 changed files with 40 additions and 37 deletions

View File

@ -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,
),
)

View File

@ -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(",")

View File

@ -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) {

View File

@ -23,7 +23,7 @@
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code
>curl -X GET {{ request.url_root }}api/v1/links -H "X-Api-Key: {{
g.user.wallets[0].inkey }}"
user.wallets[0].inkey }}"
</code>
</q-card-section>
</q-card>
@ -50,7 +50,7 @@
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code
>curl -X GET {{ request.url_root }}api/v1/links/&lt;withdraw_id&gt; -H
"X-Api-Key: {{ g.user.wallets[0].inkey }}"
"X-Api-Key: {{ user.wallets[0].inkey }}"
</code>
</q-card-section>
</q-card>
@ -83,7 +83,7 @@
"max_withdrawable": &lt;integer&gt;, "uses": &lt;integer&gt;,
"wait_time": &lt;integer&gt;, "is_unique": &lt;boolean&gt;}' -H
"Content-type: application/json" -H "X-Api-Key: {{
g.user.wallets[0].adminkey }}"
user.wallets[0].adminkey }}"
</code>
</q-card-section>
</q-card>
@ -119,7 +119,7 @@
"max_withdrawable": &lt;integer&gt;, "uses": &lt;integer&gt;,
"wait_time": &lt;integer&gt;, "is_unique": &lt;boolean&gt;}' -H
"Content-type: application/json" -H "X-Api-Key: {{
g.user.wallets[0].adminkey }}"
user.wallets[0].adminkey }}"
</code>
</q-card-section>
</q-card>
@ -143,7 +143,7 @@
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code
>curl -X DELETE {{ request.url_root }}api/v1/links/&lt;withdraw_id&gt;
-H "X-Api-Key: {{ g.user.wallets[0].adminkey }}"
-H "X-Api-Key: {{ user.wallets[0].adminkey }}"
</code>
</q-card-section>
</q-card>
@ -171,7 +171,7 @@
<code
>curl -X GET {{ request.url_root
}}api/v1/links/&lt;the_hash&gt;/&lt;lnurl_id&gt; -H "X-Api-Key: {{
g.user.wallets[0].inkey }}"
user.wallets[0].inkey }}"
</code>
</q-card-section>
</q-card>

View File

@ -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}")