refactor: tiny url to use require decorators and wallet.id (#2338)

- also use `wallet.wallet.id` as key instead of `wallet.wallet.inkey`
This commit is contained in:
dni ⚡
2024-03-21 13:32:55 +01:00
committed by GitHub
parent 43a797444a
commit c03b81d2ea

View File

@ -9,7 +9,8 @@ from starlette.responses import RedirectResponse
from lnbits.decorators import ( from lnbits.decorators import (
WalletTypeInfo, WalletTypeInfo,
get_key_type, require_admin_key,
require_invoice_key,
) )
from ..crud import ( from ..crud import (
@ -28,15 +29,15 @@ tinyurl_router = APIRouter()
description="creates a tinyurl", description="creates a tinyurl",
) )
async def api_create_tinyurl( async def api_create_tinyurl(
url: str, endless: bool = False, wallet: WalletTypeInfo = Depends(get_key_type) url: str, endless: bool = False, wallet: WalletTypeInfo = Depends(require_admin_key)
): ):
tinyurls = await get_tinyurl_by_url(url) tinyurls = await get_tinyurl_by_url(url)
try: try:
for tinyurl in tinyurls: for tinyurl in tinyurls:
if tinyurl: if tinyurl:
if tinyurl.wallet == wallet.wallet.inkey: if tinyurl.wallet == wallet.wallet.id:
return tinyurl return tinyurl
return await create_tinyurl(url, endless, wallet.wallet.inkey) return await create_tinyurl(url, endless, wallet.wallet.id)
except Exception: except Exception:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Unable to create tinyurl" status_code=HTTPStatus.BAD_REQUEST, detail="Unable to create tinyurl"
@ -49,12 +50,12 @@ async def api_create_tinyurl(
description="get a tinyurl by id", description="get a tinyurl by id",
) )
async def api_get_tinyurl( async def api_get_tinyurl(
tinyurl_id: str, wallet: WalletTypeInfo = Depends(get_key_type) tinyurl_id: str, wallet: WalletTypeInfo = Depends(require_invoice_key)
): ):
try: try:
tinyurl = await get_tinyurl(tinyurl_id) tinyurl = await get_tinyurl(tinyurl_id)
if tinyurl: if tinyurl:
if tinyurl.wallet == wallet.wallet.inkey: if tinyurl.wallet == wallet.wallet.id:
return tinyurl return tinyurl
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Wrong key provided." status_code=HTTPStatus.FORBIDDEN, detail="Wrong key provided."
@ -71,12 +72,12 @@ async def api_get_tinyurl(
description="delete a tinyurl by id", description="delete a tinyurl by id",
) )
async def api_delete_tinyurl( async def api_delete_tinyurl(
tinyurl_id: str, wallet: WalletTypeInfo = Depends(get_key_type) tinyurl_id: str, wallet: WalletTypeInfo = Depends(require_admin_key)
): ):
try: try:
tinyurl = await get_tinyurl(tinyurl_id) tinyurl = await get_tinyurl(tinyurl_id)
if tinyurl: if tinyurl:
if tinyurl.wallet == wallet.wallet.inkey: if tinyurl.wallet == wallet.wallet.id:
await delete_tinyurl(tinyurl_id) await delete_tinyurl(tinyurl_id)
return {"deleted": True} return {"deleted": True}
raise HTTPException( raise HTTPException(