Skip to content

Commit cb6fd7f

Browse files
committed
fixed followers/following implementation and testing
1 parent a9cb5c7 commit cb6fd7f

7 files changed

Lines changed: 80 additions & 71 deletions

File tree

‎setup.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def finalize_options(self):
4141
def run_tests(self):
4242
# import here, cause outside the eggs aren't loaded
4343
import pytest
44-
errno = pytest.main('stream/tests.py --cov stream --cov-report term-missing -v')
44+
errno = pytest.main(
45+
'stream/tests.py --cov stream --cov-report term-missing -v')
4546
sys.exit(errno)
4647

4748
setup(

‎stream/__init__.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import re, os
1+
import re
2+
import os
23

34
__author__ = 'Thierry Schellenbach'
45
__copyright__ = 'Copyright 2012, Thierry Schellenbach'
@@ -22,13 +23,12 @@ def connect(api_key=None, api_secret=None, site_id=None):
2223
stream_url = os.environ.get('STREAM_URL')
2324
# support for the heroku STREAM_URL syntax
2425
if stream_url and not api_key:
25-
pattern = re.compile('https\:\/\/(\w+)\:(\w+).*site=(\d+)', re.IGNORECASE)
26+
pattern = re.compile(
27+
'https\:\/\/(\w+)\:(\w+).*site=(\d+)', re.IGNORECASE)
2628
result = pattern.match(stream_url)
27-
if result and len(result.groups())==3:
29+
if result and len(result.groups()) == 3:
2830
api_key, api_secret, site_id = result.groups()
2931
else:
3032
raise ValueError('Invalid api key or heroku url')
31-
32-
return StreamClient(api_key, api_secret, site_id)
33-
3433

34+
return StreamClient(api_key, api_secret, site_id)

‎stream/client.py‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import datetime
1313

1414

15-
16-
1715
class StreamClient(object):
1816
base_url = 'https://getstream.io/api/'
1917

@@ -58,11 +56,11 @@ def feed(self, feed_id):
5856
'''
5957
validate_feed(feed_id)
6058
from stream.feed import Feed
61-
59+
6260
# generate the token
6361
feed_together = feed_id.replace(':', '')
6462
token = sign(self.api_secret, feed_together)
65-
63+
6664
return Feed(self, feed_id, token)
6765

6866
def get_default_params(self):
@@ -71,7 +69,7 @@ def get_default_params(self):
7169
'''
7270
params = dict(api_key=self.api_key)
7371
return params
74-
72+
7573
def _make_request(self, method, relative_url, authorization, params=None, data=None):
7674
params = params or {}
7775
data = data or {}
@@ -81,7 +79,7 @@ def _make_request(self, method, relative_url, authorization, params=None, data=N
8179

8280
headers = {'Authorization': authorization}
8381
headers['Content-type'] = 'application/json'
84-
82+
8583
url = self.base_url + relative_url
8684

8785
serialized = serializer.dumps(data)
@@ -104,7 +102,8 @@ def raise_exception(self, result, status_code):
104102
if exception_fields is not None:
105103
errors = []
106104
for field, errors in exception_fields.items():
107-
errors.append('Field "%s" errors: %s' % (field, ','.join(errors)))
105+
errors.append('Field "%s" errors: %s' %
106+
(field, ','.join(errors)))
108107
error_message = '\n'.join(errors)
109108
error_code = result.get('code')
110109
exception_dict = get_exception_dict()

‎stream/feed.py‎

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ class Feed(object):
66
def __init__(self, client, feed_id, token):
77
'''
88
Initializes the Feed class
9-
9+
1010
:param client: the api client
1111
:param feed_id: the feed id (string)
12-
13-
12+
13+
1414
'''
1515
self.client = client
1616
self.feed_id = feed_id
@@ -30,11 +30,11 @@ def add_activity(self, activity_data):
3030
'''
3131
Adds an activity to the feed, this will also trigger an update
3232
to all the feeds which follow this feed
33-
33+
3434
:param activity_data: a dict with the activity data
35-
35+
3636
**Example**::
37-
37+
3838
activity_data = {'actor': 1, 'verb': 'tweet', 'object': 1}
3939
activity_id = feed.add_activity(activity_data)
4040
'''
@@ -44,15 +44,15 @@ def add_activity(self, activity_data):
4444
result = self.client.post(
4545
self.feed_url, data=activity_data, authorization=self.authorization)
4646
return result
47-
47+
4848
def add_activities(self, activity_list):
4949
'''
5050
Adds a list of activities to the feed
51-
51+
5252
:param activity_list: a list with the activity data dicts
53-
53+
5454
**Example**::
55-
55+
5656
activity_data = [
5757
{'actor': 1, 'verb': 'tweet', 'object': 1},
5858
{'actor': 2, 'verb': 'watch', 'object': 2},
@@ -61,7 +61,8 @@ def add_activities(self, activity_list):
6161
'''
6262
for activity_data in activity_list:
6363
if activity_data.get('to'):
64-
activity_data['to'] = self.add_to_signature(activity_data['to'])
64+
activity_data['to'] = self.add_to_signature(
65+
activity_data['to'])
6566

6667
data = dict(activities=activity_list)
6768
result = self.client.post(
@@ -71,7 +72,7 @@ def add_activities(self, activity_list):
7172
def remove_activity(self, activity_id=None, foreign_id=None):
7273
'''
7374
Removes an activity from the feed
74-
75+
7576
:param activity_id: the activity id to remove from this feed
7677
(note this will also remove the activity from feeds which follow this feed)
7778
:param foreign_id: the foreign id you provided when adding the activity
@@ -83,13 +84,14 @@ def remove_activity(self, activity_id=None, foreign_id=None):
8384
params = dict()
8485
if foreign_id is not None:
8586
params['foreign_id'] = '1'
86-
result = self.client.delete(url, authorization=self.authorization, params=params)
87+
result = self.client.delete(
88+
url, authorization=self.authorization, params=params)
8789
return result
8890

8991
def follow(self, target_feed):
9092
'''
9193
Follows the given feed
92-
94+
9395
:param target_feed: the feed to follow, ie flat:3
9496
'''
9597
url = self.feed_url + 'follows/'
@@ -101,12 +103,6 @@ def follow(self, target_feed):
101103
url, data=data, authorization=self.authorization)
102104
return response
103105

104-
def parse_follow_data(self, response):
105-
return {
106-
'count': response['count'],
107-
'results': response['results']
108-
}
109-
110106
def followers(self, offset=0, limit=25):
111107
params = {
112108
'limit': limit,
@@ -115,7 +111,7 @@ def followers(self, offset=0, limit=25):
115111
url = self.feed_url + 'followers/'
116112
response = self.client.get(
117113
url, params=params, authorization=self.authorization)
118-
return self.parse_follow_data(response)
114+
return response
119115

120116
def following(self, offset=0, limit=25, feeds=None):
121117
feeds = feeds is not None and ','.join(feeds) or ''
@@ -127,7 +123,7 @@ def following(self, offset=0, limit=25, feeds=None):
127123
url = self.feed_url + 'follows/'
128124
response = self.client.get(
129125
url, params=params, authorization=self.authorization)
130-
return self.parse_follow_data(response)
126+
return response
131127

132128
def unfollow(self, target_feed):
133129
'''
@@ -141,12 +137,12 @@ def unfollow(self, target_feed):
141137
def get(self, **params):
142138
'''
143139
Get the activities in this feed
144-
140+
145141
**Example**::
146-
142+
147143
# fast pagination using id filtering
148144
feed.get(limit=10, id_lte=100292310)
149-
145+
150146
# slow pagination using offset
151147
feed.get(limit=10, offset=10)
152148
'''

‎stream/serializer.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
The date and datetime formats from the API are automatically supported and parsed
77
'''
88

9+
910
def _datetime_encoder(obj):
1011
if isinstance(obj, (datetime.datetime, datetime.date)):
1112
return obj.isoformat()
@@ -22,13 +23,17 @@ def _datetime_decoder(dict_):
2223

2324
if value is not None and isinstance(value, basestring):
2425
try:
25-
# The api always returns times like this 2014-07-25T09:12:24.735
26-
datetime_obj = datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f" )
26+
# The api always returns times like this
27+
# 2014-07-25T09:12:24.735
28+
datetime_obj = datetime.datetime.strptime(
29+
value, "%Y-%m-%dT%H:%M:%S.%f")
2730
dict_[key] = datetime_obj
2831
except (ValueError, TypeError):
2932
try:
30-
# The api always returns times like this 2014-07-25T09:12:24.735
31-
datetime_obj = datetime.datetime.strptime(value, "%Y-%m-%d" )
33+
# The api always returns times like this
34+
# 2014-07-25T09:12:24.735
35+
datetime_obj = datetime.datetime.strptime(
36+
value, "%Y-%m-%d")
3237
dict_[key] = datetime_obj.date()
3338
except (ValueError, TypeError):
3439
continue

‎stream/signing.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ def b64_encode(s):
1010
def sign(secret, value):
1111
'''
1212
Base64 encoded sha1 signature
13-
13+
1414
:param secret: the secret
1515
:param value: the value to sign (commonly a feed id such as user:1)
16-
16+
1717
**Example**::
1818
signature = sign('secret', 'user:1')
19-
19+
2020
'''
2121
key = hashlib.sha1((secret).encode('utf-8')).digest()
2222
signed = hmac.new(key, msg=value.encode('utf8'), digestmod=hashlib.sha1)

0 commit comments

Comments
 (0)