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

64 lines
1.8 KiB
Python
Raw Normal View History

2022-03-07 05:03:32 +00:00
from http import HTTPStatus
2022-03-12 14:18:09 +00:00
2022-11-24 14:53:11 +01:00
from fastapi import Body
from fastapi.params import Depends
2022-03-12 14:18:09 +00:00
from starlette.exceptions import HTTPException
2022-03-07 05:03:32 +00:00
from lnbits.core.crud import get_wallet
from lnbits.decorators import check_admin
2022-03-12 14:18:09 +00:00
from lnbits.extensions.admin import admin_ext
2022-12-05 15:43:26 +01:00
from lnbits.extensions.admin.models import AdminSettings, UpdateSettings
2022-10-03 16:36:14 +02:00
from lnbits.server import server_restart
2022-03-12 14:18:09 +00:00
2022-12-05 15:43:26 +01:00
from .crud import delete_admin_settings, get_admin_settings, update_admin_settings, update_wallet_balance
2022-10-03 16:36:14 +02:00
2022-10-12 13:08:59 +02:00
@admin_ext.get(
"/api/v1/restart/", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
)
async def api_restart_server() -> dict[str, str]:
2022-10-03 16:36:14 +02:00
server_restart.set()
return {"status": "Success"}
2022-03-12 14:18:09 +00:00
2022-03-07 05:03:32 +00:00
@admin_ext.get("/api/v1/settings/", dependencies=[Depends(check_admin)])
2022-12-05 15:43:26 +01:00
async def api_get_settings() -> AdminSettings:
return await get_admin_settings()
2022-10-12 13:08:59 +02:00
@admin_ext.put(
"/api/v1/topup/", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
)
2022-07-05 16:25:02 +01:00
async def api_update_balance(
2022-10-12 13:08:59 +02:00
id: str = Body(...), amount: int = Body(...)
) -> dict[str, str]:
2022-03-07 05:03:32 +00:00
try:
2022-10-12 13:08:59 +02:00
await get_wallet(id)
2022-03-07 05:03:32 +00:00
except:
2022-03-12 14:18:09 +00:00
raise HTTPException(
2022-10-05 09:19:07 +02:00
status_code=HTTPStatus.FORBIDDEN, detail="wallet does not exist."
2022-07-05 16:25:02 +01:00
)
2022-03-22 11:42:28 +00:00
2022-10-12 13:08:59 +02:00
await update_wallet_balance(wallet_id=id, amount=int(amount))
2022-07-05 16:25:02 +01:00
2022-03-12 14:18:09 +00:00
return {"status": "Success"}
2022-03-07 05:03:32 +00:00
2022-10-12 13:08:59 +02:00
@admin_ext.put(
"/api/v1/settings/", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
)
2022-10-05 09:19:07 +02:00
async def api_update_settings(
data: UpdateSettings = Body(...),
2022-07-05 16:25:02 +01:00
):
2022-12-05 15:43:26 +01:00
settings = await update_admin_settings(data)
2022-11-24 14:53:11 +01:00
if settings:
return {"status": "Success", "settings": settings.dict()}
2022-10-05 09:19:07 +02:00
2022-07-05 16:25:02 +01:00
2022-10-12 13:08:59 +02:00
@admin_ext.delete(
"/api/v1/settings/", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
)
async def api_delete_settings() -> dict[str, str]:
2022-12-05 15:43:26 +01:00
await delete_admin_settings()
2022-03-12 14:18:09 +00:00
return {"status": "Success"}