diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8df36c5..90845c5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,8 @@ +Changelog +--------- +2014-12-29: v1.4.0 + Add a new endpoint to get broadcasts by list_id and status + 2014-11-17: v1.3.0 * Add a new endpoint to cancel scheduled broadcast diff --git a/aweber_api/entry.py b/aweber_api/entry.py index ce50e52..8e34faf 100644 --- a/aweber_api/entry.py +++ b/aweber_api/entry.py @@ -126,7 +126,7 @@ def schedule_broadcast(self, bc_id, scheduled_for): * Note: This method only works on List Entry resources and - requires access to subscriber information. Please + requires send broadcast email permissions. Please refer to the AWeber API Reference Documentation at https://labs.aweber.com/docs/reference/1.0#broadcast_scheduler for more details on how to call this method. @@ -137,16 +137,37 @@ def schedule_broadcast(self, bc_id, scheduled_for): url = '{0}/broadcasts/{1}/schedule'.format(self.url, bc_id) return self.adapter.request('POST', url, body, response='status') + def get_broadcasts(self, status, **kwargs): + """Invoke the API method to retrieve broadcasts by status. + + * Note: + This method only works on List Entry resources. Please + refer to the AWeber API Reference Documentation at + https://labs.aweber.com/docs/reference/1.0#get_broadcasts + for more details on how to call this method. + + """ + self._method_for('list') + params = {'status': status} + params.update(kwargs) + query_string = urlencode(params) + url = '{0.url}/broadcasts?{1}'.format(self, query_string) + + data = self.adapter.request('GET', url) + collection = aweber_api.AWeberCollection(url, data, self.adapter) + collection._data['total_size'] = self._get_broadcast_count( + query_string) + return collection + def cancel_broadcast(self, bc_id): """Invoke the API method to cancel the given scheduled broadcast. * Note: This method only works on List Entry resources and - requires access to subscriber and send broadcast - information. Please refer to the AWeber API Reference - Documentation at + requires send broadcast email permissions. Please refer + to the AWeber API Reference Documentation at https://labs.aweber.com/docs/reference/1.0#cancel_broadcast - more details on how to call this method. + for more details on how to call this method. """ self._method_for('list') @@ -158,6 +179,12 @@ def _get_total_size(self, uri, **kwargs): total_size_uri = '{0}&ws.show=total_size'.format(uri) return int(self.adapter.request('GET', total_size_uri)) + def _get_broadcast_count(self, query_string): + """Get actual total size number from total_size_link.""" + total_size_uri = '{0.url}/broadcasts/total?{1}'.format( + self, query_string) + return int(self.adapter.request('GET', total_size_uri)['total_size']) + def get_parent_entry(self): """Return the parent entry of this entry diff --git a/setup.py b/setup.py index bcb75f3..36470a2 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name='aweber_api', - version='1.3.0', + version='1.4.0', author='AWeber Dev Team', author_email='api@aweber.com', maintainer='AWeber API Team', diff --git a/tests/mock_adapter.py b/tests/mock_adapter.py index 5e48dca..4b03f43 100644 --- a/tests/mock_adapter.py +++ b/tests/mock_adapter.py @@ -48,6 +48,10 @@ 'email=joe%40example.com': ({}, 'subscribers/find'), '/accounts/1/lists/303449/subscribers?ws.show=total_size&ws.op=find&' \ 'email=joe%40example.com': ({}, 'subscribers/find_ts'), + '/accounts/1/lists/303449/broadcasts/total?status=sent': ( + {'total_size': 10}, 'campaigns/303449'), + '/accounts/1/lists/303449/broadcasts?status=sent': ( + {'total_size': 10}, 'campaigns/303449'), }, 'POST' : { '/accounts/1/lists/303449/any_collection': ({ diff --git a/tests/test_aweber_entry.py b/tests/test_aweber_entry.py index ee14b75..eb73232 100644 --- a/tests/test_aweber_entry.py +++ b/tests/test_aweber_entry.py @@ -198,6 +198,27 @@ def test_should_raise_exception_when_failing(self): ) +class TestListGetBroadcasts(ListTestCase): + + def setUp(self): + super(TestListGetBroadcasts, self).setUp() + self.aweber.adapter.requests = [] + self.broadcasts = self.list_.get_broadcasts(status='sent') + self.request = self.aweber.adapter.requests[0] + + def test_should_return_collection(self): + self.assertEqual(type(self.broadcasts), AWeberCollection) + + def test_should_make_get_request(self): + self.assertEqual(self.request['method'], 'GET') + + def test_should_build_correct_url(self): + self.assertEqual( + self.request['url'], + '/accounts/1/lists/303449/broadcasts?status=sent' + ) + + class SubscriberTestCase(TestCase): def setUp(self):