-
Notifications
You must be signed in to change notification settings - Fork 7
/
views_api.py
85 lines (69 loc) · 2.87 KB
/
views_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from http import HTTPStatus
from fastapi import APIRouter, Depends, HTTPException
from lnbits.core.crud import get_wallet, get_wallet_for_key
from lnbits.core.models import WalletTypeInfo
from lnbits.decorators import require_admin_key
from lnbits.helpers import urlsafe_short_hash
from loguru import logger
from .crud import get_targets, set_targets
from .models import Target, TargetPutList
splitpayments_api_router = APIRouter()
@splitpayments_api_router.get("/api/v1/targets")
async def api_targets_get(
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> list[Target]:
targets = await get_targets(wallet.wallet.id)
return targets or []
@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] = []
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)
if not wallet:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=f"Invalid wallet '{entry.wallet}'.",
)
if wallet.id == source_wallet.wallet.id:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="Can't split to itself.",
)
if entry.percent <= 0:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=f"Invalid percent '{entry.percent}'.",
)
targets.append(
Target(
id=urlsafe_short_hash(),
wallet=entry.wallet,
source=source_wallet.wallet.id,
percent=entry.percent,
alias=entry.alias,
)
)
percent_sum = sum([target.percent for target in targets])
if percent_sum > 100:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Splitting over 100%"
)
await set_targets(source_wallet.wallet.id, targets)
except Exception as ex:
logger.warning(ex)
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail="Cannot set targets.",
) from ex
@splitpayments_api_router.delete("/api/v1/targets", status_code=HTTPStatus.OK)
async def api_targets_delete(
source_wallet: WalletTypeInfo = Depends(require_admin_key),
) -> None:
await set_targets(source_wallet.wallet.id, [])