From 570d52b8e239c49228cbb7eca4654c8a7b767b1a Mon Sep 17 00:00:00 2001 From: Yitao Xiong Date: Fri, 16 Mar 2018 13:54:24 -0400 Subject: [PATCH 1/2] Added a get_contacts_by_list_id method in the client --- hapi/contact_lists.py | 9 ++++ hapi/test/test_contact_lists.py | 82 ++++++++++++++++++++------------- 2 files changed, 59 insertions(+), 32 deletions(-) diff --git a/hapi/contact_lists.py b/hapi/contact_lists.py index 4f9486f..6ce2ea4 100644 --- a/hapi/contact_lists.py +++ b/hapi/contact_lists.py @@ -22,6 +22,15 @@ def get_contact_lists(self, **options): """ Returns all of the contact lists """ return self._call('lists', method='GET', **options) + def get_contacts_by_list_id(self, list_id, query='', **options): + """ Get all contacts in the specified list """ + return self._call( + 'lists/{list_id}/contacts/all'.format(list_id=list_id), + method='GET', + query=query, + **options + ) + def add_contact_to_a_list(self, list_id, vids, data=None, **options): """ Adds a list of contact vids to the specified list. """ data = data or {} diff --git a/hapi/test/test_contact_lists.py b/hapi/test/test_contact_lists.py index e77cf3e..fa3cce6 100644 --- a/hapi/test/test_contact_lists.py +++ b/hapi/test/test_contact_lists.py @@ -9,7 +9,7 @@ from test_contacts import ContactsClientTestCase -class ConstactListsClientTestCase(unittest2.TestCase): +class ContactsListsClientTestCase(unittest2.TestCase): """ Unit tests for the HubSpot Contact Lists API Python client. @@ -21,17 +21,17 @@ class ConstactListsClientTestCase(unittest2.TestCase): test_portal_id = 62515 def setUp(self): - self.client = ContactListsClient(**helper.get_options()) - self.contacts_client = ContactsClient(**helper.get_options()) - self.lists = [] - self.contacts =[] + self.client = ContactListsClient(**helper.get_options()) + self.contacts_client = ContactsClient(**helper.get_options()) + self.lists = [] + self.contacts =[] def tearDown(self): - """ Clean up all the created objects. """ - if self.contacts: - [self.contacts_client.delete_a_contact(contact) for contact in self.contacts] - if self.lists: - [self.client.delete_a_contact_list(list) for list in self.lists] + """ Clean up all the created objects. """ + if self.contacts: + [self.contacts_client.delete_a_contact(contact) for contact in self.contacts] + if self.lists: + [self.client.delete_a_contact_list(list) for list in self.lists] @attr('api') def test_get_contact_lists(self): @@ -39,34 +39,52 @@ def test_get_contact_lists(self): response = self.client.get_contact_lists() self.assertTrue(len(response) > 0) + def test_get_contacts_by_list_id(self): + """ Test that get contacts in a list is returning the right contacts """ + email = ContactsClientTestCase.test_contact_json['properties'][0]['value'] + contact = self.contacts_client.create_or_update_a_contact( + email, + data=ContactsClientTestCase.test_contact_json + )['vid'] + self.contacts.append(contact) + contact_list = self.client.create_a_contact_list( + list_name='test_add_contact_to_a_list' + str(random.randint(1000, 50000)), + portal_id=self.test_portal_id, + dynamic=False) + self.lists.append(contact_list['listId']) + self.client.add_contact_to_a_list(contact_list['listId'], [contact]) + response = self.client.get_contacts_by_list_id(contact_list['listId']) + self.assertEqual(len(response['contacts']), 1) + self.assertEqual(response['contacts'][0]['vid'], contact) + @attr('api') def test_add_contact_to_a_list(self): - """ Test that the add contact to a list endpoint is valid. """ - email = ContactsClientTestCase.test_contact_json['properties'][0]['value'] - contact = self.contacts_client.create_or_update_a_contact(email, data=ContactsClientTestCase.test_contact_json)['vid'] - self.contacts.append(contact) - contact_list = self.client.create_a_contact_list(list_name='test_add_contact_to_a_list' + str(random.randint(1000, 50000)), - portal_id=self.test_portal_id, - dynamic=False) - self.lists.append(contact_list['listId']) - - response = self.client.add_contact_to_a_list(contact_list['listId'], [contact]) - self.assertTrue(len(response) > 0) + """ Test that the add contact to a list endpoint is valid. """ + email = ContactsClientTestCase.test_contact_json['properties'][0]['value'] + contact = self.contacts_client.create_or_update_a_contact(email, data=ContactsClientTestCase.test_contact_json)['vid'] + self.contacts.append(contact) + contact_list = self.client.create_a_contact_list(list_name='test_add_contact_to_a_list' + str(random.randint(1000, 50000)), + portal_id=self.test_portal_id, + dynamic=False) + self.lists.append(contact_list['listId']) + + response = self.client.add_contact_to_a_list(contact_list['listId'], [contact]) + self.assertTrue(len(response) > 0) def test_create_a_contact_list(self): - """ Test that the create contact list endpoint is valid. """ - response = self.client.create_a_contact_list(list_name='test_create_a_contact_list' + str(random.randint(1000, 50000)), - portal_id=self.test_portal_id, - dynamic=False) - self.assertTrue(len(response) > 0) + """ Test that the create contact list endpoint is valid. """ + response = self.client.create_a_contact_list(list_name='test_create_a_contact_list' + str(random.randint(1000, 50000)), + portal_id=self.test_portal_id, + dynamic=False) + self.assertTrue(len(response) > 0) - self.lists.append(response['listId']) + self.lists.append(response['listId']) def test_delete_a_contact_list(self): - """ Test that the delete contact list endpoint is valid. """ - contact_list = self.client.create_a_contact_list(list_name='test_delete_a_contact_list' + str(random.randint(1000, 50000)), - portal_id=self.test_portal_id, - dynamic=False) + """ Test that the delete contact list endpoint is valid. """ + contact_list = self.client.create_a_contact_list(list_name='test_delete_a_contact_list' + str(random.randint(1000, 50000)), + portal_id=self.test_portal_id, + dynamic=False) - response = self.client.delete_a_contact_list(contact_list['listId']) + response = self.client.delete_a_contact_list(contact_list['listId']) From e9ec9e80c748c19281d8080a9494494d551c59d4 Mon Sep 17 00:00:00 2001 From: Yitao Xiong Date: Fri, 16 Mar 2018 13:57:53 -0400 Subject: [PATCH 2/2] Bump the version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 2c0d1e9..d339576 100755 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='hapipy', - version='2.10.5.3', + version='2.10.5.4', description="A python wrapper around HubSpot's APIs", long_description=open('README.md').read(), author='HubSpot Dev Team',