From d723f873082d35ddfae03133be3f0a471996dd16 Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Tue, 3 May 2022 16:11:09 +0200 Subject: [PATCH] changed get transaction list api point to use comma separated txids Signed-off-by: Lorenz Herzberger --- Makefile | 4 ++-- planetmint/lib.py | 5 ++--- planetmint/web/views/parameters.py | 7 +++++++ planetmint/web/views/transactions.py | 2 +- tests/web/test_transactions.py | 14 +++++++------- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index b29ea0f..9ef104e 100644 --- a/Makefile +++ b/Makefile @@ -75,9 +75,9 @@ lint: check-deps ## Lint the project test: check-deps test-unit test-acceptance ## Run unit and acceptance tests -test-unit: check-deps ## Run all tests once +test-unit: check-deps ## Run all tests once or specify a file/test with TEST=tests/file.py::Class::test @$(DC) up -d bdb - @$(DC) exec planetmint pytest + @$(DC) exec planetmint pytest ${TEST} test-unit-watch: check-deps ## Run all tests and wait. Every time you change code, tests will be run again @$(DC) run --rm --no-deps planetmint pytest -f diff --git a/planetmint/lib.py b/planetmint/lib.py index d16a6cf..2f63918 100644 --- a/planetmint/lib.py +++ b/planetmint/lib.py @@ -263,11 +263,10 @@ class Planetmint(object): def get_transactions(self, txn_ids): return backend.query.get_transactions(self.connection, txn_ids) - # TODO: change this to asset_ids, check references of function - def get_transactions_filtered(self, asset_id, operation=None, last_tx=None): + def get_transactions_filtered(self, asset_ids, operation=None, last_tx=None): """Get a list of transactions filtered on some criteria """ - txids = backend.query.get_txids_filtered(self.connection, asset_id, + txids = backend.query.get_txids_filtered(self.connection, asset_ids, operation, last_tx) for txid in txids: yield self.get_transaction(txid) diff --git a/planetmint/web/views/parameters.py b/planetmint/web/views/parameters.py index 6df22ff..567c6ee 100644 --- a/planetmint/web/views/parameters.py +++ b/planetmint/web/views/parameters.py @@ -13,6 +13,13 @@ def valid_txid(txid): return txid.lower() raise ValueError('Invalid hash') +def valid_txid_list(txids): + txids = txids.split(',') + r = re.compile('^[a-fA-F0-9]{64}$') + valid_txids = list(filter(r.match, txids)) + if not len(txids) == len(valid_txids): + raise ValueError('List contains invalid hash') + return [txid.lower() for txid in valid_txids] def valid_bool(val): val = val.lower() diff --git a/planetmint/web/views/transactions.py b/planetmint/web/views/transactions.py index eafaeed..7c4b839 100644 --- a/planetmint/web/views/transactions.py +++ b/planetmint/web/views/transactions.py @@ -47,7 +47,7 @@ class TransactionListApi(Resource): def get(self): parser = reqparse.RequestParser() parser.add_argument('operation', type=parameters.valid_operation) - parser.add_argument('asset_id', type=parameters.valid_txid, + parser.add_argument('asset_ids', type=parameters.valid_txid_list, required=True) parser.add_argument('last_tx', type=parameters.valid_bool, required=False) diff --git a/tests/web/test_transactions.py b/tests/web/test_transactions.py index d5c5537..8abfa55 100644 --- a/tests/web/test_transactions.py +++ b/tests/web/test_transactions.py @@ -369,24 +369,24 @@ def test_transactions_get_list_good(client): return [type('', (), {'to_dict': partial(lambda a: a, arg)}) for arg in sorted(args.items())] - asset_id = '1' * 64 + asset_ids = ['1' * 64] with patch('planetmint.Planetmint.get_transactions_filtered', get_txs_patched): - url = TX_ENDPOINT + '?asset_id=' + asset_id + url = TX_ENDPOINT + '?asset_ids=' + ','.join(asset_ids) assert client.get(url).json == [ - ['asset_id', asset_id], + ['asset_ids', asset_ids], ['last_tx', None], ['operation', None] ] - url = TX_ENDPOINT + '?asset_id=' + asset_id + '&operation=CREATE' + url = TX_ENDPOINT + '?asset_ids=' + ','.join(asset_ids) + '&operation=CREATE' assert client.get(url).json == [ - ['asset_id', asset_id], + ['asset_ids', asset_ids], ['last_tx', None], ['operation', 'CREATE'] ] - url = TX_ENDPOINT + '?asset_id=' + asset_id + '&last_tx=true' + url = TX_ENDPOINT + '?asset_ids=' + ','.join(asset_ids) + '&last_tx=true' assert client.get(url).json == [ - ['asset_id', asset_id], + ['asset_ids', asset_ids], ['last_tx', True], ['operation', None] ]