-
Notifications
You must be signed in to change notification settings - Fork 3
/
views_api.py
101 lines (84 loc) · 2.97 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
from http import HTTPStatus
from fastapi import APIRouter, Depends, Request
from fastapi.exceptions import HTTPException
from lnbits.core.models import WalletTypeInfo
from lnbits.core.services import websocket_updater
from lnbits.decorators import require_admin_key, require_invoice_key
from .crud import (
create_copilot,
delete_copilot,
get_copilot,
get_copilots,
update_copilot,
)
from .models import Copilot, CreateCopilotData
copilot_api_router = APIRouter()
@copilot_api_router.get("/api/v1/copilot")
async def api_copilots_retrieve(wallet: WalletTypeInfo = Depends(require_invoice_key)):
wallet_user = wallet.wallet.user
copilots = await get_copilots(wallet_user)
return copilots
@copilot_api_router.get(
"/api/v1/copilot/{copilot_id}", dependencies=[Depends(require_invoice_key)]
)
async def api_copilot_retrieve(
req: Request,
copilot_id: str,
):
copilot = await get_copilot(copilot_id)
if not copilot:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Copilot not found"
)
if not copilot.lnurl_toggle:
return copilot
return {**copilot.dict(), **{"lnurl": copilot.lnurl(req)}}
@copilot_api_router.post("/api/v1/copilot")
async def api_copilot_create(
data: CreateCopilotData,
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> Copilot:
data.user = wallet.wallet.user
data.wallet = wallet.wallet.id
return await create_copilot(data)
@copilot_api_router.put("/api/v1/copilot/{copilot_id}")
async def api_copilot_update(
data: CreateCopilotData,
copilot_id: str,
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> Copilot:
copilot = await get_copilot(copilot_id)
if not copilot:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Copilot does not exist"
)
data.user = wallet.wallet.user
data.wallet = wallet.wallet.id
for key, value in data.dict().items():
if value:
setattr(copilot, key, value)
return await update_copilot(copilot)
@copilot_api_router.delete(
"/api/v1/copilot/{copilot_id}", dependencies=[Depends(require_admin_key)]
)
async def api_copilot_delete(copilot_id: str) -> None:
copilot = await get_copilot(copilot_id)
if not copilot:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Copilot does not exist"
)
await delete_copilot(copilot_id)
@copilot_api_router.get("/api/v1/copilot/ws/{copilot_id}/{comment}/{data}")
async def api_copilot_ws_relay(copilot_id: str, comment: str, data: str):
copilot = await get_copilot(copilot_id)
if not copilot:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Copilot does not exist"
)
try:
await websocket_updater(copilot_id, f"{data} - {comment}")
except Exception as exc:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not your copilot"
) from exc
return ""