Skip to content

Commit 14bcdc4

Browse files
committed
added support for batch adding activities
1 parent 49a8a6e commit 14bcdc4

5 files changed

Lines changed: 43 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ user_feed_1.follow('flat:42')
4545
# Stop following another feed
4646
user_feed_1.unfollow('flat:42')
4747

48+
# Batch adding activities
49+
activities = [
50+
{'actor': 1, 'verb': 'tweet', 'object': 1},
51+
{'actor': 2, 'verb': 'watch', 'object': 3}
52+
]
53+
user_feed_1.add_activities(activities)
54+
4855
# Generating tokens for client side usage
4956
token = user_feed_1.token
5057
# Javascript client side feed initialization

‎stream/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
__copyright__ = 'Copyright 2012, Thierry Schellenbach'
55
__credits__ = ['Thierry Schellenbach, mellowmorning.com, @tschellenbach']
66
__license__ = 'BSD'
7-
__version__ = '0.2.4'
7+
__version__ = '0.2.5'
88
__maintainer__ = 'Thierry Schellenbach'
99
__email__ = 'thierryschellenbach@gmail.com'
1010
__status__ = 'Production'

‎stream/client.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from stream.signing import sign
55
from stream.utils import validate_feed
66
import os
7+
import json
78

89
logger = logging.getLogger(__name__)
910

@@ -73,10 +74,12 @@ def _make_request(self, method, relative_url, authorization, params=None, data=N
7374
default_params = self.get_default_params()
7475
default_params.update(params)
7576

76-
headers = headers = {'Authorization': authorization}
77+
headers = {'Authorization': authorization}
78+
headers['Content-type'] = 'application/json'
79+
7780
url = self.base_url + relative_url
7881

79-
response = method(url, data=data, headers=headers,
82+
response = method(url, data=json.dumps(data), headers=headers,
8083
params=default_params)
8184
logger.debug('stream api call %s, headers %s data %s',
8285
response.url, headers, data)

‎stream/feed.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ def add_activity(self, activity_data):
3434
result = self.client.post(
3535
self.feed_url, data=activity_data, authorization=self.authorization)
3636
return result
37+
38+
def add_activities(self, activity_list):
39+
'''
40+
Adds a list of activities to the feed
41+
42+
:param activity_list: a list with the activity data dicts
43+
44+
**Example**::
45+
46+
activity_data = [
47+
{'actor': 1, 'verb': 'tweet', 'object': 1},
48+
{'actor': 2, 'verb': 'watch', 'object': 2},
49+
]
50+
result = feed.add_activities(activity_data)
51+
'''
52+
data = dict(activities=activity_list)
53+
result = self.client.post(
54+
self.feed_url, data=data, authorization=self.authorization)
55+
return result
3756

3857
def remove_activity(self, activity_id):
3958
'''

‎stream/tests.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ def test_remove_activity(self):
6262
self.user1.remove_activity(activity_id)
6363
activities = self.user1.get(limit=1)['results']
6464
self.assertNotEqual(activities[0]['id'], activity_id)
65+
66+
def test_add_activities(self):
67+
activity_data = [
68+
{'actor': 1, 'verb': 'tweet', 'object': 1},
69+
{'actor': 2, 'verb': 'watch', 'object': 2},
70+
]
71+
response = self.user1.add_activities(activity_data)
72+
activity_ids = [a['id'] for a in response['activities']]
73+
activities = self.user1.get(limit=2)['results']
74+
get_activity_ids = [a['id'] for a in activities]
75+
self.assertEqual(get_activity_ids, activity_ids[::-1])
6576

6677
def test_follow(self):
6778
actor_id = random.randint(10, 100000)

0 commit comments

Comments
 (0)