[Mint] LNBits backend refactor httpx (#234)

This commit is contained in:
Angus Pearson
2023-05-19 07:55:19 +01:00
committed by GitHub
parent e25100c1e0
commit 730772d6a7

View File

@@ -18,23 +18,19 @@ class LNbitsWallet(Wallet):
def __init__(self):
self.endpoint = settings.mint_lnbits_endpoint
key = settings.mint_lnbits_key
self.headers = {"X-Api-Key": key}
self.verify = not settings.debug
self.client = httpx.AsyncClient(
verify=not settings.debug,
headers={"X-Api-Key": settings.mint_lnbits_key},
)
async def status(self) -> StatusResponse:
async with httpx.AsyncClient(verify=self.verify) as client:
try:
r = await client.get(
url=f"{self.endpoint}/api/v1/wallet",
timeout=15,
headers=self.headers,
)
r.raise_for_status()
except Exception as exc:
return StatusResponse(
f"Failed to connect to {self.endpoint} due to: {exc}", 0
)
try:
r = await self.client.get(url=f"{self.endpoint}/api/v1/wallet", timeout=15)
r.raise_for_status()
except Exception as exc:
return StatusResponse(
f"Failed to connect to {self.endpoint} due to: {exc}", 0
)
try:
data = r.json()
@@ -60,16 +56,13 @@ class LNbitsWallet(Wallet):
data["unhashed_description"] = unhashed_description.hex()
data["memo"] = memo or ""
async with httpx.AsyncClient(verify=self.verify) as client:
try:
r = await client.post(
url=f"{self.endpoint}/api/v1/payments",
json=data,
headers=self.headers,
)
r.raise_for_status()
except:
return InvoiceResponse(False, None, None, r.json()["detail"])
try:
r = await self.client.post(
url=f"{self.endpoint}/api/v1/payments", json=data
)
r.raise_for_status()
except:
return InvoiceResponse(False, None, None, r.json()["detail"])
ok, checking_id, payment_request, error_message = (
True,
None,
@@ -83,18 +76,16 @@ class LNbitsWallet(Wallet):
return InvoiceResponse(ok, checking_id, payment_request, error_message)
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
async with httpx.AsyncClient(verify=self.verify) as client:
try:
r = await client.post(
url=f"{self.endpoint}/api/v1/payments",
json={"out": True, "bolt11": bolt11},
timeout=None,
headers=self.headers,
)
r.raise_for_status()
except:
error_message = r.json()["detail"]
return PaymentResponse(None, None, None, None, error_message)
try:
r = await self.client.post(
url=f"{self.endpoint}/api/v1/payments",
json={"out": True, "bolt11": bolt11},
timeout=None,
)
r.raise_for_status()
except:
error_message = r.json()["detail"]
return PaymentResponse(None, None, None, None, error_message)
if r.status_code > 299:
return PaymentResponse(None, None, None, None, f"HTTP status: {r.reason}")
if "detail" in r.json():
@@ -116,29 +107,25 @@ class LNbitsWallet(Wallet):
return PaymentResponse(ok, checking_id, payment.fee_msat, payment.preimage)
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
async with httpx.AsyncClient(verify=self.verify) as client:
try:
r = await client.get(
url=f"{self.endpoint}/api/v1/payments/{checking_id}",
headers=self.headers,
)
r.raise_for_status()
except:
return PaymentStatus(None)
try:
r = await self.client.get(
url=f"{self.endpoint}/api/v1/payments/{checking_id}"
)
r.raise_for_status()
except:
return PaymentStatus(None)
if r.json().get("detail"):
return PaymentStatus(None)
return PaymentStatus(r.json()["paid"])
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
async with httpx.AsyncClient(verify=self.verify) as client:
try:
r = await client.get(
url=f"{self.endpoint}/api/v1/payments/{checking_id}",
headers=self.headers,
)
r.raise_for_status()
except:
return PaymentStatus(None)
try:
r = await self.client.get(
url=f"{self.endpoint}/api/v1/payments/{checking_id}"
)
r.raise_for_status()
except:
return PaymentStatus(None)
data = r.json()
if "paid" not in data and "details" not in data:
return PaymentStatus(None)