fix: call webhook in the background (from the task.py)

This commit is contained in:
Vlad Stan 2022-11-24 16:57:59 +02:00 committed by dni ⚡
parent a301b97766
commit 948e7f0a4e
2 changed files with 22 additions and 13 deletions

View File

@ -1,5 +1,6 @@
import asyncio
import httpx
from loguru import logger
from lnbits.core.models import Payment
@ -7,7 +8,8 @@ from lnbits.extensions.satspay.crud import check_address_balance, get_charge
from lnbits.helpers import get_current_extension_name
from lnbits.tasks import register_invoice_listener
# from .crud import get_ticket, set_ticket_paid
from .helpers import compact_charge
from .models import Charges
async def wait_for_paid_invoices():
@ -30,4 +32,22 @@ async def on_invoice_paid(payment: Payment) -> None:
return
await payment.set_pending(False)
await check_address_balance(charge_id=charge.id)
charge = await check_address_balance(charge_id=charge.id)
if charge.paid and charge.webhook:
await call_webhook(charge)
async def call_webhook(charge: Charges):
async with httpx.AsyncClient() as client:
try:
r = await client.post(
charge.webhook,
json=compact_charge(charge),
timeout=40,
)
except AssertionError:
charge.webhook = None
except Exception as e:
logger.warning(f"Failed to call webhook for charge {charge.id}")
logger.warning(e)

View File

@ -1,6 +1,5 @@
from http import HTTPStatus
import httpx
from fastapi.params import Depends
from starlette.exceptions import HTTPException
@ -119,16 +118,6 @@ async def api_charge_balance(charge_id):
status_code=HTTPStatus.NOT_FOUND, detail="Charge does not exist."
)
if charge.paid and charge.webhook:
async with httpx.AsyncClient() as client:
try:
r = await client.post(
charge.webhook,
json=compact_charge(charge),
timeout=40,
)
except AssertionError:
charge.webhook = None
return {
**compact_charge(charge),
**{"time_elapsed": charge.time_elapsed},