-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews_api.py
104 lines (84 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
from http import HTTPStatus
from fastapi import APIRouter, Depends, HTTPException, Request
from lnbits.core.crud import get_user
from lnbits.core.models import WalletTypeInfo
from lnbits.decorators import (
require_admin_key,
require_invoice_key,
)
from lnbits.helpers import urlsafe_short_hash
from .crud import (
create_bitcoinswitch,
delete_bitcoinswitch,
get_bitcoinswitch,
get_bitcoinswitches,
update_bitcoinswitch,
)
from .models import Bitcoinswitch, CreateBitcoinswitch
bitcoinswitch_api_router = APIRouter()
@bitcoinswitch_api_router.post(
"/api/v1/bitcoinswitch", dependencies=[Depends(require_admin_key)]
)
async def api_bitcoinswitch_create(
request: Request, data: CreateBitcoinswitch
) -> Bitcoinswitch:
bitcoinswitch_id = urlsafe_short_hash()
# compute lnurl for each pin of switch
url = request.url_for(
"bitcoinswitch.lnurl_params", bitcoinswitch_id=bitcoinswitch_id
)
for switch in data.switches:
switch.set_lnurl(str(url))
return await create_bitcoinswitch(bitcoinswitch_id, data)
@bitcoinswitch_api_router.put(
"/api/v1/bitcoinswitch/{bitcoinswitch_id}",
dependencies=[Depends(require_admin_key)],
)
async def api_bitcoinswitch_update(
request: Request, data: CreateBitcoinswitch, bitcoinswitch_id: str
):
bitcoinswitch = await get_bitcoinswitch(bitcoinswitch_id)
if not bitcoinswitch:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="bitcoinswitch does not exist"
)
for k, v in data.dict().items():
if v is not None:
setattr(bitcoinswitch, k, v)
# compute lnurl for each pin of switch
url = request.url_for(
"bitcoinswitch.lnurl_params", bitcoinswitch_id=bitcoinswitch_id
)
for switch in data.switches:
switch.set_lnurl(str(url))
bitcoinswitch.switches = data.switches
return await update_bitcoinswitch(bitcoinswitch)
@bitcoinswitch_api_router.get("/api/v1/bitcoinswitch")
async def api_bitcoinswitchs_retrieve(
key_info: WalletTypeInfo = Depends(require_invoice_key),
) -> list[Bitcoinswitch]:
user = await get_user(key_info.wallet.user)
assert user, "Bitcoinswitch cannot retrieve user"
return await get_bitcoinswitches(user.wallet_ids)
@bitcoinswitch_api_router.get(
"/api/v1/bitcoinswitch/{bitcoinswitch_id}",
dependencies=[Depends(require_invoice_key)],
)
async def api_bitcoinswitch_retrieve(bitcoinswitch_id: str):
bitcoinswitch = await get_bitcoinswitch(bitcoinswitch_id)
if not bitcoinswitch:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="bitcoinswitch does not exist"
)
return bitcoinswitch
@bitcoinswitch_api_router.delete(
"/api/v1/bitcoinswitch/{bitcoinswitch_id}",
dependencies=[Depends(require_admin_key)],
)
async def api_bitcoinswitch_delete(bitcoinswitch_id: str):
bitcoinswitch = await get_bitcoinswitch(bitcoinswitch_id)
if not bitcoinswitch:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Lnurldevice does not exist."
)
await delete_bitcoinswitch(bitcoinswitch_id)