Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8 from praekelt/feature/issue-8-add-optional-star…
Browse files Browse the repository at this point in the history
…ting-cursor

Add optional starting cursor
  • Loading branch information
Rudi committed Jan 5, 2015
2 parents 363b1bd + e2ab6d1 commit 55897e9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 10 additions & 2 deletions go_http/contacts.py
Expand Up @@ -51,16 +51,24 @@ def _api_request(self, method, api_collection, api_path, data=None):
r.raise_for_status()
return r.json()

def contacts(self):
def contacts(self, start_cursor=None):
"""
Retrieve all contacts.
This uses the API's paginated contact download.
:param start_cursor:
An optional parameter that declares the cursor to start fetching
the contacts from.
:returns:
An iterator over all contacts.
"""
page = self._api_request("GET", "contacts", "")
if start_cursor:
page = self._api_request(
"GET", "contacts", "?cursor=%s" % start_cursor)
else:
page = self._api_request("GET", "contacts", "")
while True:
for contact in page['data']:
yield contact
Expand Down
22 changes: 22 additions & 0 deletions go_http/tests/test_contacts.py
Expand Up @@ -156,6 +156,28 @@ def test_contacts_multiple_pages(self):
self.assertEqual(
sort_by_msidn(contacts), sort_by_msidn(expected_contacts))

def test_contacts_multiple_pages_with_cursor(self):
expected_contacts = []
for i in range(self.MAX_CONTACTS_PER_PAGE):
expected_contacts.append(self.make_existing_contact({
u"msisdn": u"+155564%d" % (i,),
u"name": u"Arthur",
u"surname": u"of Camelot",
}))
expected_contacts.append(self.make_existing_contact({
u"msisdn": u"+15556",
u"name": u"Arthur",
u"surname": u"of Camelot",
}))
contacts_api = self.make_client()
first_page = contacts_api._api_request("GET", "contacts", "")
cursor = first_page['cursor']
contacts = list(contacts_api.contacts(start_cursor=cursor))
contacts.extend(first_page['data'])
sort_by_msidn = lambda l: sorted(l, key=lambda d: d['msisdn'])
self.assertEqual(
sort_by_msidn(contacts), sort_by_msidn(expected_contacts))

def test_create_contact(self):
contacts = self.make_client()
contact_data = {
Expand Down

0 comments on commit 55897e9

Please sign in to comment.