make httpx calls async when doing them from view functions.

this is important so lnbits can call itself without hanging forever.

and these functions are already async so it should have been this way since ever.
This commit is contained in:
fiatjaf 2020-10-15 12:58:37 -03:00
parent c81e61e4fe
commit 9cb58833ad

View file

@ -74,8 +74,13 @@ async def api_payments_create_invoice():
lnurl_response: Union[None, bool, str] = None lnurl_response: Union[None, bool, str] = None
if g.data.get("lnurl_callback"): if g.data.get("lnurl_callback"):
async with httpx.AsyncClient() as client:
try: try:
r = httpx.get(g.data["lnurl_callback"], params={"pr": payment_request}, timeout=10) r = await client.get(
g.data["lnurl_callback"],
params={"pr": payment_request},
timeout=10,
)
if r.is_error: if r.is_error:
lnurl_response = r.text lnurl_response = r.text
else: else:
@ -149,8 +154,9 @@ async def api_payments_create():
async def api_payments_pay_lnurl(): async def api_payments_pay_lnurl():
domain = urlparse(g.data["callback"]).netloc domain = urlparse(g.data["callback"]).netloc
async with httpx.AsyncClient() as client:
try: try:
r = httpx.get( r = await client.get(
g.data["callback"], g.data["callback"],
params={"amount": g.data["amount"], "comment": g.data["comment"]}, params={"amount": g.data["amount"], "comment": g.data["comment"]},
timeout=40, timeout=40,
@ -293,7 +299,8 @@ async def api_lnurlscan(code: str):
if url.is_login: if url.is_login:
return jsonify({"domain": domain, "kind": "auth", "error": "unsupported"}) return jsonify({"domain": domain, "kind": "auth", "error": "unsupported"})
r = httpx.get(url.url) async with httpx.AsyncClient() as client:
r = await client.get(url.url, timeout=40)
if r.is_error: if r.is_error:
return jsonify({"domain": domain, "error": "failed to get parameters"}) return jsonify({"domain": domain, "error": "failed to get parameters"})