Skip to content

Commit

Permalink
Merge pull request #1 from MiranDaniel/dev
Browse files Browse the repository at this point in the history
Added Banano support, more exceptions
  • Loading branch information
MiranDaniel authored Jun 15, 2021
2 parents 169a610 + 2066116 commit ff6f694
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ node = pnrw.Node("nodeIp") # Create a new node instance

`headers` (dict): Custom headers that are sent with each request, default value is "Default"

`banano` (bool): Ensures correct configuration when using PNRW for Banano, default is False

---

## Examples
Expand Down
59 changes: 59 additions & 0 deletions pnrw/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,69 @@
class Error(Exception):
"""Base class for other exceptions"""
pass


class AddressInvalid(Exception):
def __init__(self, address, message="This address is not valid."):
self.message = message
super().__init__(self.message)


class BlockInvalid(Exception):
def __init__(self, address, message="This block is not valid, are you sure you are sending the right variable?"):
self.message = message
super().__init__(self.message)


class InvalidServerResponseHTML(Exception):
def __init__(self, message="Received invalid response from node (type HTML, expected JSON)"):
self.message = message
super().__init__(self.message)


class CannotConnect(Exception):
def __init__(self, message="Failed to establish a new connection to the node"):
self.message = message
super().__init__(self.message)


class ActionNotSupported(Exception):
def __init__(self, message="Node does not support the requested function"):
self.message = message
super().__init__(self.message)


class WalletNotFound(Exception):
def __init__(self, message="Wallet not found"):
self.message = message
super().__init__(self.message)


class InvalidBlockHash(Exception):
def __init__(self, message="Invalid block hash."):
self.message = message
super().__init__(self.message)


class UnknownError(Exception):
def __init__(self, message="The node returned an unknown error"):
self.message = message
super().__init__(self.message)


class RPCdisabled(Exception):
def __init__(self, message="RPC control is disabled on the node"):
self.message = message
super().__init__(self.message)


class InvalidBalanceNumber(Exception):
def __init__(self, message="Invalid balance number"):
self.message = message
super().__init__(self.message)


class EmptyResponse(Exception):
def __init__(self, message="Empty server response"):
self.message = message
super().__init__(self.message)
55 changes: 45 additions & 10 deletions pnrw/pnrwf.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,25 @@ def _validate_block(s):


class Node:
def __init__(self, ip, port=7076, dontUseHTTPS=False, headers="Default"):
self.ip = ip
self.port = port
self.secure = "s" if dontUseHTTPS == False else ""
def __init__(self, ip, port="Default", dontUseHTTPS=False, headers="Default", banano=False):
self.ip = ip.lower().replace("https", "").replace("http", "")
self.secure = "https" if dontUseHTTPS == False else "http"

if port == "Default":
if banano == False:
self.port = 7076
else:
self.port = 7072
else:
self.port = port

self.banano = banano

if _validate_ip(self.ip) == True:
self.target = f"http{self.secure}://{self.ip}:{self.port}"
self.target = f"{self.secure}://{self.ip}:{self.port}"
else:
self.target = f"http{self.secure}://{self.ip}"
self.target = f"{self.secure}://{self.ip}"

if headers == "Default":
self.headers = {'Content-type': 'application/json', 'Accept': '*/*',
"Accept-Encoding": "gzip, deflate, br", "Connection": "keep-alive"}
Expand All @@ -73,10 +84,34 @@ def _request(self, data):
_validate_address(data["account"])
if "block" in data:
_validate_block(data["block"])

response = requests.post(self.target, data=json.dumps(
data), headers=self.headers).text
return json.loads(response)
try:
response = requests.post(self.target, data=json.dumps(
data), headers=self.headers).text
except requests.exceptions.ConnectionError:
raise CannotConnect()
if response.startswith("<!DOCTYPE html>"):
raise InvalidServerResponseHTML()
jsload = json.loads(response)
if "message" in jsload:
if jsload["message"] == "Action is not supported":
raise ActionNotSupported()
if "error" in jsload:
if jsload["error"] == "Wallet not found":
raise WalletNotFound()
elif jsload["error"] == "Invalid block hash":
raise InvalidBlockHash()
elif jsload["error"] == "Unknown error":
raise UnknownError()
elif jsload["error"] == "RPC control is disabled":
raise RPCdisabled()
elif jsload["error"] == "Invalid balance number":
raise InvalidBalanceNumber()
elif jsload["error"] == "Empty response":
raise EmptyResponse()
else:
raise UnknownError()

return jsload

def account_balance(self, account):
response = self._request(
Expand Down

0 comments on commit ff6f694

Please sign in to comment.