Simplify code for client

This commit is contained in:
vrde 2016-03-04 16:00:30 +01:00
parent 1fa47d4b5f
commit 8ed937aecc
2 changed files with 25 additions and 76 deletions

View File

@ -41,23 +41,37 @@ class Client:
if not self.public_key or not self.private_key:
raise exceptions.KeypairNotFoundException()
def make_tx(self, new_owner, tx_input, operation='TRANSFER', payload=None):
"""Make a new transaction
def create(self, payload=None):
"""Issue a transaction to create an asset.
Refer to the documentation of ``bigchaindb.util.create_tx``
Args:
payload (dict): the payload for the transaction.
Return:
The transaction pushed to the Federation.
"""
return util.create_tx(self.public_key, new_owner, tx_input, operation, payload)
tx = util.create_tx(self.public_key, self.public_key, None, operation='CREATE', payload=payload)
signed_tx = util.sign_tx(tx, self.private_key)
return self._push(signed_tx)
def sign_tx(self, tx):
"""Sign a transaction
def transfer(self, new_owner, tx_input, payload=None):
"""Issue a transaction to transfer an asset.
Refer to the documentation of ``bigchaindb.util.sign_tx``
Args:
new_owner (str): the public key of the new owner
tx_input (str): the id of the transaction to use as input
payload (dict, optional): the payload for the transaction.
Return:
The transaction pushed to the Federation.
"""
return util.sign_tx(tx, self.private_key)
tx = util.create_tx(self.public_key, new_owner, tx_input, operation='TRANSFER', payload=payload)
signed_tx = util.sign_tx(tx, self.private_key)
return self._push(signed_tx)
def push_tx(self, tx):
def _push(self, tx):
"""Submit a transaction to the Federation.
Args:
@ -70,36 +84,6 @@ class Client:
res = requests.post(self.api_endpoint + '/transactions/', json=tx)
return res.json()
def create(self, payload=None):
"""Create a transaction.
Args:
payload (dict): the payload for the transaction.
Return:
The transaction pushed to the Federation.
"""
tx = self.make_tx(self.public_key, None, operation='CREATE', payload=payload)
signed_tx = self.sign_tx(tx)
return self.push_tx(signed_tx)
def transfer(self, new_owner, tx_input, payload=None):
"""Transfer a transaction.
Args:
new_owner (str): the public key of the new owner
tx_input (str): the id of the transaction to use as input
payload (dict, optional): the payload for the transaction.
Return:
The transaction pushed to the Federation.
"""
tx = self.make_tx(new_owner, tx_input, payload=payload)
signed_tx = self.sign_tx(tx)
return self.push_tx(signed_tx)
def temp_client():
"""Create a new temporary client.

View File

@ -29,42 +29,7 @@ def test_temp_client_returns_a_temp_client():
assert client.private_key
def test_client_can_make_transactions(client):
tx = client.make_tx('a', 123)
assert tx['transaction']['current_owner'] == client.public_key
assert tx['transaction']['new_owner'] == 'a'
assert tx['transaction']['input'] == 123
def test_client_can_sign_transactions(client):
from bigchaindb import util
tx = client.make_tx('a', 123)
signed_tx = client.sign_tx(tx)
assert signed_tx['transaction']['current_owner'] == client.public_key
assert signed_tx['transaction']['new_owner'] == 'a'
assert signed_tx['transaction']['input'] == 123
assert util.verify_signature(signed_tx)
def test_client_can_push_transactions(mock_requests_post, client):
from bigchaindb import util
tx = client.make_tx('a', 123)
signed_tx = client.sign_tx(tx)
ret_tx = client.push_tx(signed_tx)
assert ret_tx['transaction']['current_owner'] == client.public_key
assert ret_tx['transaction']['new_owner'] == 'a'
assert ret_tx['transaction']['input'] == 123
assert util.verify_signature(ret_tx)
def test_client_can_create_transactions_using_shortcut_method(mock_requests_post, client):
def test_client_can_create_assets(mock_requests_post, client):
from bigchaindb import util
tx = client.create()
@ -81,7 +46,7 @@ def test_client_can_create_transactions_using_shortcut_method(mock_requests_post
assert util.verify_signature(tx)
def test_client_can_transfer_transactions_using_shortcut_method(mock_requests_post, client):
def test_client_can_transfer_assets(mock_requests_post, client):
from bigchaindb import util
tx = client.transfer('a', 123)