lnbits-legend/lnbits/extensions/copilot/views_api.py

95 lines
2.8 KiB
Python
Raw Normal View History

2021-10-12 19:54:35 +01:00
from http import HTTPStatus
2023-01-04 09:09:30 +01:00
from fastapi import Depends, Query, Request
2021-10-18 12:08:41 +01:00
from starlette.exceptions import HTTPException
2022-12-01 18:45:00 +00:00
from lnbits.core.services import websocketUpdater
2021-10-20 12:03:11 +01:00
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
2021-10-18 12:08:41 +01:00
2021-10-12 19:54:35 +01:00
from . import copilot_ext
from .crud import (
create_copilot,
2021-10-18 12:08:41 +01:00
delete_copilot,
2021-10-12 19:54:35 +01:00
get_copilot,
get_copilots,
2021-10-18 12:08:41 +01:00
update_copilot,
2021-10-12 19:54:35 +01:00
)
2021-10-18 12:08:41 +01:00
from .models import CreateCopilotData
2021-10-12 19:54:35 +01:00
#######################COPILOT##########################
2021-10-13 12:37:10 +01:00
@copilot_ext.get("/api/v1/copilot")
async def api_copilots_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)):
2021-10-13 10:43:11 +01:00
wallet_user = wallet.wallet.user
copilots = [copilot.dict() for copilot in await get_copilots(wallet_user)]
2021-10-13 12:37:10 +01:00
try:
2021-10-13 10:43:11 +01:00
return copilots
2021-10-13 12:37:10 +01:00
except:
2021-10-17 18:33:29 +01:00
raise HTTPException(status_code=HTTPStatus.NO_CONTENT, detail="No copilots")
2021-10-12 19:54:35 +01:00
2021-10-13 12:37:10 +01:00
@copilot_ext.get("/api/v1/copilot/{copilot_id}")
async def api_copilot_retrieve(
2021-10-17 23:19:42 +01:00
req: Request,
copilot_id: str = Query(None),
wallet: WalletTypeInfo = Depends(get_key_type),
):
2021-10-12 19:54:35 +01:00
copilot = await get_copilot(copilot_id)
if not copilot:
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="Copilot not found"
2021-10-12 19:54:35 +01:00
)
if not copilot.lnurl_toggle:
return copilot.dict()
2021-10-17 23:19:42 +01:00
return {**copilot.dict(), **{"lnurl": copilot.lnurl(req)}}
2021-10-12 19:54:35 +01:00
2021-10-13 12:37:10 +01:00
@copilot_ext.post("/api/v1/copilot")
@copilot_ext.put("/api/v1/copilot/{juke_id}")
2021-10-13 10:43:11 +01:00
async def api_copilot_create_or_update(
data: CreateCopilotData,
copilot_id: str = Query(None),
wallet: WalletTypeInfo = Depends(require_admin_key),
2021-10-13 10:43:11 +01:00
):
2021-10-13 12:37:10 +01:00
data.user = wallet.wallet.user
data.wallet = wallet.wallet.id
if copilot_id:
2021-10-13 10:43:11 +01:00
copilot = await update_copilot(data, copilot_id=copilot_id)
2021-10-13 12:37:10 +01:00
else:
copilot = await create_copilot(data, inkey=wallet.wallet.inkey)
return copilot
2021-10-13 10:43:11 +01:00
2021-10-13 12:37:10 +01:00
@copilot_ext.delete("/api/v1/copilot/{copilot_id}")
async def api_copilot_delete(
copilot_id: str = Query(None),
wallet: WalletTypeInfo = Depends(require_admin_key),
):
2021-10-12 19:54:35 +01:00
copilot = await get_copilot(copilot_id)
if not copilot:
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="Copilot does not exist"
)
2021-10-12 19:54:35 +01:00
await delete_copilot(copilot_id)
return "", HTTPStatus.NO_CONTENT
2021-10-13 12:37:10 +01:00
@copilot_ext.get("/api/v1/copilot/ws/{copilot_id}/{comment}/{data}")
async def api_copilot_ws_relay(
2021-10-17 18:33:29 +01:00
copilot_id: str = Query(None), comment: str = Query(None), data: str = Query(None)
):
2021-10-12 19:54:35 +01:00
copilot = await get_copilot(copilot_id)
if not copilot:
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="Copilot does not exist"
)
2021-10-12 19:54:35 +01:00
try:
await websocketUpdater(copilot_id, str(data) + "-" + str(comment))
2021-10-12 19:54:35 +01:00
except:
2021-10-17 18:33:29 +01:00
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not your copilot")
return ""