mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-21 11:04:19 +01:00
Mint: LNbitsWallet add extra check for payment state (#601)
* add extra check for payment state * improve error handling * fix response * add failure
This commit is contained in:
@@ -44,22 +44,13 @@ class LNbitsWallet(LightningBackend):
|
|||||||
try:
|
try:
|
||||||
r = await self.client.get(url=f"{self.endpoint}/api/v1/wallet", timeout=15)
|
r = await self.client.get(url=f"{self.endpoint}/api/v1/wallet", timeout=15)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
data: dict = r.json()
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
return StatusResponse(
|
return StatusResponse(
|
||||||
error_message=f"Failed to connect to {self.endpoint} due to: {exc}",
|
error_message=f"Failed to connect to {self.endpoint} due to: {exc}",
|
||||||
balance=0,
|
balance=0,
|
||||||
)
|
)
|
||||||
|
if data.get("detail"):
|
||||||
try:
|
|
||||||
data: dict = r.json()
|
|
||||||
except Exception:
|
|
||||||
return StatusResponse(
|
|
||||||
error_message=(
|
|
||||||
f"Received invalid response from {self.endpoint}: {r.text}"
|
|
||||||
),
|
|
||||||
balance=0,
|
|
||||||
)
|
|
||||||
if "detail" in data:
|
|
||||||
return StatusResponse(
|
return StatusResponse(
|
||||||
error_message=f"LNbits error: {data['detail']}", balance=0
|
error_message=f"LNbits error: {data['detail']}", balance=0
|
||||||
)
|
)
|
||||||
@@ -87,10 +78,16 @@ class LNbitsWallet(LightningBackend):
|
|||||||
url=f"{self.endpoint}/api/v1/payments", json=data
|
url=f"{self.endpoint}/api/v1/payments", json=data
|
||||||
)
|
)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
except Exception as e:
|
data = r.json()
|
||||||
return InvoiceResponse(ok=False, error_message=str(e))
|
except httpx.HTTPStatusError:
|
||||||
|
return InvoiceResponse(
|
||||||
|
ok=False, error_message=f"HTTP status: {r.reason_phrase}"
|
||||||
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
return InvoiceResponse(ok=False, error_message=str(exc))
|
||||||
|
if data.get("detail"):
|
||||||
|
return InvoiceResponse(ok=False, error_message=data["detail"])
|
||||||
|
|
||||||
data = r.json()
|
|
||||||
checking_id, payment_request = data["checking_id"], data["payment_request"]
|
checking_id, payment_request = data["checking_id"], data["payment_request"]
|
||||||
|
|
||||||
return InvoiceResponse(
|
return InvoiceResponse(
|
||||||
@@ -109,17 +106,19 @@ class LNbitsWallet(LightningBackend):
|
|||||||
timeout=None,
|
timeout=None,
|
||||||
)
|
)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
except Exception:
|
data: dict = r.json()
|
||||||
error_message = r.json().get("detail") or r.reason_phrase
|
except httpx.HTTPStatusError:
|
||||||
return PaymentResponse(
|
return PaymentResponse(
|
||||||
result=PaymentResult.FAILED, error_message=error_message
|
result=PaymentResult.FAILED,
|
||||||
|
error_message=f"HTTP status: {r.reason_phrase}",
|
||||||
)
|
)
|
||||||
if r.json().get("detail"):
|
except Exception as exc:
|
||||||
|
return PaymentResponse(result=PaymentResult.FAILED, error_message=str(exc))
|
||||||
|
if data.get("detail"):
|
||||||
return PaymentResponse(
|
return PaymentResponse(
|
||||||
result=PaymentResult.FAILED, error_message=(r.json()["detail"],)
|
result=PaymentResult.FAILED, error_message=data["detail"]
|
||||||
)
|
)
|
||||||
|
|
||||||
data: dict = r.json()
|
|
||||||
checking_id = data.get("payment_hash")
|
checking_id = data.get("payment_hash")
|
||||||
if not checking_id:
|
if not checking_id:
|
||||||
return PaymentResponse(
|
return PaymentResponse(
|
||||||
@@ -142,6 +141,7 @@ class LNbitsWallet(LightningBackend):
|
|||||||
url=f"{self.endpoint}/api/v1/payments/{checking_id}"
|
url=f"{self.endpoint}/api/v1/payments/{checking_id}"
|
||||||
)
|
)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
data: dict = r.json()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return PaymentStatus(result=PaymentResult.UNKNOWN, error_message=str(e))
|
return PaymentStatus(result=PaymentResult.UNKNOWN, error_message=str(e))
|
||||||
data: dict = r.json()
|
data: dict = r.json()
|
||||||
@@ -171,14 +171,16 @@ class LNbitsWallet(LightningBackend):
|
|||||||
url=f"{self.endpoint}/api/v1/payments/{checking_id}"
|
url=f"{self.endpoint}/api/v1/payments/{checking_id}"
|
||||||
)
|
)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
data = r.json()
|
||||||
except httpx.HTTPStatusError as e:
|
except httpx.HTTPStatusError as e:
|
||||||
if e.response.status_code != 404:
|
if e.response.status_code != 404:
|
||||||
raise e
|
raise e
|
||||||
return PaymentStatus(
|
return PaymentStatus(
|
||||||
result=PaymentResult.UNKNOWN, error_message=e.response.text
|
result=PaymentResult.UNKNOWN, error_message=e.response.text
|
||||||
)
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return PaymentStatus(result=PaymentResult.UNKNOWN, error_message=str(e))
|
||||||
|
|
||||||
data = r.json()
|
|
||||||
if "paid" not in data and "details" not in data:
|
if "paid" not in data and "details" not in data:
|
||||||
return PaymentStatus(
|
return PaymentStatus(
|
||||||
result=PaymentResult.UNKNOWN, error_message="invalid response"
|
result=PaymentResult.UNKNOWN, error_message="invalid response"
|
||||||
|
|||||||
Reference in New Issue
Block a user