11import json
22from http import HTTPStatus
33
4- from fastapi import APIRouter , Depends , HTTPException , Query
4+ from fastapi import APIRouter , Depends , HTTPException , Query , Request
55from lnbits .core .crud import get_user
66from lnbits .core .models import SimpleStatus , WalletTypeInfo
77from lnbits .decorators import require_admin_key , require_invoice_key
1414 get_withdraw_links ,
1515 update_withdraw_link ,
1616)
17+ from .helpers import create_lnurl
1718from .models import CreateWithdrawData , HashCheck , PaginatedWithdraws , WithdrawLink
1819
1920withdraw_ext_api = APIRouter (prefix = "/api/v1" )
2021
2122
2223@withdraw_ext_api .get ("/links" , status_code = HTTPStatus .OK )
2324async def api_links (
25+ request : Request ,
2426 key_info : WalletTypeInfo = Depends (require_invoice_key ),
2527 all_wallets : bool = Query (False ),
2628 offset : int = Query (0 ),
@@ -32,12 +34,26 @@ async def api_links(
3234 user = await get_user (key_info .wallet .user )
3335 wallet_ids = user .wallet_ids if user else []
3436
35- return await get_withdraw_links (wallet_ids , limit , offset )
37+ links = await get_withdraw_links (wallet_ids , limit , offset )
38+
39+ for linkk in links .data :
40+ try :
41+ lnurl = create_lnurl (linkk , request )
42+ except ValueError as exc :
43+ raise HTTPException (
44+ status_code = HTTPStatus .INTERNAL_SERVER_ERROR ,
45+ detail = str (exc ),
46+ ) from exc
47+ linkk .lnurl = str (lnurl .bech32 )
48+
49+ return links
3650
3751
3852@withdraw_ext_api .get ("/links/{link_id}" , status_code = HTTPStatus .OK )
3953async def api_link_retrieve (
40- link_id : str , key_info : WalletTypeInfo = Depends (require_invoice_key )
54+ request : Request ,
55+ link_id : str ,
56+ key_info : WalletTypeInfo = Depends (require_invoice_key ),
4157) -> WithdrawLink :
4258 link = await get_withdraw_link (link_id , 0 )
4359
@@ -50,12 +66,22 @@ async def api_link_retrieve(
5066 raise HTTPException (
5167 detail = "Not your withdraw link." , status_code = HTTPStatus .FORBIDDEN
5268 )
69+
70+ try :
71+ lnurl = create_lnurl (link , request )
72+ except ValueError as exc :
73+ raise HTTPException (
74+ status_code = HTTPStatus .INTERNAL_SERVER_ERROR ,
75+ detail = str (exc ),
76+ ) from exc
77+ link .lnurl = str (lnurl .bech32 )
5378 return link
5479
5580
5681@withdraw_ext_api .post ("/links" , status_code = HTTPStatus .CREATED )
5782@withdraw_ext_api .put ("/links/{link_id}" )
5883async def api_link_create_or_update (
84+ request : Request ,
5985 data : CreateWithdrawData ,
6086 link_id : str | None = None ,
6187 key_info : WalletTypeInfo = Depends (require_admin_key ),
@@ -131,6 +157,15 @@ async def api_link_create_or_update(
131157 link = await update_withdraw_link (link )
132158 else :
133159 link = await create_withdraw_link (wallet_id = key_info .wallet .id , data = data )
160+ try :
161+ lnurl = create_lnurl (link , request )
162+ except ValueError as exc :
163+ raise HTTPException (
164+ status_code = HTTPStatus .INTERNAL_SERVER_ERROR ,
165+ detail = str (exc ),
166+ ) from exc
167+
168+ link .lnurl = str (lnurl .bech32 )
134169
135170 return link
136171
0 commit comments