Skip to content

Commit 9a7adea

Browse files
author
Jian Cheung
committed
Changed iter method to use cursor pagination API-92
1 parent f98ba71 commit 9a7adea

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

clever/__init__.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -694,30 +694,38 @@ def instance_url(self):
694694
# Classes of API operations
695695

696696

697+
def get_link(response, rel):
698+
links = response.get('links', None)
699+
for link in links:
700+
if link.get('rel', None) == rel:
701+
return link.get('uri')
702+
return None
703+
697704
class ListableAPIResource(APIResource):
705+
ITER_LIMIT = 1000
698706

699707
@classmethod
700708
def all(cls, auth=None, **params):
701709
return list(cls.iter(auth, **params))
702710

703711
@classmethod
704712
def iter(cls, auth=None, **params):
705-
for unsupported_param in ['limit', 'page']:
713+
for unsupported_param in ['limit', 'page', 'starting_after', 'ending_before']:
706714
if unsupported_param in params:
707715
raise CleverError("ListableAPIResource does not support '%s' parameter" %
708716
(unsupported_param,))
709717

710718
requestor = APIRequestor(auth)
711719
url = cls.class_url()
712-
params['page'] = 1
713-
params['limit'] = 1000
714-
done_paginating = False
715-
while not done_paginating:
720+
params['limit'] = cls.ITER_LIMIT
721+
722+
while url:
716723
response, auth = requestor.request('get', url, params)
717724
for datum in convert_to_clever_object(cls, response, auth):
718725
yield datum
719-
params['page'] += 1
720-
done_paginating = len(response['data']) < params['limit']
726+
url = get_link(response, 'next')
727+
# params already included in url from get_link
728+
params = {}
721729

722730

723731
class CreatableAPIResource(APIResource):

test/test_clever.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,23 @@ def test_iter(self):
7676
for district in clever.District.iter():
7777
self.assertTrue(district.id)
7878

79+
def test_iter_count(self):
80+
r = requests.get('https://api.clever.com/v1.1/students?count=true',
81+
headers={'Authorization': 'Bearer DEMO_TOKEN'})
82+
83+
req_count = json.loads(r.text)["count"]
84+
iter_count = len([x for x in clever.Student.iter()])
85+
86+
self.assertTrue(req_count > clever.Student.ITER_LIMIT)
87+
self.assertEqual(req_count, iter_count)
88+
7989
def test_unsupported_params(self):
8090
self.assertRaises(clever.CleverError, lambda: clever.District.all(page=2))
8191
self.assertRaises(clever.CleverError, lambda: clever.District.all(limit=10))
92+
self.assertRaises(clever.CleverError, lambda: clever.District.all(
93+
starting_after='4fd43cc56d11340000000005'))
94+
self.assertRaises(clever.CleverError, lambda: clever.District.all(
95+
ending_before='4fd43cc56d11340000000005'))
8296
self.assertRaises(clever.CleverError, lambda: clever.District.all(page=2, limit=10))
8397

8498
def test_unicode(self):

0 commit comments

Comments
 (0)