From 7f41d73a844eb79f1399f78d117bd942b2b5a5b1 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Fri, 9 Oct 2020 17:06:50 -0300 Subject: [PATCH] SSE-based invoices stream for LNbitsWallet. --- lnbits/wallets/lnbits.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lnbits/wallets/lnbits.py b/lnbits/wallets/lnbits.py index f014bf459..78e8f8ca1 100644 --- a/lnbits/wallets/lnbits.py +++ b/lnbits/wallets/lnbits.py @@ -1,4 +1,4 @@ -import trio # type: ignore +import json import httpx from os import getenv from typing import Optional, Dict, AsyncGenerator @@ -68,6 +68,18 @@ class LNbitsWallet(Wallet): return PaymentStatus(r.json()["paid"]) async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: - print("lnbits does not support paid invoices stream yet") - await trio.sleep(5) - yield "" + url = f"{self.endpoint}/api/v1/payments/sse" + + async with httpx.AsyncClient(timeout=None, headers=self.key) as client: + async with client.stream("GET", url) as r: + async for line in r.aiter_lines(): + if line.startswith("data:"): + try: + data = json.loads(line[5:]) + except json.decoder.JSONDecodeError: + continue + + if type(data) is not list or len(data) < 9: + continue + + yield data[8] # payment_hash