feat: code quality (#26)

* feat: code quality

* fixes
This commit is contained in:
dni ⚡ 2024-08-02 10:05:26 +02:00 committed by GitHub
commit 32bf4ae1d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 2822 additions and 61 deletions

View file

@ -1,39 +1,38 @@
from http import HTTPStatus
from typing import List
from fastapi import Depends
from loguru import logger
from starlette.exceptions import HTTPException
from fastapi import APIRouter, Depends, HTTPException
from lnbits.core.crud import get_wallet, get_wallet_for_key
from lnbits.decorators import WalletTypeInfo, require_admin_key
from lnbits.core.models import WalletTypeInfo
from lnbits.decorators import require_admin_key
from loguru import logger
from . import splitpayments_ext
from .crud import get_targets, set_targets
from .models import Target, TargetPutList
splitpayments_api_router = APIRouter()
@splitpayments_ext.get("/api/v1/targets")
@splitpayments_api_router.get("/api/v1/targets")
async def api_targets_get(
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> List[Target]:
) -> list[Target]:
targets = await get_targets(wallet.wallet.id)
return targets or []
@splitpayments_ext.put("/api/v1/targets", status_code=HTTPStatus.OK)
@splitpayments_api_router.put("/api/v1/targets", status_code=HTTPStatus.OK)
async def api_targets_set(
target_put: TargetPutList,
source_wallet: WalletTypeInfo = Depends(require_admin_key),
) -> None:
try:
targets: List[Target] = []
targets: list[Target] = []
for entry in target_put.targets:
if entry.wallet.find("@") < 0 and entry.wallet.find("LNURL") < 0:
wallet = await get_wallet(entry.wallet)
if not wallet:
wallet = await get_wallet_for_key(entry.wallet, "invoice")
wallet = await get_wallet_for_key(entry.wallet)
if not wallet:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
@ -42,7 +41,8 @@ async def api_targets_set(
if wallet.id == source_wallet.wallet.id:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Can't split to itself."
status_code=HTTPStatus.BAD_REQUEST,
detail="Can't split to itself.",
)
if entry.percent <= 0:
@ -73,10 +73,10 @@ async def api_targets_set(
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail="Cannot set targets.",
)
) from ex
@splitpayments_ext.delete("/api/v1/targets", status_code=HTTPStatus.OK)
@splitpayments_api_router.delete("/api/v1/targets", status_code=HTTPStatus.OK)
async def api_targets_delete(
source_wallet: WalletTypeInfo = Depends(require_admin_key),
) -> None: