Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Commit 8eebb40

Browse files
committed
Merge pull request #33 from Clever/follow-links
Follow links
2 parents 41c16a0 + 04fa033 commit 8eebb40

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ If you'd like more control over pagination, or to limit the number of resources
5656
print students.next()
5757
```
5858

59+
You may also use the `starting_after` or `ending_before` parameters with the `iter` method:
60+
61+
```python
62+
students = clever.Student.iter(starting_after="530e5960049e75a9262cff1d")
63+
for s in students:
64+
print students.next()
65+
```
66+
5967
The `retrieve` class method takes in a Clever ID and returns a specific resource. The object (or list of objects in the case of `all`) supports accessing properties using either dot notation or dictionary notation:
6068

6169
```python

clever/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,20 +710,21 @@ def all(cls, auth=None, **params):
710710

711711
@classmethod
712712
def iter(cls, auth=None, **params):
713-
for unsupported_param in ['limit', 'page', 'starting_after', 'ending_before']:
713+
for unsupported_param in ['limit', 'page']:
714714
if unsupported_param in params:
715715
raise CleverError("ListableAPIResource does not support '%s' parameter" %
716716
(unsupported_param,))
717717

718718
requestor = APIRequestor(auth)
719719
url = cls.class_url()
720-
params['limit'] = cls.ITER_LIMIT
720+
params['limit'] = cls.ITER_LIMIT
721721

722722
while url:
723723
response, auth = requestor.request('get', url, params)
724724
for datum in convert_to_clever_object(cls, response, auth):
725725
yield datum
726-
url = get_link(response, 'next')
726+
727+
url = get_link(response, 'prev' if 'ending_before' in params else 'next')
727728
# params already included in url from get_link
728729
params = {}
729730

test/test_clever.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import unittest
55
import httmock
6+
import itertools
67

78
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
89
import clever
@@ -36,9 +37,23 @@ def test_list_accessors(self):
3637
district = clever.District.all()[0]
3738
self.assertEqual(district['name'], district.name)
3839

40+
def test_starting_after(self):
41+
allevents = clever.Event.all()
42+
second_to_last_id = allevents[len(allevents)-2]['id']
43+
events = clever.Event.iter(starting_after=second_to_last_id)
44+
count = len(list(events))
45+
self.assertTrue(count==1)
46+
47+
def test_ending_before(self):
48+
allevents = clever.Event.all()
49+
second_id = allevents[1]['id']
50+
events = clever.Event.iter(ending_before=second_id)
51+
count = len(list(events))
52+
self.assertTrue(count==1)
53+
3954
def test_unicode(self):
4055
# Make sure unicode requests can be sent
41-
self.assertRaises(clever.InvalidRequestError, clever.District.retrieve, id=u'☃')
56+
self.assertRaises(clever.APIError, clever.District.retrieve, id=u'☃')
4257

4358
def test_none_values(self):
4459
district = clever.District.all(sort=None)[0]
@@ -76,6 +91,20 @@ def test_iter(self):
7691
for district in clever.District.iter():
7792
self.assertTrue(district.id)
7893

94+
def test_starting_after(self):
95+
allevents = clever.Event.all()
96+
second_to_last_id = allevents[len(allevents)-2]['id']
97+
events = clever.Event.iter(starting_after=second_to_last_id)
98+
count = len(list(events))
99+
self.assertTrue(count==1)
100+
101+
def test_ending_before(self):
102+
allevents = clever.Event.all()
103+
second_id = allevents[1]['id']
104+
events = clever.Event.iter(ending_before=second_id)
105+
count = len(list(events))
106+
self.assertTrue(count==1)
107+
79108
def test_iter_count(self):
80109
r = requests.get('https://api.clever.com/v1.1/students?count=true',
81110
headers={'Authorization': 'Bearer DEMO_TOKEN'})
@@ -89,10 +118,6 @@ def test_iter_count(self):
89118
def test_unsupported_params(self):
90119
self.assertRaises(clever.CleverError, lambda: clever.District.all(page=2))
91120
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'))
96121
self.assertRaises(clever.CleverError, lambda: clever.District.all(page=2, limit=10))
97122

98123
def test_unicode(self):
@@ -160,7 +185,7 @@ def test_rate_limiter(self):
160185
suite = unittest.TestSuite()
161186
for TestClass in [
162187
functional_test({"api_key": "DEMO_KEY"}),
163-
functional_test({"token": "7f76343d50b9e956138169e8cbb4630bb887b18"}),
188+
functional_test({"token": "DEMO_TOKEN"}),
164189
AuthenticationErrorTest,
165190
InvalidRequestErrorTest,
166191
TooManyRequestsErrorTest]:

0 commit comments

Comments
 (0)