Skip to content

Commit 93cf009

Browse files
committed
add support for following and followers APIs
1 parent 34e8a96 commit 93cf009

2 files changed

Lines changed: 75 additions & 3 deletions

File tree

‎stream/feed.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,34 @@ def follow(self, target_feed):
8484
url, data=dict(target=target_feed), authorization=self.authorization)
8585
return response
8686

87+
def parse_follow_data(self, response):
88+
return {
89+
'count': response['count'],
90+
'results': response['results']
91+
}
92+
93+
def followers(self, limit=100, page=1):
94+
params = {
95+
'limit': limit,
96+
'page': page
97+
}
98+
url = self.feed_url + 'followers/'
99+
response = self.client.get(
100+
url, params=params, authorization=self.authorization)
101+
return self.parse_follow_data(response)
102+
103+
def following(self, limit=100, page=1, feeds=None):
104+
feeds = feeds is not None and ','.join(feeds) or ''
105+
params = {
106+
'limit': limit,
107+
'page': page,
108+
'filter': feeds
109+
}
110+
url = self.feed_url + 'follows/'
111+
response = self.client.get(
112+
url, params=params, authorization=self.authorization)
113+
return self.parse_follow_data(response)
114+
87115
def unfollow(self, target_feed):
88116
'''
89117
Unfollow the given feed

‎stream/tests.py‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,53 @@ def test_unfollow(self):
132132
activity_id_found = activity['id'] if activity is not None else None
133133
self.assertNotEqual(activity_id_found, activity_id)
134134

135+
def test_empty_followings(self):
136+
asocial = client.feed('user:asocialpython')
137+
followings = asocial.following()
138+
self.assertEqual(followings['count'], 0)
139+
self.assertEqual(followings['results'], [])
140+
141+
def test_get_followings(self):
142+
social = client.feed('user:psocial')
143+
social.follow('user:apy')
144+
social.follow('user:bpy')
145+
followings = social.following(limit=1, page=2)
146+
self.assertEqual(followings['count'], 2)
147+
self.assertEqual(followings['results'][0]['feed_id'], 'user:psocial')
148+
self.assertEqual(followings['results'][0]['target_id'], 'user:apy')
149+
150+
def test_empty_followers(self):
151+
asocial = client.feed('user:asocialpython')
152+
followers = asocial.following()
153+
self.assertEqual(followers['count'], 0)
154+
self.assertEqual(followers['results'], [])
155+
156+
def test_get_followers(self):
157+
social = client.feed('user:psocial')
158+
client.feed('user:spammy1').follow('user:psocial')
159+
client.feed('user:spammy2').follow('user:psocial')
160+
followers = social.followers(limit=1, page=2)
161+
self.assertEqual(followers['count'], 2)
162+
self.assertEqual(followers['results'][0]['feed_id'], 'user:spammy1')
163+
self.assertEqual(followers['results'][0]['target_id'], 'user:psocial')
164+
165+
def test_empty_do_i_follow(self):
166+
social = client.feed('user:psocial')
167+
social.follow('user:apy')
168+
social.follow('user:bpy')
169+
followings = social.following(feeds=['user:cpy'])
170+
self.assertEqual(followings['count'], 0)
171+
self.assertEqual(followings['results'], [])
172+
173+
def test_do_i_follow(self):
174+
social = client.feed('user:psocial')
175+
social.follow('user:apy')
176+
social.follow('user:bpy')
177+
followings = social.following(feeds=['user:apy'])
178+
self.assertEqual(followings['count'], 1)
179+
self.assertEqual(followings['results'][0]['feed_id'], 'user:psocial')
180+
self.assertEqual(followings['results'][0]['target_id'], 'user:apy')
181+
135182
def test_get(self):
136183
activity_data = {'actor': 1, 'verb': 'tweet', 'object': 1}
137184
activity_id = self.user1.add_activity(activity_data)['id']
@@ -252,6 +299,3 @@ def test_serialization(self):
252299
serialized = serializer.dumps(data)
253300
loaded = serializer.loads(serialized)
254301
self.assertEqual(data, loaded)
255-
256-
257-

0 commit comments

Comments
 (0)