From 63d02426859065722856bce99403a95fa16ca97f Mon Sep 17 00:00:00 2001 From: Stefan Stammberger Date: Sat, 11 Sep 2021 11:02:48 +0200 Subject: [PATCH] fix: more return types --- lnbits/core/views/api.py | 6 +++--- lnbits/core/views/generic.py | 2 +- lnbits/core/views/public_api.py | 23 ++++++++++++++++------- lnbits/tasks.py | 4 +++- lnbits/wallets/lnpay.py | 6 ++++-- lnbits/wallets/opennode.py | 10 +++++++--- 6 files changed, 34 insertions(+), 17 deletions(-) diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index 9aea5d80..d28d5eb2 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -295,14 +295,14 @@ async def api_payment(payment_hash, wallet: WalletTypeInfo = Depends(get_key_typ payment = await wallet.wallet.get_payment(payment_hash) if not payment: - return {"message": "Payment does not exist."}, HTTPStatus.NOT_FOUND + return {"message": "Payment does not exist."} elif not payment.pending: - return {"paid": True, "preimage": payment.preimage}, HTTPStatus.OK + return {"paid": True, "preimage": payment.preimage} try: await payment.check_pending() except Exception: - return {"paid": False}, HTTPStatus.OK + return {"paid": False} return {"paid": not payment.pending, "preimage": payment.preimage} diff --git a/lnbits/core/views/generic.py b/lnbits/core/views/generic.py index 2e041802..d7c95050 100644 --- a/lnbits/core/views/generic.py +++ b/lnbits/core/views/generic.py @@ -204,7 +204,7 @@ async def lnurlwallet(request: Request): async def manifest(usr: str): user = await get_user(usr) if not user: - return "", HTTPStatus.NOT_FOUND + raise HTTPException(status_code=HTTPStatus.NOT_FOUND) return { "short_name": "LNbits", diff --git a/lnbits/core/views/public_api.py b/lnbits/core/views/public_api.py index 02758521..70f949dc 100644 --- a/lnbits/core/views/public_api.py +++ b/lnbits/core/views/public_api.py @@ -1,7 +1,7 @@ import asyncio import datetime from http import HTTPStatus - +from fastapi import HTTPException from lnbits import bolt11 from .. import core_app @@ -14,17 +14,23 @@ async def api_public_payment_longpolling(payment_hash): payment = await get_standalone_payment(payment_hash) if not payment: - return {"message": "Payment does not exist."}, HTTPStatus.NOT_FOUND + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, + detail="Payment does not exist." + ) elif not payment.pending: - return {"status": "paid"}, HTTPStatus.OK + return {"status": "paid"} try: invoice = bolt11.decode(payment.bolt11) expiration = datetime.datetime.fromtimestamp(invoice.date + invoice.expiry) if expiration < datetime.datetime.now(): - return {"status": "expired"}, HTTPStatus.OK + return {"status": "expired"} except: - return {"message": "Invalid bolt11 invoice."}, HTTPStatus.BAD_REQUEST + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, + detail="Invalid bolt11 invoice." + ) payment_queue = asyncio.Queue(0) @@ -37,7 +43,7 @@ async def api_public_payment_longpolling(payment_hash): async for payment in payment_queue.get(): if payment.payment_hash == payment_hash: nonlocal response - response = ({"status": "paid"}, HTTPStatus.OK) + response = {"status": "paid"} cancel_scope.cancel() async def timeouter(cancel_scope): @@ -51,4 +57,7 @@ async def api_public_payment_longpolling(payment_hash): if response: return response else: - return {"message": "timeout"}, HTTPStatus.REQUEST_TIMEOUT + raise HTTPException( + status_code=HTTPStatus.REQUEST_TIMEOUT, + detail="timeout" + ) diff --git a/lnbits/tasks.py b/lnbits/tasks.py index ab1ebc46..4e73a0af 100644 --- a/lnbits/tasks.py +++ b/lnbits/tasks.py @@ -4,6 +4,8 @@ import traceback from http import HTTPStatus from typing import List, Callable +from fastapi.exceptions import HTTPException + from lnbits.settings import WALLET from lnbits.core.crud import ( get_payments, @@ -61,7 +63,7 @@ async def webhook_handler(): handler = getattr(WALLET, "webhook_listener", None) if handler: return await handler() - return "", HTTPStatus.NO_CONTENT + raise HTTPException(status_code=HTTPStatus.NO_CONTENT) internal_invoice_queue = asyncio.Queue(0) diff --git a/lnbits/wallets/lnpay.py b/lnbits/wallets/lnpay.py index 305400df..ab8e0d81 100644 --- a/lnbits/wallets/lnpay.py +++ b/lnbits/wallets/lnpay.py @@ -1,5 +1,6 @@ import json import asyncio +from fastapi.exceptions import HTTPException import httpx from os import getenv from http import HTTPStatus @@ -133,7 +134,7 @@ class LNPayWallet(Wallet): or "event" not in data or data["event"].get("name") != "wallet_receive" ): - return "", HTTPStatus.NO_CONTENT + raise HTTPException(status_code=HTTPStatus.NO_CONTENT) lntx_id = data["data"]["wtx"]["lnTx"]["id"] async with httpx.AsyncClient() as client: @@ -145,4 +146,5 @@ class LNPayWallet(Wallet): if data["settled"]: await self.queue.put(lntx_id) - return "", HTTPStatus.NO_CONTENT + raise HTTPException(status_code=HTTPStatus.NO_CONTENT) + diff --git a/lnbits/wallets/opennode.py b/lnbits/wallets/opennode.py index d955cc0b..ddc2849e 100644 --- a/lnbits/wallets/opennode.py +++ b/lnbits/wallets/opennode.py @@ -1,4 +1,6 @@ import asyncio + +from fastapi.exceptions import HTTPException from lnbits.helpers import url_for import hmac import httpx @@ -133,14 +135,16 @@ class OpenNodeWallet(Wallet): async def webhook_listener(self): data = await request.form if "status" not in data or data["status"] != "paid": - return "", HTTPStatus.NO_CONTENT + raise HTTPException(status_code=HTTPStatus.NO_CONTENT) + charge_id = data["id"] x = hmac.new(self.auth["Authorization"].encode("ascii"), digestmod="sha256") x.update(charge_id.encode("ascii")) if x.hexdigest() != data["hashed_order"]: print("invalid webhook, not from opennode") - return "", HTTPStatus.NO_CONTENT + raise HTTPException(status_code=HTTPStatus.NO_CONTENT) await self.queue.put(charge_id) - return "", HTTPStatus.NO_CONTENT + raise HTTPException(status_code=HTTPStatus.NO_CONTENT) +