migrate from aiohttp to httpx following master.

This commit is contained in:
fiatjaf 2020-09-29 22:04:51 -03:00
parent 2c92205703
commit bbe8d05af4
3 changed files with 20 additions and 24 deletions

View file

@ -1,4 +1,4 @@
import aiohttp import httpx
from lnbits.core.models import Payment from lnbits.core.models import Payment
@ -13,9 +13,9 @@ async def on_invoice_paid(payment: Payment) -> None:
# no pay_link or this webhook has already been sent # no pay_link or this webhook has already been sent
return return
if pay_link.webhook_url: if pay_link.webhook_url:
async with aiohttp.ClientSession() as session: async with httpx.AsyncClient() as client:
try: try:
r = await session.post( r = await client.post(
pay_link.webhook_url, pay_link.webhook_url,
json={ json={
"payment_hash": payment.payment_hash, "payment_hash": payment.payment_hash,
@ -23,8 +23,8 @@ async def on_invoice_paid(payment: Payment) -> None:
"amount": payment.amount, "amount": payment.amount,
"lnurlp": pay_link.id, "lnurlp": pay_link.id,
}, },
timeout=60, timeout=40,
) )
mark_webhook_sent(payment.payment_hash, r.status) mark_webhook_sent(payment.payment_hash, r.status_code)
except aiohttp.client_exceptions.ClientError: except httpx.RequestError:
mark_webhook_sent(payment.payment_hash, -1) mark_webhook_sent(payment.payment_hash, -1)

View file

@ -1,6 +1,6 @@
import json import json
import asyncio import asyncio
import aiohttp import httpx
from os import getenv from os import getenv
from http import HTTPStatus from http import HTTPStatus
from typing import Optional, Dict, AsyncGenerator from typing import Optional, Dict, AsyncGenerator
@ -93,12 +93,12 @@ class LNPayWallet(Wallet):
return "", HTTPStatus.NO_CONTENT return "", HTTPStatus.NO_CONTENT
lntx_id = data["data"]["wtx"]["lnTx"]["id"] lntx_id = data["data"]["wtx"]["lnTx"]["id"]
async with aiohttp.ClientSession() as session: async with httpx.AsyncClient() as client:
resp = await session.get( r = await client.get(
f"{self.endpoint}/user/lntx/{lntx_id}?fields=settled", f"{self.endpoint}/user/lntx/{lntx_id}?fields=settled",
headers=self.auth_api, headers=self.auth_api,
) )
data = await resp.json() data = r.json()
if data["settled"]: if data["settled"]:
self.queue.put_nowait(lntx_id) self.queue.put_nowait(lntx_id)

View file

@ -1,7 +1,6 @@
import random import random
import requests
import json import json
from aiohttp_sse_client import client as sse_client import httpx
from os import getenv from os import getenv
from typing import Optional, AsyncGenerator from typing import Optional, AsyncGenerator
@ -30,9 +29,7 @@ class SparkWallet(Wallet):
elif kwargs: elif kwargs:
params = kwargs params = kwargs
r = requests.post( r = httpx.post(self.url + "/rpc", headers={"X-Access": self.token}, json={"method": key, "params": params})
self.url + "/rpc", headers={"X-Access": self.token}, json={"method": key, "params": params}
)
try: try:
data = r.json() data = r.json()
except: except:
@ -98,12 +95,11 @@ class SparkWallet(Wallet):
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
url = self.url + "/stream?access-key=" + self.token url = self.url + "/stream?access-key=" + self.token
conn = sse_client.EventSource(url)
async with conn as es: async with httpx.AsyncClient() as client:
async for event in es: async with client.stream("GET", url) as r:
try: async for line in r.aiter_lines():
if event.type == "inv-paid": if line.startswith("data:"):
data = json.loads(event.data) data = json.loads(line[5:])
yield data["label"] if "pay_index" in data and data.get("status") == "paid":
except ConnectionError: yield data["label"]
pass