code quality fixes and cleanup
This commit is contained in:
parent
29b1b17967
commit
2bbbcd4d65
32 changed files with 4943 additions and 1368 deletions
174
views_api.py
174
views_api.py
|
|
@ -1,162 +1,178 @@
|
|||
from http import HTTPStatus
|
||||
import json
|
||||
from typing import List, Optional, Dict
|
||||
from .models import NWCRegistrationRequest, NWCGetResponse
|
||||
from fastapi import Depends, Request
|
||||
from lnbits.decorators import (
|
||||
WalletTypeInfo,
|
||||
require_admin_key
|
||||
)
|
||||
from fastapi.responses import JSONResponse
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from . import nwcprovider_ext
|
||||
|
||||
from .crud import get_nwc, get_wallet_nwcs, get_all_config_nwc, create_nwc, delete_nwc, get_budgets_nwc, get_config_nwc,set_config_nwc
|
||||
|
||||
|
||||
from lnbits.decorators import check_admin
|
||||
from fastapi import Depends
|
||||
import secp256k1
|
||||
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
|
||||
|
||||
from .crud import (
|
||||
create_nwc,
|
||||
delete_nwc,
|
||||
get_all_config_nwc,
|
||||
get_budgets_nwc,
|
||||
get_config_nwc,
|
||||
get_nwc,
|
||||
get_wallet_nwcs,
|
||||
set_config_nwc,
|
||||
)
|
||||
from .models import NWCGetResponse, NWCRegistrationRequest
|
||||
from .permission import nwc_permissions
|
||||
|
||||
nwcprovider_api_router = APIRouter()
|
||||
|
||||
|
||||
# Get supported permissions
|
||||
@nwcprovider_ext.get("/api/v1/permissions", status_code=HTTPStatus.OK)
|
||||
@nwcprovider_api_router.get("/api/v1/permissions", status_code=HTTPStatus.OK)
|
||||
async def api_get_permissions(
|
||||
req: Request,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
) -> Dict:
|
||||
return nwc_permissions
|
||||
|
||||
return nwc_permissions
|
||||
|
||||
|
||||
## Get nwc keys associated with the wallet
|
||||
@nwcprovider_ext.get("/api/v1/nwc", status_code=HTTPStatus.OK, response_model=List[NWCGetResponse])
|
||||
@nwcprovider_api_router.get(
|
||||
"/api/v1/nwc", status_code=HTTPStatus.OK, response_model=List[NWCGetResponse]
|
||||
)
|
||||
async def api_get_nwcs(
|
||||
req: Request,
|
||||
includeExpired: Optional[bool] = False,
|
||||
calculateSpendBudget: Optional[bool] = False,
|
||||
include_expired: bool = False,
|
||||
calculate_spent_budget: bool = False,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
):
|
||||
|
||||
|
||||
wallet_id = wallet.wallet.id
|
||||
nwcs = await get_wallet_nwcs(wallet_id, includeExpired)
|
||||
nwcs = await get_wallet_nwcs(wallet_id, include_expired)
|
||||
out = []
|
||||
for nwc in nwcs:
|
||||
budgets = await get_budgets_nwc(nwc.pubkey, calculateSpendBudget)
|
||||
res = NWCGetResponse(
|
||||
data=nwc,
|
||||
budgets=budgets
|
||||
)
|
||||
budgets = await get_budgets_nwc(nwc.pubkey, calculate_spent_budget)
|
||||
res = NWCGetResponse(data=nwc, budgets=budgets)
|
||||
out.append(res)
|
||||
return out
|
||||
|
||||
return out
|
||||
|
||||
|
||||
# Get a nwc key
|
||||
@nwcprovider_ext.get("/api/v1/nwc/{pubkey}", status_code=HTTPStatus.OK, response_model=NWCGetResponse)
|
||||
@nwcprovider_api_router.get(
|
||||
"/api/v1/nwc/{pubkey}", status_code=HTTPStatus.OK, response_model=NWCGetResponse
|
||||
)
|
||||
async def api_get_nwc(
|
||||
req: Request,
|
||||
pubkey: str,
|
||||
includeExpired: Optional[bool] = False,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key)
|
||||
include_expired: Optional[bool] = False,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
) -> NWCGetResponse:
|
||||
wallet_id = wallet.wallet.id
|
||||
nwc = await get_nwc(pubkey, wallet_id, includeExpired)
|
||||
res = NWCGetResponse(
|
||||
data=nwc,
|
||||
budgets=await get_budgets_nwc(pubkey)
|
||||
)
|
||||
nwc = await get_nwc(pubkey, wallet_id, include_expired)
|
||||
if not nwc:
|
||||
raise Exception("Pubkey has no associated wallet")
|
||||
res = NWCGetResponse(data=nwc, budgets=await get_budgets_nwc(pubkey))
|
||||
return res
|
||||
|
||||
|
||||
|
||||
# Get pairing url for given secret
|
||||
@nwcprovider_ext.get("/api/v1/pairing/{secret}", status_code=HTTPStatus.OK, response_model=str)
|
||||
async def api_get_pairing_url(
|
||||
req: Request,
|
||||
secret: str
|
||||
) -> str:
|
||||
pprivkey = await get_config_nwc("provider_key")
|
||||
@nwcprovider_api_router.get(
|
||||
"/api/v1/pairing/{secret}", status_code=HTTPStatus.OK, response_model=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")
|
||||
relay = await get_config_nwc("relay")
|
||||
relay_alias = await get_config_nwc("relay_alias")
|
||||
if not relay:
|
||||
raise Exception("Extension is not configured")
|
||||
relay_alias: Optional[str] = await get_config_nwc("relay_alias")
|
||||
if relay_alias:
|
||||
relay = relay_alias
|
||||
else:
|
||||
if relay == "nostrclient":
|
||||
scheme = req.url.scheme # http or https
|
||||
netloc = req.url.netloc # hostname and port
|
||||
if scheme=="http":
|
||||
if scheme == "http":
|
||||
scheme = "ws"
|
||||
else:
|
||||
scheme = "wss"
|
||||
netloc += "/nostrclient/api/v1/relay"
|
||||
relay = f"{scheme}://{netloc}"
|
||||
relay = f"{scheme}://{netloc}"
|
||||
psk = secp256k1.PrivateKey(bytes.fromhex(pprivkey))
|
||||
ppk = psk.pubkey
|
||||
if not ppk:
|
||||
raise Exception("Error generating pubkey")
|
||||
ppubkey = ppk.serialize().hex()[2:]
|
||||
url = "nostr+walletconnect://"
|
||||
url += ppubkey
|
||||
url += "?relay="+relay
|
||||
url += "&secret="+secret
|
||||
#lud16=?
|
||||
url += "?relay=" + relay
|
||||
url += "&secret=" + secret
|
||||
# lud16=?
|
||||
return url
|
||||
|
||||
|
||||
## Register a new nwc key
|
||||
@nwcprovider_ext.put("/api/v1/nwc/{pubkey}", status_code=HTTPStatus.CREATED, response_model=NWCGetResponse)
|
||||
@nwcprovider_api_router.put(
|
||||
"/api/v1/nwc/{pubkey}",
|
||||
status_code=HTTPStatus.CREATED,
|
||||
response_model=NWCGetResponse,
|
||||
)
|
||||
async def api_register_nwc(
|
||||
req: Request,
|
||||
pubkey: str,
|
||||
req: Request,
|
||||
pubkey: str,
|
||||
registration_data: NWCRegistrationRequest, # Use the Pydantic model here
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key)
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
):
|
||||
wallet_id = wallet.wallet.id
|
||||
nwc = await create_nwc(pubkey, wallet_id, registration_data.description, registration_data.expires_at, registration_data.permissions, registration_data.budgets)
|
||||
budgets = await get_budgets_nwc(pubkey)
|
||||
res = NWCGetResponse(
|
||||
data=nwc,
|
||||
budgets=budgets
|
||||
nwc = await create_nwc(
|
||||
pubkey,
|
||||
wallet_id,
|
||||
registration_data.description,
|
||||
registration_data.expires_at,
|
||||
registration_data.permissions,
|
||||
registration_data.budgets,
|
||||
)
|
||||
budgets = await get_budgets_nwc(pubkey)
|
||||
res = NWCGetResponse(data=nwc, budgets=budgets)
|
||||
return res
|
||||
|
||||
|
||||
# Delete a nwc key
|
||||
@nwcprovider_ext.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(
|
||||
req: Request,
|
||||
pubkey: str,
|
||||
wallet: WalletTypeInfo=Depends(require_admin_key)
|
||||
req: Request, pubkey: str, wallet: WalletTypeInfo = Depends(require_admin_key)
|
||||
):
|
||||
wallet_id = wallet.wallet.id
|
||||
await delete_nwc(pubkey, wallet_id)
|
||||
return JSONResponse(content={"message": f"NWC key {pubkey} deleted successfully."})
|
||||
|
||||
|
||||
|
||||
|
||||
# Get config
|
||||
@nwcprovider_ext.get("/api/v1/config", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)])
|
||||
@nwcprovider_api_router.get(
|
||||
"/api/v1/config", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
|
||||
)
|
||||
async def api_get_all_config_nwc(
|
||||
req: Request,
|
||||
req: Request,
|
||||
):
|
||||
config = await get_all_config_nwc()
|
||||
return config
|
||||
|
||||
|
||||
# Get config
|
||||
@nwcprovider_ext.get("/api/v1/config/{key}", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)])
|
||||
async def api_get_config_nwc(
|
||||
req: Request,
|
||||
key:str
|
||||
):
|
||||
@nwcprovider_api_router.get(
|
||||
"/api/v1/config/{key}",
|
||||
status_code=HTTPStatus.OK,
|
||||
dependencies=[Depends(check_admin)],
|
||||
)
|
||||
async def api_get_config_nwc(req: Request, key: str):
|
||||
config = await get_config_nwc(key)
|
||||
out = {}
|
||||
out[key] = config
|
||||
return out
|
||||
|
||||
|
||||
|
||||
# Set config
|
||||
@nwcprovider_ext.post("/api/v1/config", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)])
|
||||
async def api_set_config_nwc(
|
||||
req: Request
|
||||
):
|
||||
@nwcprovider_api_router.post(
|
||||
"/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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue