[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,11 +55,23 @@ 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:
@boltcards_ext.put(
"/api/v1/cards/{card_id}",
status_code=HTTPStatus.OK,
dependencies=[Depends(validate_card)]
)
async def api_card_create_or_update(
data: CreateCardData,
card_id: str,
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> Card:
card = await get_card(card_id) card = await get_card(card_id)
if not card: if not card:
raise HTTPException( raise HTTPException(
@ -82,7 +88,19 @@ async def api_card_create_or_update(
status_code=HTTPStatus.BAD_REQUEST, status_code=HTTPStatus.BAD_REQUEST,
) )
card = await update_card(card_id, **data.dict()) card = await update_card(card_id, **data.dict())
else: 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) checkUid = await get_card_by_uid(data.uid)
if checkUid: if checkUid:
raise HTTPException( raise HTTPException(
@ -90,8 +108,8 @@ async def api_card_create_or_update(
status_code=HTTPStatus.BAD_REQUEST, status_code=HTTPStatus.BAD_REQUEST,
) )
card = await create_card(wallet_id=wallet.wallet.id, data=data) card = await create_card(wallet_id=wallet.wallet.id, data=data)
assert card assert card, "create_card should always return a card"
return card.dict() 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)