format
This commit is contained in:
parent
ac69ae9846
commit
44d3e28182
11 changed files with 630 additions and 638 deletions
99
views_api.py
99
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, Query
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from fastapi.responses import JSONResponse
|
||||
from lnbits.core.models import WalletTypeInfo
|
||||
from lnbits.decorators import check_admin, require_admin_key
|
||||
|
|
@ -18,35 +18,34 @@ from .crud import (
|
|||
set_config_nwc,
|
||||
)
|
||||
from .models import (
|
||||
NWCRegistrationRequest,
|
||||
GetBudgetsNWC,
|
||||
GetWalletNWC,
|
||||
NWCGetResponse,
|
||||
CreateNWCKey,
|
||||
DeleteNWC,
|
||||
GetBudgetsNWC,
|
||||
GetNWC,
|
||||
GetBudgetsNWC
|
||||
GetWalletNWC,
|
||||
NWCGetResponse,
|
||||
NWCRegistrationRequest,
|
||||
)
|
||||
from .paranoia import (
|
||||
assert_boolean,
|
||||
assert_sane_string,
|
||||
assert_valid_pubkey,
|
||||
assert_valid_wallet_id,
|
||||
)
|
||||
from .permission import nwc_permissions
|
||||
from .paranoia import assert_valid_wallet_id, assert_valid_pubkey, assert_sane_string, assert_boolean
|
||||
|
||||
nwcprovider_api_router = APIRouter()
|
||||
|
||||
|
||||
# Get supported permissions
|
||||
@nwcprovider_api_router.get(
|
||||
"/api/v1/permissions",
|
||||
status_code=HTTPStatus.OK
|
||||
)
|
||||
@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(
|
||||
include_expired: bool = False,
|
||||
|
|
@ -59,18 +58,14 @@ async def api_get_nwcs(
|
|||
assert_valid_wallet_id(wallet_id)
|
||||
assert_boolean(include_expired)
|
||||
assert_boolean(calculate_spent_budget)
|
||||
# ## #
|
||||
# ## #
|
||||
|
||||
wallet_nwcs = GetWalletNWC(
|
||||
wallet=wallet_id,
|
||||
include_expired=include_expired
|
||||
)
|
||||
wallet_nwcs = GetWalletNWC(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=calculate_spent_budget
|
||||
pubkey=nwc.pubkey, calculate_spent=calculate_spent_budget
|
||||
)
|
||||
budgets = await get_budgets_nwc(budgets_nwc)
|
||||
res = NWCGetResponse(data=nwc, budgets=budgets)
|
||||
|
|
@ -80,14 +75,12 @@ 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(
|
||||
pubkey: str,
|
||||
include_expired: bool = False,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key)
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
) -> NWCGetResponse:
|
||||
wallet_id = wallet.wallet.id
|
||||
|
||||
|
|
@ -97,28 +90,23 @@ async def api_get_nwc(
|
|||
assert_valid_wallet_id(wallet_id)
|
||||
# ## #
|
||||
|
||||
nwc = await get_nwc(GetNWC(pubkey=pubkey, wallet=wallet_id, include_expired=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(
|
||||
GetBudgetsNWC(
|
||||
pubkey=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:
|
||||
|
||||
# hardening #
|
||||
assert_sane_string(secret)
|
||||
# ## #
|
||||
|
|
@ -163,7 +151,7 @@ async def api_get_pairing_url(
|
|||
)
|
||||
async def api_register_nwc(
|
||||
pubkey: str,
|
||||
data: NWCRegistrationRequest,
|
||||
data: NWCRegistrationRequest,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
):
|
||||
wallet_id = wallet.wallet.id
|
||||
|
|
@ -183,23 +171,15 @@ async def api_register_nwc(
|
|||
budgets=data.budgets,
|
||||
)
|
||||
)
|
||||
budgets = await get_budgets_nwc(
|
||||
GetBudgetsNWC(
|
||||
pubkey=pubkey
|
||||
)
|
||||
)
|
||||
budgets = await get_budgets_nwc(GetBudgetsNWC(pubkey=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(
|
||||
pubkey: str,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key)
|
||||
pubkey: str, wallet: WalletTypeInfo = Depends(require_admin_key)
|
||||
):
|
||||
wallet_id = wallet.wallet.id
|
||||
|
||||
|
|
@ -208,20 +188,13 @@ async def api_delete_nwc(
|
|||
assert_valid_wallet_id(wallet_id)
|
||||
# ## #
|
||||
|
||||
await delete_nwc(DeleteNWC(
|
||||
pubkey=pubkey,
|
||||
wallet=wallet_id
|
||||
))
|
||||
return JSONResponse(
|
||||
content={"message": f"NWC key {pubkey} deleted successfully."}
|
||||
)
|
||||
await delete_nwc(DeleteNWC(pubkey=pubkey, wallet=wallet_id))
|
||||
return JSONResponse(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()
|
||||
|
|
@ -243,9 +216,7 @@ 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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue