From 63b2ffc41c1cc0d8fb67a60e333f95714db63d7f Mon Sep 17 00:00:00 2001 From: Rudi Giesler Date: Mon, 5 Jan 2015 15:37:33 +0200 Subject: [PATCH 1/2] Added optional start_cursor parameter to the contacts function --- go_http/contacts.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/go_http/contacts.py b/go_http/contacts.py index f4eff5a..20dc062 100644 --- a/go_http/contacts.py +++ b/go_http/contacts.py @@ -51,16 +51,20 @@ 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=""): """ 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", "") + page = self._api_request("GET", "contacts", start_cursor) while True: for contact in page['data']: yield contact From e2ab6d131d4bc1afc5890629ce4c367090a01cde Mon Sep 17 00:00:00 2001 From: Rudi Giesler Date: Mon, 5 Jan 2015 16:09:43 +0200 Subject: [PATCH 2/2] Add test for starting getting contacts from a cursor --- go_http/contacts.py | 8 ++++++-- go_http/tests/test_contacts.py | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/go_http/contacts.py b/go_http/contacts.py index 20dc062..46024b1 100644 --- a/go_http/contacts.py +++ b/go_http/contacts.py @@ -51,7 +51,7 @@ def _api_request(self, method, api_collection, api_path, data=None): r.raise_for_status() return r.json() - def contacts(self, start_cursor=""): + def contacts(self, start_cursor=None): """ Retrieve all contacts. @@ -64,7 +64,11 @@ def contacts(self, start_cursor=""): :returns: An iterator over all contacts. """ - page = self._api_request("GET", "contacts", start_cursor) + 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 diff --git a/go_http/tests/test_contacts.py b/go_http/tests/test_contacts.py index 7cceab3..f3ed000 100644 --- a/go_http/tests/test_contacts.py +++ b/go_http/tests/test_contacts.py @@ -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 = {