diff --git a/cashu/wallet/wallet.py b/cashu/wallet/wallet.py index 719ffee..b32421a 100644 --- a/cashu/wallet/wallet.py +++ b/cashu/wallet/wallet.py @@ -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