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,18 +74,23 @@ 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"):
try: async with httpx.AsyncClient() as client:
r = httpx.get(g.data["lnurl_callback"], params={"pr": payment_request}, timeout=10) try:
if r.is_error: r = await client.get(
lnurl_response = r.text g.data["lnurl_callback"],
else: params={"pr": payment_request},
resp = json.loads(r.text) timeout=10,
if resp["status"] != "OK": )
lnurl_response = resp["reason"] if r.is_error:
lnurl_response = r.text
else: else:
lnurl_response = True resp = json.loads(r.text)
except (httpx.ConnectError, httpx.RequestError): if resp["status"] != "OK":
lnurl_response = False lnurl_response = resp["reason"]
else:
lnurl_response = True
except (httpx.ConnectError, httpx.RequestError):
lnurl_response = False
return ( return (
jsonify( jsonify(
@ -149,16 +154,17 @@ 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
try: async with httpx.AsyncClient() as client:
r = httpx.get( try:
g.data["callback"], r = await client.get(
params={"amount": g.data["amount"], "comment": g.data["comment"]}, g.data["callback"],
timeout=40, params={"amount": g.data["amount"], "comment": g.data["comment"]},
) timeout=40,
if r.is_error: )
if r.is_error:
return jsonify({"message": "failed to connect"}), HTTPStatus.BAD_REQUEST
except (httpx.ConnectError, httpx.RequestError):
return jsonify({"message": "failed to connect"}), HTTPStatus.BAD_REQUEST return jsonify({"message": "failed to connect"}), HTTPStatus.BAD_REQUEST
except (httpx.ConnectError, httpx.RequestError):
return jsonify({"message": "failed to connect"}), HTTPStatus.BAD_REQUEST
params = json.loads(r.text) params = json.loads(r.text)
if params.get("status") == "ERROR": if params.get("status") == "ERROR":
@ -293,9 +299,10 @@ 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:
if r.is_error: r = await client.get(url.url, timeout=40)
return jsonify({"domain": domain, "error": "failed to get parameters"}) if r.is_error:
return jsonify({"domain": domain, "error": "failed to get parameters"})
try: try:
jdata = json.loads(r.text) jdata = json.loads(r.text)