Skip to content
This repository has been archived by the owner on Aug 29, 2019. It is now read-only.

Commit

Permalink
Add get_broadcasts
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly O'Brien authored and Vraj Mohan committed Jan 12, 2015
1 parent 4a6d37e commit 98daedb
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -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

Expand Down
37 changes: 32 additions & 5 deletions aweber_api/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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')
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='aweber_api',
version='1.3.0',
version='1.4.0',
author='AWeber Dev Team',
author_email='[email protected]',
maintainer='AWeber API Team',
Expand Down
4 changes: 4 additions & 0 deletions tests/mock_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': ({
Expand Down
21 changes: 21 additions & 0 deletions tests/test_aweber_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 98daedb

Please sign in to comment.