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

168 lines
5.1 KiB
Python
Raw Normal View History

2022-08-14 23:52:55 +02:00
import secrets
2022-06-13 21:08:06 +02:00
from http import HTTPStatus
from fastapi.params import Depends, Query
2022-08-29 08:51:32 -06:00
from loguru import logger
2022-06-13 21:08:06 +02:00
from starlette.exceptions import HTTPException
from starlette.requests import Request
from lnbits.core.crud import get_user
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
from . import boltcards_ext
from .crud import (
2022-07-15 16:43:06 +02:00
create_card,
delete_card,
2022-08-29 08:51:32 -06:00
enable_disable_card,
2022-06-13 21:08:06 +02:00
get_card,
get_card_by_otp,
get_card_by_uid,
2022-07-15 16:43:06 +02:00
get_cards,
2022-06-21 22:04:43 +02:00
get_hits,
2022-08-29 08:51:32 -06:00
get_refunds,
2022-06-13 21:08:06 +02:00
update_card,
update_card_otp,
2022-06-13 21:08:06 +02:00
)
from .models import CreateCardData
2022-07-15 16:43:06 +02:00
2022-08-29 14:18:18 +01:00
2022-06-13 21:08:06 +02:00
@boltcards_ext.get("/api/v1/cards")
async def api_cards(
g: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)
):
wallet_ids = [g.wallet.id]
if all_wallets:
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
return [card.dict() for card in await get_cards(wallet_ids)]
2022-07-15 16:43:06 +02:00
2022-06-13 21:08:06 +02:00
@boltcards_ext.post("/api/v1/cards", status_code=HTTPStatus.CREATED)
@boltcards_ext.put("/api/v1/cards/{card_id}", status_code=HTTPStatus.OK)
2022-08-14 23:52:55 +02:00
async def api_card_create_or_update(
2022-07-15 16:43:06 +02:00
# req: Request,
2022-06-13 21:08:06 +02:00
data: CreateCardData,
card_id: str = None,
wallet: WalletTypeInfo = Depends(require_admin_key),
):
2022-08-19 16:54:06 -06:00
try:
if len(bytes.fromhex(data.uid)) != 7:
raise HTTPException(
2022-08-19 16:52:06 -06:00
detail="Invalid bytes for card uid.", status_code=HTTPStatus.BAD_REQUEST
)
2022-08-19 16:54:06 -06:00
if len(bytes.fromhex(data.k0)) != 16:
raise HTTPException(
2022-08-19 16:52:06 -06:00
detail="Invalid bytes for k0.", status_code=HTTPStatus.BAD_REQUEST
)
2022-08-19 16:54:06 -06:00
if len(bytes.fromhex(data.k1)) != 16:
raise HTTPException(
2022-08-19 16:52:06 -06:00
detail="Invalid bytes for k1.", status_code=HTTPStatus.BAD_REQUEST
)
2022-08-19 16:54:06 -06:00
if len(bytes.fromhex(data.k2)) != 16:
raise HTTPException(
2022-08-19 16:52:06 -06:00
detail="Invalid bytes for k2.", status_code=HTTPStatus.BAD_REQUEST
)
2022-08-19 16:54:06 -06:00
except:
raise HTTPException(
detail="Invalid byte data provided.", status_code=HTTPStatus.BAD_REQUEST
)
2022-06-13 21:08:06 +02:00
if card_id:
card = await get_card(card_id)
if not card:
raise HTTPException(
detail="Card does not exist.", status_code=HTTPStatus.NOT_FOUND
)
if card.wallet != wallet.wallet.id:
raise HTTPException(
detail="Not your card.", status_code=HTTPStatus.FORBIDDEN
)
checkUid = await get_card_by_uid(data.uid)
if checkUid and checkUid.id != card_id:
raise HTTPException(
detail="UID already registered. Delete registered card and try again.",
status_code=HTTPStatus.BAD_REQUEST,
)
2022-07-15 16:43:06 +02:00
card = await update_card(card_id, **data.dict())
2022-06-13 21:08:06 +02:00
else:
checkUid = await get_card_by_uid(data.uid)
if checkUid:
raise HTTPException(
detail="UID already registered. Delete registered card and try again.",
status_code=HTTPStatus.BAD_REQUEST,
)
2022-07-15 16:43:06 +02:00
card = await create_card(wallet_id=wallet.wallet.id, data=data)
2022-06-13 21:08:06 +02:00
return card.dict()
2022-08-29 14:18:18 +01:00
2022-08-28 10:58:17 +01:00
@boltcards_ext.get("/api/v1/cards/enable/{card_id}/{enable}", status_code=HTTPStatus.OK)
async def enable_card(
card_id,
enable,
wallet: WalletTypeInfo = Depends(require_admin_key),
):
card = await get_card(card_id)
if not card:
2022-08-29 14:18:18 +01:00
raise HTTPException(detail="No card found.", status_code=HTTPStatus.NOT_FOUND)
2022-08-28 10:58:17 +01:00
if card.wallet != wallet.wallet.id:
2022-08-29 14:18:18 +01:00
raise HTTPException(detail="Not your card.", status_code=HTTPStatus.FORBIDDEN)
2022-08-28 10:58:17 +01:00
card = await enable_disable_card(enable=enable, id=card_id)
return card.dict()
2022-07-15 16:43:06 +02:00
2022-08-29 14:18:18 +01:00
2022-06-13 21:08:06 +02:00
@boltcards_ext.delete("/api/v1/cards/{card_id}")
2022-08-14 23:52:55 +02:00
async def api_card_delete(card_id, wallet: WalletTypeInfo = Depends(require_admin_key)):
2022-06-13 21:08:06 +02:00
card = await get_card(card_id)
if not card:
raise HTTPException(
detail="Card does not exist.", status_code=HTTPStatus.NOT_FOUND
)
if card.wallet != wallet.wallet.id:
2022-07-15 16:43:06 +02:00
raise HTTPException(detail="Not your card.", status_code=HTTPStatus.FORBIDDEN)
2022-06-13 21:08:06 +02:00
await delete_card(card_id)
2022-11-02 15:30:18 +00:00
return "", HTTPStatus.NO_CONTENT
2022-06-13 21:08:06 +02:00
2022-07-15 16:43:06 +02:00
2022-06-21 22:04:43 +02:00
@boltcards_ext.get("/api/v1/hits")
async def api_hits(
g: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)
):
wallet_ids = [g.wallet.id]
2022-08-24 09:33:38 +01:00
if all_wallets:
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
cards = await get_cards(wallet_ids)
cards_ids = []
for card in cards:
cards_ids.append(card.id)
return [hit.dict() for hit in await get_hits(cards_ids)]
2022-08-29 14:18:18 +01:00
2022-08-24 09:33:38 +01:00
@boltcards_ext.get("/api/v1/refunds")
async def api_hits(
g: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)
):
wallet_ids = [g.wallet.id]
2022-06-21 22:04:43 +02:00
if all_wallets:
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
cards = await get_cards(wallet_ids)
cards_ids = []
for card in cards:
cards_ids.append(card.id)
2022-08-27 16:37:31 +01:00
hits = await get_hits(cards_ids)
hits_ids = []
for hit in hits:
hits_ids.append(hit.id)
2022-06-21 22:04:43 +02:00
2022-08-29 14:18:18 +01:00
return [refund.dict() for refund in await get_refunds(hits_ids)]