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

Commit

Permalink
Merge tag 'go-http-api-0.2.4' into release/0.2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudi Giesler committed Mar 25, 2015
2 parents 2969265 + c945925 commit 5408808
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go_http/__init__.py
Expand Up @@ -2,7 +2,7 @@

from .send import HttpApiSender, LoggingSender

__version__ = "0.2.3"
__version__ = "0.2.4"

__all__ = [
'HttpApiSender', 'LoggingSender',
Expand Down
32 changes: 32 additions & 0 deletions go_http/contacts.py
Expand Up @@ -12,6 +12,7 @@


class ContactsApiClient(object):

"""
Client for Vumi Go's contacts API.
Expand Down Expand Up @@ -182,3 +183,34 @@ def delete_group(self, group_key):
Key for the group to delete.
"""
return self._api_request("DELETE", "groups", group_key)

def group_contacts(self, group_key, start_cursor=None):
"""
Retrieve all group contacts.
This uses the API's paginated contact download.
:param str group_key
Key for the group to query.
:param start_cursor:
An optional parameter that declares the cursor to start fetching
the contacts from.
:returns:
An iterator over all group contacts.
"""
if start_cursor:
page = self._api_request(
"GET", "groups/%s" % group_key,
"contacts?cursor=%s" % start_cursor)
else:
page = self._api_request(
"GET", "groups/%s" % group_key, "contacts")
while True:
for contact in page['data']:
yield contact
if page['cursor'] is None:
break
page = self._api_request(
"GET", "groups/%s" % group_key, "contacts?cursor=%s" %
page['cursor'])
82 changes: 82 additions & 0 deletions go_http/tests/test_contacts.py
Expand Up @@ -14,6 +14,7 @@


class FakeContactsApiAdapter(HTTPAdapter):

"""
Adapter for FakeContactsApi.
Expand Down Expand Up @@ -445,3 +446,84 @@ def test_delete_group(self):
def test_delete_missing_group(self):
client = self.make_client()
self.assert_http_error(404, client.delete_group, 'foo')

def test_group_contacts_multiple_pages_with_cursor(self):
client = self.make_client()
self.make_existing_group({
u'name': 'key',
})
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",
u"groups": ["key"],
}))
expected_contacts.append(self.make_existing_contact({
u"msisdn": u"+15556",
u"name": u"Arthur",
u"surname": u"of Camelot",
u"groups": ["key"],
}))
first_page = client._api_request("GET", "groups/key", "contacts")
cursor = first_page['cursor']
contacts = list(client.group_contacts(group_key="key",
start_cursor=cursor))
contacts.extend(first_page['data'])
contacts.sort(key=lambda d: d['msisdn'])
expected_contacts.sort(key=lambda d: d['msisdn'])
self.assertEqual(contacts, expected_contacts)

def test_group_contacts_multiple_pages(self):
expected_contacts = []
self.make_existing_group({
u'name': 'key',
})
self.make_existing_group({
u'name': 'diffkey',
})
for i in range(self.MAX_CONTACTS_PER_PAGE + 1):
expected_contacts.append(self.make_existing_contact({
u"msisdn": u"+155564%d" % (i,),
u"name": u"Arthur",
u"surname": u"of Camelot",
u"groups": ["key"],
}))
self.make_existing_contact({
u"msisdn": u"+1234567",
u"name": u"Nancy",
u"surname": u"of Camelot",
u"groups": ["diffkey"],
})
client = self.make_client()
contacts = list(client.group_contacts("key"))

contacts.sort(key=lambda d: d['msisdn'])
expected_contacts.sort(key=lambda d: d['msisdn'])

self.assertEqual(contacts, expected_contacts)

def test_group_contacts_none_found(self):
expected_contacts = []
other_group_contacts = []
self.make_existing_group({
u'name': 'key',
})
self.make_existing_group({
u'name': 'diffkey',
})
for i in range(self.MAX_CONTACTS_PER_PAGE + 1):
other_group_contacts.append(self.make_existing_contact({
u"msisdn": u"+155564%d" % (i,),
u"name": u"Arthur",
u"surname": u"of Camelot",
u"groups": ["diffkey"],
}))
client = self.make_client()
contacts = list(client.group_contacts("key"))

contacts.sort(key=lambda d: d['msisdn'])
expected_contacts.sort(key=lambda d: d['msisdn'])

self.assertEqual(contacts, expected_contacts)
2 changes: 1 addition & 1 deletion requirements-dev.txt
Expand Up @@ -4,4 +4,4 @@ pytest
pytest-xdist
pytest-cov
requests_testadapter
fake-go-contacts>=0.1.8
fake-go-contacts>=0.1.9
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -2,7 +2,7 @@

setup(
name="go_http",
version="0.2.3",
version="0.2.4",
url='http://github.com/praekelt/go-http-api',
license='BSD',
description="A client library for Vumi Go's HTTP API",
Expand Down

0 comments on commit 5408808

Please sign in to comment.