fix models
This commit is contained in:
parent
4a8303b049
commit
0518e6f7d0
4 changed files with 137 additions and 117 deletions
107
views_api.py
107
views_api.py
|
|
@ -2,7 +2,7 @@ from http import HTTPStatus
|
|||
from typing import Dict, List, Optional
|
||||
|
||||
import secp256k1
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from fastapi import APIRouter, Depends, Request, Query
|
||||
from fastapi.responses import JSONResponse
|
||||
from lnbits.core.models import WalletTypeInfo
|
||||
from lnbits.decorators import check_admin, require_admin_key
|
||||
|
|
@ -18,13 +18,14 @@ from .crud import (
|
|||
set_config_nwc,
|
||||
)
|
||||
from .models import (
|
||||
DeleteNWC,
|
||||
NWCRegistrationRequest,
|
||||
GetBudgetsNWC,
|
||||
GetNWCKey,
|
||||
GetNWCs,
|
||||
GetWalletNWC,
|
||||
NWCGetResponse,
|
||||
RegisterNWC,
|
||||
CreateNWCKey,
|
||||
DeleteNWC,
|
||||
GetNWC,
|
||||
GetBudgetsNWC
|
||||
)
|
||||
from .permission import nwc_permissions
|
||||
|
||||
|
|
@ -32,31 +33,36 @@ nwcprovider_api_router = APIRouter()
|
|||
|
||||
|
||||
# Get supported permissions
|
||||
@nwcprovider_api_router.get("/api/v1/permissions", status_code=HTTPStatus.OK)
|
||||
async def api_get_permissions(
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
) -> Dict:
|
||||
@nwcprovider_api_router.get(
|
||||
"/api/v1/permissions",
|
||||
status_code=HTTPStatus.OK
|
||||
)
|
||||
async def api_get_permissions() -> Dict:
|
||||
return nwc_permissions
|
||||
|
||||
|
||||
## Get nwc keys associated with the wallet
|
||||
@nwcprovider_api_router.get(
|
||||
"/api/v1/nwc", status_code=HTTPStatus.OK, response_model=List[NWCGetResponse]
|
||||
"/api/v1/nwc",
|
||||
status_code=HTTPStatus.OK,
|
||||
response_model=List[NWCGetResponse]
|
||||
)
|
||||
async def api_get_nwcs(
|
||||
data: GetNWCs,
|
||||
include_expired: bool = False,
|
||||
calculate_spent_budget: bool = False,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
):
|
||||
|
||||
wallet_id = wallet.wallet.id
|
||||
wallet_nwcs = GetWalletNWC(
|
||||
wallet_id=wallet_id, include_expired=data.include_expired
|
||||
wallet=wallet_id,
|
||||
include_expired=include_expired
|
||||
)
|
||||
nwcs = await get_wallet_nwcs(wallet_nwcs)
|
||||
out = []
|
||||
for nwc in nwcs:
|
||||
budgets_nwc = GetBudgetsNWC(
|
||||
pubkey=nwc.pubkey, calculate_spent=data.calculate_spent_budget
|
||||
pubkey=nwc.pubkey,
|
||||
calculate_spent=calculate_spent_budget
|
||||
)
|
||||
budgets = await get_budgets_nwc(budgets_nwc)
|
||||
res = NWCGetResponse(data=nwc, budgets=budgets)
|
||||
|
|
@ -66,24 +72,37 @@ async def api_get_nwcs(
|
|||
|
||||
# Get a nwc key
|
||||
@nwcprovider_api_router.get(
|
||||
"/api/v1/nwc/{pubkey}", status_code=HTTPStatus.OK, response_model=NWCGetResponse
|
||||
"/api/v1/nwc/{pubkey}",
|
||||
status_code=HTTPStatus.OK,
|
||||
response_model=NWCGetResponse
|
||||
)
|
||||
async def api_get_nwc(
|
||||
data: GetNWCKey, wallet: WalletTypeInfo = Depends(require_admin_key)
|
||||
pubkey: str,
|
||||
include_expired: Optional[bool] = False,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key)
|
||||
) -> NWCGetResponse:
|
||||
wallet_id = wallet.wallet.id
|
||||
nwc = await get_nwc(data.pubkey, wallet_id, data.include_expired)
|
||||
nwc = await get_nwc(GetNWC(pubkey=pubkey, wallet=wallet_id, include_expired=include_expired))
|
||||
if not nwc:
|
||||
raise Exception("Pubkey has no associated wallet")
|
||||
res = NWCGetResponse(data=nwc, budgets=await get_budgets_nwc(data.pubkey))
|
||||
res = NWCGetResponse(data=nwc, budgets=await get_budgets_nwc(
|
||||
GetBudgetsNWC(
|
||||
pubkey=pubkey
|
||||
)
|
||||
))
|
||||
return res
|
||||
|
||||
|
||||
# Get pairing url for given secret
|
||||
@nwcprovider_api_router.get(
|
||||
"/api/v1/pairing/{secret}", status_code=HTTPStatus.OK, response_model=str
|
||||
"/api/v1/pairing/{secret}",
|
||||
status_code=HTTPStatus.OK,
|
||||
response_model=str
|
||||
)
|
||||
async def api_get_pairing_url(req: Request, secret: str) -> str:
|
||||
async def api_get_pairing_url(
|
||||
req: Request,
|
||||
secret: str
|
||||
) -> str:
|
||||
pprivkey: Optional[str] = await get_config_nwc("provider_key")
|
||||
if not pprivkey:
|
||||
raise Exception("Extension is not configured")
|
||||
|
|
@ -123,38 +142,54 @@ async def api_get_pairing_url(req: Request, secret: str) -> str:
|
|||
response_model=NWCGetResponse,
|
||||
)
|
||||
async def api_register_nwc(
|
||||
data: RegisterNWC, # Use the Pydantic model here
|
||||
pubkey: str,
|
||||
data: NWCRegistrationRequest,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
):
|
||||
wallet_id = wallet.wallet.id
|
||||
nwc = await create_nwc(
|
||||
data.pubkey,
|
||||
wallet_id,
|
||||
data.registration_data.description,
|
||||
data.registration_data.expires_at,
|
||||
data.registration_data.permissions,
|
||||
data.registration_data.budgets,
|
||||
CreateNWCKey(
|
||||
pubkey=pubkey,
|
||||
wallet=wallet_id,
|
||||
description=data.description,
|
||||
expires_at=data.expires_at,
|
||||
permissions=data.permissions,
|
||||
budgets=data.budgets,
|
||||
)
|
||||
)
|
||||
budgets = await get_budgets_nwc(
|
||||
GetBudgetsNWC(
|
||||
pubkey=pubkey
|
||||
)
|
||||
)
|
||||
budgets = await get_budgets_nwc(data.pubkey)
|
||||
res = NWCGetResponse(data=nwc, budgets=budgets)
|
||||
return res
|
||||
|
||||
|
||||
# Delete a nwc key
|
||||
@nwcprovider_api_router.delete("/api/v1/nwc/{pubkey}", status_code=HTTPStatus.OK)
|
||||
@nwcprovider_api_router.delete(
|
||||
"/api/v1/nwc/{pubkey}",
|
||||
status_code=HTTPStatus.OK
|
||||
)
|
||||
async def api_delete_nwc(
|
||||
data: DeleteNWC, wallet: WalletTypeInfo = Depends(require_admin_key)
|
||||
pubkey: str,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key)
|
||||
):
|
||||
wallet_id = wallet.wallet.id
|
||||
await delete_nwc(data.pubkey, wallet_id)
|
||||
await delete_nwc(DeleteNWC(
|
||||
pubkey=pubkey,
|
||||
wallet=wallet_id
|
||||
))
|
||||
return JSONResponse(
|
||||
content={"message": f"NWC key {data.pubkey} deleted successfully."}
|
||||
content={"message": f"NWC key {pubkey} deleted successfully."}
|
||||
)
|
||||
|
||||
|
||||
# Get config
|
||||
@nwcprovider_api_router.get(
|
||||
"/api/v1/config", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
|
||||
"/api/v1/config",
|
||||
status_code=HTTPStatus.OK,
|
||||
dependencies=[Depends(check_admin)]
|
||||
)
|
||||
async def api_get_all_config_nwc():
|
||||
config = await get_all_config_nwc()
|
||||
|
|
@ -176,10 +211,12 @@ async def api_get_config_nwc(key: str):
|
|||
|
||||
# Set config
|
||||
@nwcprovider_api_router.post(
|
||||
"/api/v1/config", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
|
||||
"/api/v1/config",
|
||||
status_code=HTTPStatus.OK,
|
||||
dependencies=[Depends(check_admin)]
|
||||
)
|
||||
async def api_set_config_nwc(req: Request):
|
||||
data = await req.json()
|
||||
for key, value in data.items():
|
||||
await set_config_nwc(key, value)
|
||||
return await api_get_all_config_nwc(req)
|
||||
return await api_get_all_config_nwc()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue