can't seem to make url_for work

This commit is contained in:
Tiago vasconcelos 2021-10-06 11:10:24 +01:00
parent 81826f3c13
commit 7646bbefd5
4 changed files with 17 additions and 16 deletions

View File

@ -22,7 +22,7 @@ async def api_lnurl_response(request: Request, link_id):
rate = await get_fiat_rate_satoshis(link.currency) if link.currency else 1
resp = LnurlPayResponse(
callback=url_for("lnurlp.api_lnurl_callback", link_id=link.id, extra=request.path_params['extra'], _external=True),
callback=request.url_for("lnurlp.api_lnurl_callback", link_id=link.id, extra=request.path_params['extra'], _external=True),
min_sendable=math.ceil(link.min * rate) * 1000,
max_sendable=round(link.max * rate) * 1000,
metadata=link.lnurlpay_metadata,
@ -35,7 +35,7 @@ async def api_lnurl_response(request: Request, link_id):
return params
@lnurlp_ext.get("/api/v1/lnurl/cb/{link_id}", status_code=HTTPStatus.OK)
@lnurlp_ext.get("/api/v1/lnurl/cb/{link_id}", status_code=HTTPStatus.OK, name="lnurlp.api_lnurl_callback")
async def api_lnurl_callback(request: Request, link_id):
link = await increment_pay_link(link_id, served_pr=1)
if not link:

View File

@ -37,10 +37,9 @@ class PayLink(BaseModel):
data = dict(row)
return cls(**data)
@property
def lnurl(self) -> str:
r = Request
url = r.url_for("lnurlp.api_lnurl_response", link_id=self.id, _external=True)
def lnurl(self, req: Request) -> str:
url = req.url_for("lnurlp.api_lnurl_response", link_id=self.id)
return lnurl_encode(url)
@property

View File

@ -51,7 +51,7 @@ new Vue({
LNbits.api
.request(
'GET',
'/lnurlp/api/v1/links?all_wallets',
'/lnurlp/api/v1/links?all_wallets=true',
this.g.user.wallets[0].inkey
)
.then(response => {

View File

@ -6,7 +6,7 @@ from pydantic.main import BaseModel
from http import HTTPStatus
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl # type: ignore
from starlette.exceptions import HTTPException
from starlette.requests import Request
from fastapi import Request
from starlette.responses import HTMLResponse, JSONResponse # type: ignore
from lnbits.core.crud import get_user
@ -30,17 +30,19 @@ async def api_list_currencies_available():
@lnurlp_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:
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
# print("LINKS", [link.dict() for link in await get_pay_links(wallet_ids)])
print("LINKS", [{"lnurl": link.lnurl(req)} for link in await get_pay_links(wallet_ids)])
try:
return [
{**link._asdict(), **{"lnurl": link.lnurl}}
for link in await get_pay_links(wallet_ids)
]
return [link.dict() for link in await get_pay_links(wallet_ids)]
# return [
# {**link.dict(), "lnurl": link.lnurl}
# for link in await get_pay_links(wallet_ids)
# ]
except LnurlInvalidUrl:
raise HTTPException(
@ -57,7 +59,7 @@ async def api_links(wallet: WalletTypeInfo = Depends(get_key_type), all_wallets:
@lnurlp_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)):
async def api_link_retrieve(r: Request, link_id, wallet: WalletTypeInfo = Depends(get_key_type)):
link = await get_pay_link(link_id)
if not link:
@ -74,7 +76,7 @@ async def api_link_retrieve(link_id, wallet: WalletTypeInfo = Depends(get_key_ty
)
# return {"message": "Not your pay link."}, HTTPStatus.FORBIDDEN
return {**link._asdict(), **{"lnurl": link.lnurl}}
return {**link._asdict(), **{"lnurl": link.lnurl(r)}}
@lnurlp_ext.post("/api/v1/links", status_code=HTTPStatus.CREATED)