Skip to content

Commit

Permalink
Added custom exceptions, fixed formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
MiranDaniel committed Jun 14, 2021
1 parent 2252fda commit 169a610
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions pnrw/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .pnrwf import Node as N
from . import exceptions
Node = N
10 changes: 10 additions & 0 deletions pnrw/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

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)
48 changes: 40 additions & 8 deletions pnrw/pnrwf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""
Copyright (C) 2021 MiranDaniel
This program is free software: you can redistribute it and/or modify
Expand All @@ -13,10 +14,13 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""

import requests
import json
from .exceptions import *


def _validate_ip(s):
a = s.split('.')
Expand All @@ -31,6 +35,24 @@ def _validate_ip(s):
return False
return True


def _validate_address(s):
if s.startswith("nano_") == False and s.startswith("ban_") == False:
raise AddressInvalid(s)
else:
if s.startswith("nano_"):
if len(s) != 65:
raise AddressInvalid(s)
elif s.startswith("ban_"):
if len(s) != 64:
raise AddressInvalid(s)


def _validate_block(s):
if len(s) != 64:
raise BlockInvalid(s)


class Node:
def __init__(self, ip, port=7076, dontUseHTTPS=False, headers="Default"):
self.ip = ip
Expand All @@ -40,13 +62,20 @@ def __init__(self, ip, port=7076, dontUseHTTPS=False, headers="Default"):
self.target = f"http{self.secure}://{self.ip}:{self.port}"
else:
self.target = f"http{self.secure}://{self.ip}"
if headers != "Default":
self.headers = {'Content-type': 'application/json', 'Accept': '*/*',"Accept-Encoding":"gzip, deflate, br","Connection":"keep-alive"}
if headers == "Default":
self.headers = {'Content-type': 'application/json', 'Accept': '*/*',
"Accept-Encoding": "gzip, deflate, br", "Connection": "keep-alive"}
else:
self.headers = headers

def _request(self, data):
response = requests.post(self.target, data=json.dumps(data), headers=self.headers).text
if "account" in 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)

def account_balance(self, account):
Expand Down Expand Up @@ -310,7 +339,7 @@ def delegators_count(self, account):

def deterministic_key(self, seed, index):
response = self._request(
{"action": "deterministic_key", "seed":seed,"index":index})
{"action": "deterministic_key", "seed": seed, "index": index})
return response

def epoch_upgrade(self, epoch, key):
Expand All @@ -322,8 +351,9 @@ def frontier_count(self):
response = self._request({"action": "frontier_count"})
return int(response["count"])

def frontiers(self,account,count):
response = self._request({"action": "frontiers","account":account,"count":count})
def frontiers(self, account, count):
response = self._request(
{"action": "frontiers", "account": account, "count": count})
return response["frontiers"]

def keepalive(self, address, port):
Expand All @@ -340,7 +370,8 @@ def key_expand(self, key):
return response

def ledger(self, account, count):
response = self._request({"action": "ledger", "account":account,"count":count})
response = self._request(
{"action": "ledger", "account": account, "count": count})
ret = {}
for i in response["accounts"]:
ret[i] = {
Expand Down Expand Up @@ -378,7 +409,8 @@ def pending_exists(self, Hash):
return int(response["exists"])

def process(self, json_block, subtype, block):
response = self._request({"action": "process", "json_block":json_block,"subtype":subtype, "block":block})
response = self._request(
{"action": "process", "json_block": json_block, "subtype": subtype, "block": block})
return response["hash"]

def representatives(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='pnrw',
version='0.1.1',
version='0.1.2',
description='Python Nanocurrency RPC Wrapper',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 169a610

Please sign in to comment.