Merge pull request #403 from arcbtc/FastAPI

Added some IF EXISTS
This commit is contained in:
Arc
2021-11-09 18:09:18 +00:00
committed by GitHub
3 changed files with 23 additions and 14 deletions

View File

@@ -29,7 +29,7 @@ Using this wallet requires the installation of the `pylightning` Python package.
### LND (gRPC) ### LND (gRPC)
Using this wallet requires the installation of the `grpcio` Python packages. Using this wallet requires the installation of the `grpcio` and `protobuf` Python packages.
- `LNBITS_BACKEND_WALLET_CLASS`: **LndWallet** - `LNBITS_BACKEND_WALLET_CLASS`: **LndWallet**
- `LND_GRPC_ENDPOINT`: ip_address - `LND_GRPC_ENDPOINT`: ip_address

View File

@@ -47,16 +47,22 @@ from ..tasks import api_invoice_listeners
@core_app.get("/api/v1/wallet") @core_app.get("/api/v1/wallet")
async def api_wallet(wallet: WalletTypeInfo = Depends(get_key_type)): async def api_wallet(wallet: WalletTypeInfo = Depends(get_key_type)):
return { if wallet.wallet_type == 0:
"id": wallet.wallet.id, return {
"name": wallet.wallet.name, "id": wallet.wallet.id,
"balance": wallet.wallet.balance_msat, "name": wallet.wallet.name,
} "balance": wallet.wallet.balance_msat,
}
else:
return {
"name": wallet.wallet.name,
"balance": wallet.wallet.balance_msat,
}
@core_app.put("/api/v1/wallet/{new_name}") @core_app.put("/api/v1/wallet/{new_name}")
async def api_update_wallet( async def api_update_wallet(
new_name: str, wallet: WalletTypeInfo = Depends(get_key_type) new_name: str, wallet: WalletTypeInfo = Depends(WalletAdminKeyChecker())
): ):
await update_wallet(wallet.wallet.id, new_name) await update_wallet(wallet.wallet.id, new_name)
return { return {
@@ -193,7 +199,6 @@ async def api_payments_create(
wallet: WalletTypeInfo = Depends(get_key_type), wallet: WalletTypeInfo = Depends(get_key_type),
invoiceData: CreateInvoiceData = Body(...), invoiceData: CreateInvoiceData = Body(...),
): ):
if wallet.wallet_type < 0 or wallet.wallet_type > 2: if wallet.wallet_type < 0 or wallet.wallet_type > 2:
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Key is invalid") raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Key is invalid")
@@ -315,7 +320,7 @@ async def subscribe(request: Request, wallet: Wallet):
@core_app.get("/api/v1/payments/sse") @core_app.get("/api/v1/payments/sse")
async def api_payments_sse( async def api_payments_sse(
request: Request, wallet: WalletTypeInfo = Depends(get_key_type) request: Request, wallet: WalletTypeInfo = Depends(WalletAdminKeyChecker())
): ):
return EventSourceResponse( return EventSourceResponse(
subscribe(request, wallet), ping=20, media_type="text/event-stream" subscribe(request, wallet), ping=20, media_type="text/event-stream"

View File

@@ -1,7 +1,10 @@
imports_ok = True
try: try:
from google import protobuf
import grpc import grpc
except ImportError: # pragma: nocover except ImportError: # pragma: nocover
grpc = None imports_ok = False
import binascii import binascii
import base64 import base64
@@ -9,8 +12,9 @@ import hashlib
from os import environ, error, getenv from os import environ, error, getenv
from typing import Optional, Dict, AsyncGenerator from typing import Optional, Dict, AsyncGenerator
import lnbits.wallets.lnd_grpc_files.lightning_pb2 as ln if imports_ok:
import lnbits.wallets.lnd_grpc_files.lightning_pb2_grpc as lnrpc import lnbits.wallets.lnd_grpc_files.lightning_pb2 as ln
import lnbits.wallets.lnd_grpc_files.lightning_pb2_grpc as lnrpc
from .base import ( from .base import (
StatusResponse, StatusResponse,
@@ -76,9 +80,9 @@ environ["GRPC_SSL_CIPHER_SUITES"] = "HIGH+ECDSA"
class LndWallet(Wallet): class LndWallet(Wallet):
def __init__(self): def __init__(self):
if grpc is None: # pragma: nocover if not imports_ok: # pragma: nocover
raise ImportError( raise ImportError(
"The `grpcio` library must be installed to use `GRPC LndWallet`. Alternatively try using the LndRESTWallet." "The `grpcio` and `protobuf` library must be installed to use `GRPC LndWallet`. Alternatively try using the LndRESTWallet."
) )
endpoint = getenv("LND_GRPC_ENDPOINT") endpoint = getenv("LND_GRPC_ENDPOINT")