lnbits-legend/lnbits/extensions/scrub/views_api.py

108 lines
3.1 KiB
Python
Raw Normal View History

2022-05-17 21:27:52 +02:00
from http import HTTPStatus
2023-01-02 11:25:57 +01:00
from fastapi import Depends, Query
2022-05-17 21:27:52 +02:00
from starlette.exceptions import HTTPException
from lnbits.core.crud import get_user
2022-06-17 12:41:12 +02:00
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
2022-05-17 21:27:52 +02:00
from . import scrub_ext
from .crud import (
2022-05-19 12:39:59 +02:00
create_scrub_link,
delete_scrub_link,
get_scrub_link,
get_scrub_links,
2022-06-22 14:30:06 +02:00
unique_scrubed_wallet,
2022-05-19 12:39:59 +02:00
update_scrub_link,
2022-05-17 21:27:52 +02:00
)
2022-06-21 17:44:59 +02:00
from .models import CreateScrubLink
2022-05-17 21:27:52 +02:00
@scrub_ext.get("/api/v1/links", status_code=HTTPStatus.OK)
async def api_links(
wallet: WalletTypeInfo = Depends(get_key_type),
all_wallets: bool = Query(False),
):
wallet_ids = [wallet.wallet.id]
if all_wallets:
2023-01-02 11:25:01 +01:00
user = await get_user(wallet.wallet.user)
wallet_ids = user.wallet_ids if user else []
2022-05-17 21:27:52 +02:00
try:
2022-07-05 17:21:49 +02:00
return [link.dict() for link in await get_scrub_links(wallet_ids)]
2022-05-17 21:27:52 +02:00
2022-05-19 12:39:59 +02:00
except:
2022-05-17 21:27:52 +02:00
raise HTTPException(
2022-05-19 12:39:59 +02:00
status_code=HTTPStatus.NOT_FOUND,
2022-06-07 12:04:34 +02:00
detail="No SCRUB links made yet",
2022-05-17 21:27:52 +02:00
)
2022-07-05 17:21:49 +02:00
2022-06-22 17:46:27 +02:00
@scrub_ext.get("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
2023-01-02 11:25:57 +01:00
async def api_link_retrieve(link_id, wallet: WalletTypeInfo = Depends(get_key_type)):
2022-06-22 17:46:27 +02:00
link = await get_scrub_link(link_id)
if not link:
raise HTTPException(
detail="Scrub link does not exist.", status_code=HTTPStatus.NOT_FOUND
)
if link.wallet != wallet.wallet.id:
raise HTTPException(
detail="Not your pay link.", status_code=HTTPStatus.FORBIDDEN
)
return link
2022-05-17 21:27:52 +02:00
@scrub_ext.post("/api/v1/links", status_code=HTTPStatus.CREATED)
@scrub_ext.put("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
2022-06-17 12:41:12 +02:00
async def api_scrub_create_or_update(
data: CreateScrubLink,
2022-05-17 21:27:52 +02:00
link_id=None,
2022-06-17 12:41:12 +02:00
wallet: WalletTypeInfo = Depends(require_admin_key),
2022-05-17 21:27:52 +02:00
):
if link_id:
2022-06-17 12:41:12 +02:00
link = await get_scrub_link(link_id)
2022-05-17 21:27:52 +02:00
if not link:
raise HTTPException(
detail="Scrub link does not exist.", status_code=HTTPStatus.NOT_FOUND
)
if link.wallet != wallet.wallet.id:
raise HTTPException(
detail="Not your pay link.", status_code=HTTPStatus.FORBIDDEN
)
2022-06-17 12:41:12 +02:00
link = await update_scrub_link(**data.dict(), link_id=link_id)
2022-05-17 21:27:52 +02:00
else:
2022-06-22 14:30:06 +02:00
wallet_has_scrub = await unique_scrubed_wallet(wallet_id=data.wallet)
if wallet_has_scrub > 0:
raise HTTPException(
2022-07-05 17:21:49 +02:00
detail="Wallet is already being Scrubbed",
status_code=HTTPStatus.FORBIDDEN,
2022-06-22 14:30:06 +02:00
)
link = await create_scrub_link(data=data)
2022-06-17 12:41:12 +02:00
return link
2022-05-17 21:27:52 +02:00
@scrub_ext.delete("/api/v1/links/{link_id}")
2022-06-21 11:31:18 +02:00
async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(require_admin_key)):
2022-06-17 12:41:12 +02:00
link = await get_scrub_link(link_id)
2022-05-17 21:27:52 +02:00
if not link:
raise HTTPException(
detail="Scrub link does not exist.", status_code=HTTPStatus.NOT_FOUND
)
if link.wallet != wallet.wallet.id:
raise HTTPException(
detail="Not your pay link.", status_code=HTTPStatus.FORBIDDEN
)
2022-06-17 12:41:12 +02:00
await delete_scrub_link(link_id)
2022-11-02 16:30:18 +01:00
return "", HTTPStatus.NO_CONTENT