mirror of
https://github.com/aljazceru/nutshell.git
synced 2026-02-18 23:24:20 +01:00
fix: add raise_for_status call after HTTP calls
This commit is contained in:
@@ -45,10 +45,12 @@ class LedgerAPI:
|
||||
|
||||
@staticmethod
|
||||
def _get_keys(url):
|
||||
resp = requests.get(url + "/keys").json()
|
||||
resp = requests.get(url + "/keys")
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
return {
|
||||
int(amt): PublicKey(bytes.fromhex(val), raw=True)
|
||||
for amt, val in resp.items()
|
||||
for amt, val in data.items()
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
@@ -88,6 +90,7 @@ class LedgerAPI:
|
||||
def request_mint(self, amount):
|
||||
"""Requests a mint from the server and returns Lightning invoice."""
|
||||
r = requests.get(self.url + "/mint", params={"amount": amount})
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
@staticmethod
|
||||
@@ -130,13 +133,11 @@ class LedgerAPI:
|
||||
json=payloads.dict(),
|
||||
params={"payment_hash": payment_hash},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
try:
|
||||
promises_list = resp.json()
|
||||
except:
|
||||
if resp.status_code >= 300:
|
||||
raise Exception(f"Error: {f'mint returned {resp.status_code}'}")
|
||||
else:
|
||||
raise Exception("Unkown mint error.")
|
||||
raise Exception("Unkown mint error.")
|
||||
if "error" in promises_list:
|
||||
raise Exception("Error: {}".format(promises_list["error"]))
|
||||
|
||||
@@ -180,14 +181,11 @@ class LedgerAPI:
|
||||
self.url + "/split",
|
||||
json=split_payload.dict(),
|
||||
)
|
||||
|
||||
resp.raise_for_status()
|
||||
try:
|
||||
promises_dict = resp.json()
|
||||
except:
|
||||
if resp.status_code >= 300:
|
||||
raise Exception(f"Error: {f'mint returned {resp.status_code}'}")
|
||||
else:
|
||||
raise Exception("Unkown mint error.")
|
||||
raise Exception("Unkown mint error.")
|
||||
if "error" in promises_dict:
|
||||
raise Exception("Mint Error: {}".format(promises_dict["error"]))
|
||||
promises_fst = [BlindedSignature.from_dict(p) for p in promises_dict["fst"]]
|
||||
@@ -204,28 +202,36 @@ class LedgerAPI:
|
||||
|
||||
async def check_spendable(self, proofs: List[Proof]):
|
||||
payload = CheckRequest(proofs=proofs)
|
||||
return_dict = requests.post(
|
||||
resp = requests.post(
|
||||
self.url + "/check",
|
||||
json=payload.dict(),
|
||||
).json()
|
||||
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return_dict = resp.json()
|
||||
|
||||
return return_dict
|
||||
|
||||
async def check_fees(self, payment_request: str):
|
||||
"""Checks whether the Lightning payment is internal."""
|
||||
payload = CheckFeesRequest(pr=payment_request)
|
||||
return_dict = requests.post(
|
||||
resp = requests.post(
|
||||
self.url + "/checkfees",
|
||||
json=payload.dict(),
|
||||
).json()
|
||||
)
|
||||
resp.raise_for_status()
|
||||
|
||||
return_dict = resp.json()
|
||||
return return_dict
|
||||
|
||||
async def pay_lightning(self, proofs: List[Proof], invoice: str):
|
||||
payload = MeltRequest(proofs=proofs, invoice=invoice)
|
||||
return_dict = requests.post(
|
||||
resp = requests.post(
|
||||
self.url + "/melt",
|
||||
json=payload.dict(),
|
||||
).json()
|
||||
)
|
||||
resp.raise_for_status()
|
||||
|
||||
return_dict = resp.json()
|
||||
return return_dict
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user