-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviews_api.py
110 lines (86 loc) · 3.1 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from http import HTTPStatus
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, Query
from lnbits.core.crud import get_user
from lnbits.core.models import WalletTypeInfo
from lnbits.decorators import (
require_admin_key,
require_invoice_key,
)
from .crud import (
create_scrub_link,
delete_scrub_link,
get_scrub_link,
get_scrub_links,
unique_scrubed_wallet,
update_scrub_link,
)
from .models import CreateScrubLink, ScrubLink
scrub_api_router = APIRouter()
@scrub_api_router.get("/api/v1/links", status_code=HTTPStatus.OK)
async def api_links(
key_info: WalletTypeInfo = Depends(require_invoice_key),
all_wallets: bool = Query(False),
) -> list[ScrubLink]:
wallet_ids = [key_info.wallet.id]
if all_wallets:
user = await get_user(key_info.wallet.user)
wallet_ids = user.wallet_ids if user else []
return await get_scrub_links(wallet_ids)
@scrub_api_router.get("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
async def api_link_retrieve(
link_id: str, key_info: WalletTypeInfo = Depends(require_invoice_key)
):
link = await get_scrub_link(link_id)
if not link:
raise HTTPException(
detail="Scrub link does not exist.", status_code=HTTPStatus.NOT_FOUND
)
if link.wallet != key_info.wallet.id:
raise HTTPException(
detail="Not your pay link.", status_code=HTTPStatus.FORBIDDEN
)
return link
@scrub_api_router.post("/api/v1/links", status_code=HTTPStatus.CREATED)
@scrub_api_router.put("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
async def api_scrub_create_or_update(
data: CreateScrubLink,
link_id: Optional[str] = None,
wallet: WalletTypeInfo = Depends(require_admin_key),
):
if link_id:
link = await get_scrub_link(link_id)
if not link:
raise HTTPException(
detail="Scrub link does not exist.", status_code=HTTPStatus.NOT_FOUND
)
if link.wallet != wallet.wallet.id:
raise HTTPException(
detail="Not your pay link.", status_code=HTTPStatus.FORBIDDEN
)
for k, v in data.dict().items():
setattr(link, k, v)
link = await update_scrub_link(link)
else:
wallet_has_scrub = await unique_scrubed_wallet(wallet_id=data.wallet)
if wallet_has_scrub > 0:
raise HTTPException(
detail="Wallet is already being Scrubbed",
status_code=HTTPStatus.FORBIDDEN,
)
link = await create_scrub_link(data=data)
return link
@scrub_api_router.delete("/api/v1/links/{link_id}")
async def api_link_delete(
link_id: str, key_info: WalletTypeInfo = Depends(require_admin_key)
):
link = await get_scrub_link(link_id)
if not link:
raise HTTPException(
detail="Scrub link does not exist.", status_code=HTTPStatus.NOT_FOUND
)
if link.wallet != key_info.wallet.id:
raise HTTPException(
detail="Not your pay link.", status_code=HTTPStatus.FORBIDDEN
)
await delete_scrub_link(link_id)