Skip to content

Commit

Permalink
graphenelib included into beem, in order to allow installation of ste…
Browse files Browse the repository at this point in the history
…em-python parallel to beem.

reduced dependency
this allow easier pushing to conda-forge
  • Loading branch information
holgern committed Feb 27, 2018
1 parent 7356a51 commit 3f6cee8
Show file tree
Hide file tree
Showing 50 changed files with 2,542 additions and 16 deletions.
2 changes: 1 addition & 1 deletion beem/message.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
import logging
from binascii import hexlify, unhexlify
from graphenebase.ecdsa import verify_message, sign_message
from beemgraphenebase.ecdsasig import verify_message, sign_message
from beembase.account import PublicKey
from beem.instance import shared_steem_instance
from beem.account import Account
Expand Down
2 changes: 1 addition & 1 deletion beem/wallet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import os
from graphenebase import bip38
from beemgraphenebase import bip38
from beembase.account import PrivateKey, GPHPrivateKey
from .account import Account
from .exceptions import (
Expand Down
2 changes: 1 addition & 1 deletion beemapi/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from grapheneapi.graphenewsrpc import RPCError
from beemgrapheneapi.graphenewsrpc import RPCError


def decodeRPCErrorMsg(e):
Expand Down
2 changes: 1 addition & 1 deletion beemapi/steemnoderpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json
import time
from itertools import cycle
from grapheneapi.graphenewsrpc import GrapheneWebsocketRPC
from beemgrapheneapi.graphenewsrpc import GrapheneWebsocketRPC
from beembase.chains import known_chains
from . import exceptions
import logging
Expand Down
2 changes: 1 addition & 1 deletion beembase/account.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from graphenebase.account import (
from beemgraphenebase.account import (
PasswordKey as GPHPasswordKey,
BrainKey as GPHBrainKey,
Address as GPHAddress,
Expand Down
2 changes: 1 addition & 1 deletion beembase/bip38.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from graphenebase.bip38 import (
from beemgraphenebase.bip38 import (
encrypt as GPHencrypt,
decrypt as GPHdecrypt
)
Expand Down
6 changes: 3 additions & 3 deletions beembase/objects.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import json
from collections import OrderedDict
from graphenebase.types import (
from beemgraphenebase.types import (
Uint8, Int16, Uint16, Uint32, Uint64,
Varint32, Int64, String, Bytes, Void,
Array, PointInTime, Signature, Bool,
Set, Fixed_array, Optional, Static_variant,
Map, Id, VoteId,
ObjectId as GPHObjectId
)
from graphenebase.objects import GrapheneObject, isArgsThisClass
from beemgraphenebase.objects import GrapheneObject, isArgsThisClass
from .objecttypes import object_type
from .account import PublicKey
from graphenebase.objects import Operation as GPHOperation
from beemgraphenebase.objects import Operation as GPHOperation
from .operationids import operations
import struct
default_prefix = "STM"
Expand Down
2 changes: 1 addition & 1 deletion beembase/operations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import OrderedDict
import json
from graphenebase.types import (
from beemgraphenebase.types import (
Uint8, Int16, Uint16, Uint32, Uint64,
Varint32, Int64, String, Bytes, Void,
Array, PointInTime, Signature, Bool,
Expand Down
2 changes: 1 addition & 1 deletion beembase/signedtransactions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from graphenebase.signedtransactions import Signed_Transaction as GrapheneSigned_Transaction
from beemgraphenebase.signedtransactions import Signed_Transaction as GrapheneSigned_Transaction
from .operations import Operation
from .chains import known_chains
import logging
Expand Down
2 changes: 1 addition & 1 deletion beembase/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
Op_wrapper,
Account_create,
)
from graphenebase.transactions import getBlockParams, formatTimeFromNow, timeformat
from beemgraphenebase.transactions import getBlockParams, formatTimeFromNow, timeformat
4 changes: 4 additions & 0 deletions beemgrapheneapi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .version import version as __version__
__all__ = ['grapheneapi',
'graphenewsrpc'
]
127 changes: 127 additions & 0 deletions beemgrapheneapi/grapheneapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import sys
import json
import logging
log = logging.getLogger(__name__)

try:
import requests
except ImportError:
raise ImportError("Missing dependency: python-requests")


class UnauthorizedError(Exception):
pass


class RPCError(Exception):
pass


class RPCConnection(Exception):
pass


class GrapheneAPI(object):
""" Graphene JSON-HTTP-RPC API
This class serves as an abstraction layer for easy use of the
Grapehene API.
:param str host: Host of the API server
:param int port: Port to connect to
:param str username: Username for Authentication (if required,
defaults to "")
:param str password: Password for Authentication (if required,
defaults to "")
All RPC commands of the Graphene client are exposed as methods
in the class ``grapheneapi``. Once an instance of GrapheneAPI is
created with host, port, username, and password, e.g.,
.. code-block:: python
from grapheneapi import GrapheneAPI
rpc = GrapheneAPI("localhost", 8092, "", "")
any call available to that port can be issued using the instance
via the syntax rpc.*command*(*parameters*). Example:
.. code-block:: python
rpc.info()
.. note:: A distinction has to be made whether the connection is
made to a **witness/full node** which handles the
blockchain and P2P network, or a **cli-wallet** that
handles wallet related actions! The available commands
differ drastically!
If you are connected to a wallet, you can simply initiate a transfer with:
.. code-block:: python
res = client.transfer("sender","receiver","5", "USD", "memo", True);
Again, the witness node does not offer access to construct any transactions,
and hence the calls available to the witness-rpc can be seen as read-only for
the blockchain.
"""
def __init__(self, host, port, username="", password=""):
self.host = host
self.port = port
self.username = username
self.password = password
self.headers = {'content-type': 'application/json'}

def rpcexec(self, payload):
""" Manual execute a command on API (internally used)
param str payload: The payload containing the request
return: Servers answer to the query
rtype: json
raises RPCConnection: if no connction can be made
raises UnauthorizedError: if the user is not authorized
raise ValueError: if the API returns a non-JSON formated answer
It is not recommended to use this method directly, unless
you know what you are doing. All calls available to the API
will be wrapped to methods directly::
info -> grapheneapi.info()
"""
try:
response = requests.post("http://{}:{}/rpc".format(self.host,
self.port),
data=json.dumps(payload, ensure_ascii=False).encode('utf8'),
headers=self.headers,
auth=(self.username, self.password))
if response.status_code == 401:
raise UnauthorizedError
ret = json.loads(response.text)
if 'error' in ret:
if 'detail' in ret['error']:
raise RPCError(ret['error']['detail'])
else:
raise RPCError(ret['error']['message'])
except requests.exceptions.RequestException:
raise RPCConnection("Error connecting to Client!")
except UnauthorizedError:
raise UnauthorizedError("Invalid login credentials!")
except ValueError:
raise ValueError("Client returned invalid format. Expected JSON!")
except RPCError as err:
raise err
else:
return ret["result"]

def __getattr__(self, name):
""" Map all methods to RPC calls and pass through the arguments
"""
def method(*args):
query = {"method": name,
"params": args,
"jsonrpc": "2.0",
"id": 0}
r = self.rpcexec(query)
return r
return method

0 comments on commit 3f6cee8

Please sign in to comment.