lnbits-legend/lnbits/core/views/admin_api.py

75 lines
2.0 KiB
Python
Raw Normal View History

2022-03-07 06:03:32 +01:00
from http import HTTPStatus
from typing import Optional
2022-03-12 15:18:09 +01:00
from fastapi import Body, Depends
2022-03-12 15:18:09 +01:00
from starlette.exceptions import HTTPException
2022-03-07 06:03:32 +01:00
from lnbits.core.crud import get_wallet
from lnbits.core.models import User
from lnbits.core.services import update_cached_settings, update_wallet_balance
from lnbits.decorators import check_admin, check_super_user
2022-10-03 16:36:14 +02:00
from lnbits.server import server_restart
2022-12-19 09:10:41 +01:00
from lnbits.settings import AdminSettings, EditableSettings
2022-03-12 15:18:09 +01:00
from .. import core_app
from ..crud import delete_admin_settings, get_admin_settings, update_admin_settings
2022-10-03 16:36:14 +02:00
@core_app.get("/admin/api/v1/settings/")
async def api_get_settings(
user: User = Depends(check_admin),
) -> Optional[AdminSettings]:
admin_settings = await get_admin_settings(user.super_user)
return admin_settings
@core_app.put(
"/admin/api/v1/settings/",
status_code=HTTPStatus.OK,
dependencies=[Depends(check_admin)],
)
2022-12-19 09:10:41 +01:00
async def api_update_settings(data: EditableSettings):
await update_admin_settings(data)
update_cached_settings(dict(data))
return {"status": "Success"}
@core_app.delete(
"/admin/api/v1/settings/",
status_code=HTTPStatus.OK,
dependencies=[Depends(check_super_user)],
)
async def api_delete_settings() -> None:
await delete_admin_settings()
server_restart.set()
@core_app.get(
"/admin/api/v1/restart/",
status_code=HTTPStatus.OK,
dependencies=[Depends(check_super_user)],
2022-10-12 13:08:59 +02:00
)
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 15:18:09 +01:00
2022-03-07 06:03:32 +01:00
@core_app.put(
"/admin/api/v1/topup/",
status_code=HTTPStatus.OK,
dependencies=[Depends(check_super_user)],
2022-10-12 13:08:59 +02:00
)
async def api_topup_balance(
2022-10-12 13:08:59 +02:00
id: str = Body(...), amount: int = Body(...)
) -> dict[str, str]:
2022-03-07 06:03:32 +01:00
try:
2022-10-12 13:08:59 +02:00
await get_wallet(id)
2022-03-07 06:03:32 +01:00
except:
2022-03-12 15:18:09 +01:00
raise HTTPException(
2022-10-05 09:19:07 +02:00
status_code=HTTPStatus.FORBIDDEN, detail="wallet does not exist."
2022-07-05 17:25:02 +02:00
)
2022-03-22 12:42:28 +01:00
2022-10-12 13:08:59 +02:00
await update_wallet_balance(wallet_id=id, amount=int(amount))
2022-07-05 17:25:02 +02:00
2022-03-12 15:18:09 +01:00
return {"status": "Success"}