2022-03-07 06:03:32 +01:00
|
|
|
from http import HTTPStatus
|
2022-12-06 10:58:59 +01:00
|
|
|
from typing import Optional
|
2022-03-12 15:18:09 +01:00
|
|
|
|
2022-12-08 14:41:52 +01:00
|
|
|
from fastapi import Body
|
2022-11-24 14:53:11 +01:00
|
|
|
from fastapi.params import 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
|
2022-12-08 14:41:52 +01:00
|
|
|
from lnbits.core.models import AdminSettings, UpdateSettings, User
|
|
|
|
from lnbits.core.services import update_wallet_balance
|
2022-10-04 13:49:32 +02:00
|
|
|
from lnbits.decorators import check_admin
|
2022-10-03 16:36:14 +02:00
|
|
|
from lnbits.server import server_restart
|
2022-03-12 15:18:09 +01:00
|
|
|
|
2022-12-08 14:41:52 +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
|
|
|
|
2022-11-30 16:53:46 +01:00
|
|
|
|
2022-12-08 14:41:52 +01:00
|
|
|
@core_app.get(
|
|
|
|
"/admin/api/v1/restart/",
|
|
|
|
status_code=HTTPStatus.OK,
|
|
|
|
dependencies=[Depends(check_admin)],
|
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
|
|
|
|
2022-12-08 14:41:52 +01:00
|
|
|
@core_app.get("/admin/api/v1/settings/")
|
2022-12-07 11:00:48 +01:00
|
|
|
async def api_get_settings(
|
2022-12-07 11:02:23 +01:00
|
|
|
user: User = Depends(check_admin), # type: ignore
|
2022-12-07 11:00:48 +01:00
|
|
|
) -> Optional[AdminSettings]:
|
|
|
|
admin_settings = await get_admin_settings(user.super_user)
|
2022-12-06 17:08:21 +01:00
|
|
|
return admin_settings
|
2022-10-21 11:13:40 +02:00
|
|
|
|
|
|
|
|
2022-12-08 14:41:52 +01:00
|
|
|
@core_app.put(
|
|
|
|
"/admin/api/v1/topup/",
|
|
|
|
status_code=HTTPStatus.OK,
|
|
|
|
dependencies=[Depends(check_admin)],
|
2022-10-12 13:08:59 +02:00
|
|
|
)
|
2022-12-08 14:41:52 +01: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"}
|
2022-03-07 06:03:32 +01:00
|
|
|
|
|
|
|
|
2022-12-08 14:41:52 +01:00
|
|
|
@core_app.put(
|
|
|
|
"/admin/api/v1/settings/",
|
|
|
|
status_code=HTTPStatus.OK,
|
|
|
|
dependencies=[Depends(check_admin)],
|
2022-10-12 13:08:59 +02:00
|
|
|
)
|
2022-12-06 10:58:59 +01:00
|
|
|
async def api_update_settings(data: UpdateSettings):
|
|
|
|
await update_admin_settings(data)
|
|
|
|
return {"status": "Success"}
|
2022-10-05 09:19:07 +02:00
|
|
|
|
2022-07-05 17:25:02 +02:00
|
|
|
|
2022-12-08 14:41:52 +01:00
|
|
|
@core_app.delete(
|
|
|
|
"/admin/api/v1/settings/",
|
|
|
|
status_code=HTTPStatus.OK,
|
|
|
|
dependencies=[Depends(check_admin)],
|
2022-10-12 13:08:59 +02:00
|
|
|
)
|
|
|
|
async def api_delete_settings() -> dict[str, str]:
|
2022-12-05 15:43:26 +01:00
|
|
|
await delete_admin_settings()
|
2022-03-12 15:18:09 +01:00
|
|
|
return {"status": "Success"}
|