fix: more return types

This commit is contained in:
Stefan Stammberger 2021-09-11 11:02:48 +02:00
parent d8d8c6b454
commit 63d0242685
No known key found for this signature in database
GPG key ID: 645FA807E935D9D5
6 changed files with 34 additions and 17 deletions

View file

@ -295,14 +295,14 @@ async def api_payment(payment_hash, wallet: WalletTypeInfo = Depends(get_key_typ
payment = await wallet.wallet.get_payment(payment_hash) payment = await wallet.wallet.get_payment(payment_hash)
if not payment: if not payment:
return {"message": "Payment does not exist."}, HTTPStatus.NOT_FOUND return {"message": "Payment does not exist."}
elif not payment.pending: elif not payment.pending:
return {"paid": True, "preimage": payment.preimage}, HTTPStatus.OK return {"paid": True, "preimage": payment.preimage}
try: try:
await payment.check_pending() await payment.check_pending()
except Exception: except Exception:
return {"paid": False}, HTTPStatus.OK return {"paid": False}
return {"paid": not payment.pending, "preimage": payment.preimage} return {"paid": not payment.pending, "preimage": payment.preimage}

View file

@ -204,7 +204,7 @@ async def lnurlwallet(request: Request):
async def manifest(usr: str): async def manifest(usr: str):
user = await get_user(usr) user = await get_user(usr)
if not user: if not user:
return "", HTTPStatus.NOT_FOUND raise HTTPException(status_code=HTTPStatus.NOT_FOUND)
return { return {
"short_name": "LNbits", "short_name": "LNbits",

View file

@ -1,7 +1,7 @@
import asyncio import asyncio
import datetime import datetime
from http import HTTPStatus from http import HTTPStatus
from fastapi import HTTPException
from lnbits import bolt11 from lnbits import bolt11
from .. import core_app from .. import core_app
@ -14,17 +14,23 @@ async def api_public_payment_longpolling(payment_hash):
payment = await get_standalone_payment(payment_hash) payment = await get_standalone_payment(payment_hash)
if not payment: 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: elif not payment.pending:
return {"status": "paid"}, HTTPStatus.OK return {"status": "paid"}
try: try:
invoice = bolt11.decode(payment.bolt11) invoice = bolt11.decode(payment.bolt11)
expiration = datetime.datetime.fromtimestamp(invoice.date + invoice.expiry) expiration = datetime.datetime.fromtimestamp(invoice.date + invoice.expiry)
if expiration < datetime.datetime.now(): if expiration < datetime.datetime.now():
return {"status": "expired"}, HTTPStatus.OK return {"status": "expired"}
except: 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) payment_queue = asyncio.Queue(0)
@ -37,7 +43,7 @@ async def api_public_payment_longpolling(payment_hash):
async for payment in payment_queue.get(): async for payment in payment_queue.get():
if payment.payment_hash == payment_hash: if payment.payment_hash == payment_hash:
nonlocal response nonlocal response
response = ({"status": "paid"}, HTTPStatus.OK) response = {"status": "paid"}
cancel_scope.cancel() cancel_scope.cancel()
async def timeouter(cancel_scope): async def timeouter(cancel_scope):
@ -51,4 +57,7 @@ async def api_public_payment_longpolling(payment_hash):
if response: if response:
return response return response
else: else:
return {"message": "timeout"}, HTTPStatus.REQUEST_TIMEOUT raise HTTPException(
status_code=HTTPStatus.REQUEST_TIMEOUT,
detail="timeout"
)

View file

@ -4,6 +4,8 @@ import traceback
from http import HTTPStatus from http import HTTPStatus
from typing import List, Callable from typing import List, Callable
from fastapi.exceptions import HTTPException
from lnbits.settings import WALLET from lnbits.settings import WALLET
from lnbits.core.crud import ( from lnbits.core.crud import (
get_payments, get_payments,
@ -61,7 +63,7 @@ async def webhook_handler():
handler = getattr(WALLET, "webhook_listener", None) handler = getattr(WALLET, "webhook_listener", None)
if handler: if handler:
return await handler() return await handler()
return "", HTTPStatus.NO_CONTENT raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
internal_invoice_queue = asyncio.Queue(0) internal_invoice_queue = asyncio.Queue(0)

View file

@ -1,5 +1,6 @@
import json import json
import asyncio import asyncio
from fastapi.exceptions import HTTPException
import httpx import httpx
from os import getenv from os import getenv
from http import HTTPStatus from http import HTTPStatus
@ -133,7 +134,7 @@ class LNPayWallet(Wallet):
or "event" not in data or "event" not in data
or data["event"].get("name") != "wallet_receive" 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"] lntx_id = data["data"]["wtx"]["lnTx"]["id"]
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
@ -145,4 +146,5 @@ class LNPayWallet(Wallet):
if data["settled"]: if data["settled"]:
await self.queue.put(lntx_id) await self.queue.put(lntx_id)
return "", HTTPStatus.NO_CONTENT raise HTTPException(status_code=HTTPStatus.NO_CONTENT)

View file

@ -1,4 +1,6 @@
import asyncio import asyncio
from fastapi.exceptions import HTTPException
from lnbits.helpers import url_for from lnbits.helpers import url_for
import hmac import hmac
import httpx import httpx
@ -133,14 +135,16 @@ class OpenNodeWallet(Wallet):
async def webhook_listener(self): async def webhook_listener(self):
data = await request.form data = await request.form
if "status" not in data or data["status"] != "paid": if "status" not in data or data["status"] != "paid":
return "", HTTPStatus.NO_CONTENT raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
charge_id = data["id"] charge_id = data["id"]
x = hmac.new(self.auth["Authorization"].encode("ascii"), digestmod="sha256") x = hmac.new(self.auth["Authorization"].encode("ascii"), digestmod="sha256")
x.update(charge_id.encode("ascii")) x.update(charge_id.encode("ascii"))
if x.hexdigest() != data["hashed_order"]: if x.hexdigest() != data["hashed_order"]:
print("invalid webhook, not from opennode") print("invalid webhook, not from opennode")
return "", HTTPStatus.NO_CONTENT raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
await self.queue.put(charge_id) await self.queue.put(charge_id)
return "", HTTPStatus.NO_CONTENT raise HTTPException(status_code=HTTPStatus.NO_CONTENT)