[FIX] split up api endpoints (#15)

This commit is contained in:
dni ⚡ 2023-08-22 18:17:49 +02:00 committed by GitHub
commit f153c26517
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,7 +18,7 @@ from .crud import (
get_refunds, get_refunds,
update_card, update_card,
) )
from .models import CreateCardData from .models import Card, CreateCardData
@boltcards_ext.get("/api/v1/cards") @boltcards_ext.get("/api/v1/cards")
@ -34,13 +34,7 @@ async def api_cards(
return [card.dict() for card in await get_cards(wallet_ids)] return [card.dict() for card in await get_cards(wallet_ids)]
@boltcards_ext.post("/api/v1/cards", status_code=HTTPStatus.CREATED) def validate_card(data: CreateCardData):
@boltcards_ext.put("/api/v1/cards/{card_id}", status_code=HTTPStatus.OK)
async def api_card_create_or_update(
data: CreateCardData,
card_id: Optional[str],
wallet: WalletTypeInfo = Depends(require_admin_key),
):
try: try:
if len(bytes.fromhex(data.uid)) != 7: if len(bytes.fromhex(data.uid)) != 7:
raise HTTPException( raise HTTPException(
@ -61,37 +55,61 @@ async def api_card_create_or_update(
raise HTTPException( raise HTTPException(
detail="Invalid bytes for k2.", status_code=HTTPStatus.BAD_REQUEST detail="Invalid bytes for k2.", status_code=HTTPStatus.BAD_REQUEST
) )
except: except Exception:
raise HTTPException( raise HTTPException(
detail="Invalid byte data provided.", status_code=HTTPStatus.BAD_REQUEST detail="Invalid byte data provided.", status_code=HTTPStatus.BAD_REQUEST
) )
if card_id:
card = await get_card(card_id)
if not card: @boltcards_ext.put(
raise HTTPException( "/api/v1/cards/{card_id}",
detail="Card does not exist.", status_code=HTTPStatus.NOT_FOUND status_code=HTTPStatus.OK,
) dependencies=[Depends(validate_card)]
if card.wallet != wallet.wallet.id: )
raise HTTPException( async def api_card_create_or_update(
detail="Not your card.", status_code=HTTPStatus.FORBIDDEN data: CreateCardData,
) card_id: str,
checkUid = await get_card_by_uid(data.uid) wallet: WalletTypeInfo = Depends(require_admin_key),
if checkUid and checkUid.id != card_id: ) -> Card:
raise HTTPException(
detail="UID already registered. Delete registered card and try again.", card = await get_card(card_id)
status_code=HTTPStatus.BAD_REQUEST, if not card:
) raise HTTPException(
card = await update_card(card_id, **data.dict()) detail="Card does not exist.", status_code=HTTPStatus.NOT_FOUND
else: )
checkUid = await get_card_by_uid(data.uid) if card.wallet != wallet.wallet.id:
if checkUid: raise HTTPException(
raise HTTPException( detail="Not your card.", status_code=HTTPStatus.FORBIDDEN
detail="UID already registered. Delete registered card and try again.", )
status_code=HTTPStatus.BAD_REQUEST, checkUid = await get_card_by_uid(data.uid)
) if checkUid and checkUid.id != card_id:
card = await create_card(wallet_id=wallet.wallet.id, data=data) raise HTTPException(
assert card detail="UID already registered. Delete registered card and try again.",
return card.dict() status_code=HTTPStatus.BAD_REQUEST,
)
card = await update_card(card_id, **data.dict())
assert card, "update_card should always return a card"
return card
@boltcards_ext.post(
"/api/v1/cards",
status_code=HTTPStatus.CREATED,
dependencies=[Depends(validate_card)]
)
async def api_card_create(
data: CreateCardData,
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> Card:
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,
)
card = await create_card(wallet_id=wallet.wallet.id, data=data)
assert card, "create_card should always return a card"
return card
@boltcards_ext.get("/api/v1/cards/enable/{card_id}/{enable}", status_code=HTTPStatus.OK) @boltcards_ext.get("/api/v1/cards/enable/{card_id}/{enable}", status_code=HTTPStatus.OK)