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

61 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
from fastapi import Body, Depends, Request
2022-10-05 09:19:07 +02:00
from loguru import logger
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
2022-10-03 16:36:14 +02:00
from lnbits.core.models import User
from lnbits.decorators import check_admin
2022-03-12 14:18:09 +00:00
from lnbits.extensions.admin import admin_ext
from lnbits.extensions.admin.models import UpdateSettings
2022-03-22 11:42:28 +00:00
from lnbits.requestvars import g
2022-10-03 16:36:14 +02:00
from lnbits.server import server_restart
from lnbits.settings import settings
2022-03-12 14:18:09 +00:00
2022-10-05 09:19:07 +02:00
from .crud import delete_settings, update_settings, update_wallet_balance
2022-10-03 16:36:14 +02:00
2022-10-05 09:19:07 +02:00
@admin_ext.get("/api/v1/restart/", status_code=HTTPStatus.OK)
async def api_restart_server(user: User = Depends(check_admin)):
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
2022-10-05 09:19:07 +02:00
@admin_ext.put("/api/v1/topup/", status_code=HTTPStatus.OK)
2022-07-05 16:25:02 +01:00
async def api_update_balance(
wallet_id, topup_amount: int, user: User = Depends(check_admin)
2022-07-05 16:25:02 +01:00
):
2022-03-07 05:03:32 +00:00
try:
wallet = await get_wallet(wallet_id)
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-03-22 10:28:44 +00:00
await update_wallet_balance(wallet_id=wallet_id, amount=int(topup_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-05 09:19:07 +02:00
@admin_ext.put("/api/v1/settings/", status_code=HTTPStatus.OK)
async def api_update_settings(
user: User = Depends(check_admin),
data: UpdateSettings = Body(...),
2022-07-05 16:25:02 +01:00
):
2022-10-05 09:19:07 +02:00
await update_settings(data)
return {"status": "Success"}
2022-07-05 16:25:02 +01:00
2022-10-05 09:19:07 +02:00
@admin_ext.delete("/api/v1/settings/", status_code=HTTPStatus.OK)
async def api_delete_settings(
user: User = Depends(check_admin),
):
await delete_settings()
2022-03-12 14:18:09 +00:00
return {"status": "Success"}
2022-10-05 09:19:07 +02:00
@admin_ext.get("/api/v1/backup/", status_code=HTTPStatus.OK)
async def api_backup(user: User = Depends(check_admin)):
return {"status": "not implemented"}