Skip to content

Commit

Permalink
Merge pull request #352 from SubstraFoundation/chaincode-pagination
Browse files Browse the repository at this point in the history
Handle chaincode pagination
  • Loading branch information
Kelvin-M authored Dec 8, 2020
2 parents e6ee19e + fee959f commit c3a16ad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 13 additions & 1 deletion backend/substrapp/ledger/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,19 @@ def call_ledger(channel_name, call_type, fcn, *args, **kwargs):
ts = time.time()
error = None
try:
return _call_ledger(channel_name, call_type, fcn, *args, **kwargs)
response = _call_ledger(channel_name, call_type, fcn, *args, **kwargs)

if isinstance(response, dict) and 'bookmark' in response:
results = response['results'] # first results
while response['results'] and len(response['bookmark']) > 0:
kwargs['args'] = {'bookmark': response['bookmark']}
response = _call_ledger(channel_name, call_type, fcn, *args, **kwargs)
results.extend(response['results']) # following results
else:
response = results

return response

except Exception as e:
error = e.__class__.__name__
raise
Expand Down
14 changes: 13 additions & 1 deletion backend/substrapp/tests/tests_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from substrapp.ledger.exceptions import LedgerAssetNotFound, LedgerInvalidResponse

from substrapp.ledger.api import get_object_from_ledger, log_fail_tuple, log_start_tuple, \
log_success_tuple, query_tuples
log_success_tuple, query_tuples, call_ledger

from .assets import traintuple

import os

Expand Down Expand Up @@ -136,3 +138,13 @@ def test_path_traversal(self):
with self.assertRaises(Exception):
model_dst_path = os.path.join(DIRECTORY, 'model/../../hackermodel')
raise_if_path_traversal([model_dst_path], os.path.join(DIRECTORY, 'model/'))

def test_call_ledger_with_bookmark(self):

with patch('substrapp.ledger.api._call_ledger') as m_call_ledger:
m_call_ledger.side_effect = [
{'results': traintuple[i:i + 2], 'bookmark': f'bookmark_{i}'}
for i in range(0, len(traintuple), 2)
] + [{'results': "", 'bookmark': 'bookmark_end'}]
response = call_ledger(CHANNEL, 'query', 'queryTraintuples')
self.assertEqual(response, traintuple)

0 comments on commit c3a16ad

Please sign in to comment.