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

58 lines
1.9 KiB
Python
Raw Normal View History

2022-02-08 10:35:20 +00:00
from http import HTTPStatus
import asyncio
from fastapi import Request
from fastapi.param_functions import Query
from fastapi.params import Depends
from starlette.exceptions import HTTPException
from lnbits.core.crud import get_user
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
from lnbits.utils.exchange_rates import currencies
from lnbits.settings import LNBITS_ADMIN_USERS
2022-02-08 13:13:36 +00:00
from . import nostradmin_ext
2022-02-08 10:35:20 +00:00
from .crud import (
create_nostrkeys,
get_nostrkeys,
create_nostrnotes,
get_nostrnotes,
create_nostrrelays,
get_nostrrelays,
get_nostrrelaylist,
2022-02-08 13:13:36 +00:00
update_nostrrelaysetlist,
2022-02-08 10:35:20 +00:00
create_nostrconnections,
get_nostrconnections,
)
2022-02-08 13:13:36 +00:00
from .models import nostrKeys, nostrCreateRelays, nostrRelaySetList
2022-02-08 10:35:20 +00:00
2022-02-08 13:13:36 +00:00
@nostradmin_ext.get("/api/v1/relays")
2022-02-08 10:35:20 +00:00
async def api_relays_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)):
relays = await get_nostrrelays()
if not relays:
await create_nostrrelays(nostrCreateRelays(relay="wss://relayer.fiatjaf.com"))
await create_nostrrelays(
nostrCreateRelays(relay="wss://nostr-pub.wellorder.net")
)
relays = await get_nostrrelays()
try:
return [{**relays.dict()} for relays in await relays]
except:
None
2022-02-08 13:13:36 +00:00
@nostradmin_ext.get("/api/v1/relaylist")
2022-02-08 10:35:20 +00:00
async def api_relaylist(wallet: WalletTypeInfo = Depends(get_key_type)):
if wallet.wallet.user not in LNBITS_ADMIN_USERS:
raise HTTPException(
status_code=HTTPStatus.UNAUTHORIZED, detail="User not authorized."
)
return await get_nostrrelaylist()
2022-02-08 13:13:36 +00:00
@nostradmin_ext.post("/api/v1/setlist")
async def api_relayssetlist(data: nostrRelaySetList, wallet: WalletTypeInfo = Depends(get_key_type)):
2022-02-08 10:35:20 +00:00
if wallet.wallet.user not in LNBITS_ADMIN_USERS:
raise HTTPException(
status_code=HTTPStatus.UNAUTHORIZED, detail="User not authorized."
)
2022-02-08 13:13:36 +00:00
return await update_nostrrelaysetlist(data)