Skip to content

Commit 6181ff6

Browse files
committed
added easy support for generating feed tokens
2 parents 78f0e27 + 2e2370f commit 6181ff6

7 files changed

Lines changed: 101 additions & 104 deletions

File tree

‎README.md‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
stream-python
2+
=============
3+
4+
[![image](https://circleci.com/gh/tschellenbach/stream-python.png?circle-token=ca08d1aa53fd4f9c3255a89bde5cb08c59b9586a)](https://circleci.com/gh/tschellenbach/stream-python/tree/master)
5+
6+
[![Coverage Status](https://coveralls.io/repos/tschellenbach/stream-python/badge.png?branch=master)](https://coveralls.io/r/tschellenbach/stream-python?branch=master)
7+
8+
stream-python is a Python client for [Stream](https://getstream.io/).
9+
10+
### Installation
11+
12+
#### Install from Pypi
13+
14+
15+
```bash
16+
pip install stream-python
17+
```
18+
19+
### Usage
20+
21+
```python
22+
# Instantiate a new client
23+
import stream
24+
client = stream.connect('YOUR_API_KEY', 'API_KEY_SECRET')
25+
# Find your API keys here https://getstream.io/dashboard/
26+
27+
# Instantiate a feed object
28+
user_feed_1 = client.feed('user:1')
29+
30+
# Get activities from 5 to 10 (slow pagination)
31+
result = user_feed_1.get(limit=5, offset=5)
32+
# (Recommended & faster) Filter on an id less than the given UUID
33+
result = user_feed_1.get(limit=5, id_lt="e561de8f-00f1-11e4-b400-0cc47a024be0")
34+
35+
# Create a new activity
36+
activity_data = {'actor': 1, 'verb': 'tweet', 'object': 1}
37+
activity_response = user_feed_1.add_activity(activity_data)
38+
39+
# Remove an activity by its id
40+
user_feed_1.remove("e561de8f-00f1-11e4-b400-0cc47a024be0")
41+
42+
# Follow another feed
43+
user_feed_1.follow('flat:42')
44+
45+
# Stop following another feed
46+
user_feed_1.unfollow('flat:42')
47+
48+
# Generating tokens for client side usage
49+
token = user_feed_1.token
50+
# Javascript client side feed initialization
51+
# user1 = client.feed('user:1', '{{ token }}');
52+
```
53+
54+
Docs are available on [GetStream.io](http://getstream.io/docs/).
55+
56+
API docs are on [Read the
57+
docs](http://stream-python.readthedocs.org/en/latest/).
58+
59+
60+
61+
### Contributing
62+
63+
First, make sure you can run the test suite. Tests are run via py.test
64+
65+
```bash
66+
py.test stream/tests.py
67+
# with coverage
68+
py.test stream/tests.py --cov stream --cov-report html
69+
# against a local API backend
70+
LOCAL=true py.test stream/tests.py
71+
```

‎README.rst‎

Lines changed: 0 additions & 78 deletions
This file was deleted.

‎setup.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
from stream import __version__, __maintainer__, __email__
77
import sys
88

9-
long_description = open('README.rst').read()
9+
long_description = open('README.md').read()
1010

1111
unit = 'unittest2py3k' if sys.version_info > (3, 0, 0) else 'unittest2'
1212
tests_require = [
1313
'pep8',
1414
unit,
1515
#'pytest',
1616
'python-coveralls',
17+
'unittest2',
1718
'pytest-cov',
1819
]
1920

‎stream/__init__.py‎

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

33
__author__ = 'Thierry Schellenbach'
44
__copyright__ = 'Copyright 2012, Thierry Schellenbach'
@@ -10,7 +10,7 @@
1010
__status__ = 'Production'
1111

1212

13-
def connect(api_key, api_secret=None, site_id=None):
13+
def connect(api_key=None, api_secret=None, site_id=None):
1414
'''
1515
Returns a Client object
1616
@@ -19,11 +19,11 @@ def connect(api_key, api_secret=None, site_id=None):
1919
:param site_id: the site id (used for listening to feed changes)
2020
'''
2121
from stream.client import StreamClient
22-
22+
stream_url = os.environ.get('STREAM_URL')
2323
# support for the heroku STREAM_URL syntax
24-
if api_key.startswith('https'):
24+
if stream_url and not api_key:
2525
pattern = re.compile('https\:\/\/(\w+)\:(\w+).*site=(\d+)', re.IGNORECASE)
26-
result = pattern.match(api_key)
26+
result = pattern.match(stream_url)
2727
if result and len(result.groups())==3:
2828
api_key, api_secret, site_id = result.groups()
2929
else:

‎stream/client.py‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,14 @@ def get_default_params(self):
6666
params = dict(api_key=self.api_key)
6767
return params
6868

69-
def get_headers(self, feed):
70-
'''
71-
Returns the headers with the signed authorization key
72-
'''
73-
feed_together = feed.replace(':', '')
74-
headers = {'Authorization': '%s %s' % (feed_together, feed.token)}
75-
return headers
76-
77-
def _make_request(self, method, relative_url, feed, params=None, data=None):
69+
def _make_request(self, method, relative_url, authorization, params=None, data=None):
7870
params = params or {}
7971
data = data or {}
8072

8173
default_params = self.get_default_params()
8274
default_params.update(params)
8375

84-
headers = self.get_headers(feed)
76+
headers = headers = {'Authorization': authorization}
8577
url = self.base_url + relative_url
8678

8779
response = method(url, data=data, headers=headers,

‎stream/feed.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def __init__(self, client, feed_id, token):
1717
self.feed_url = 'feed/%s/' % feed_id.replace(':', '/')
1818
self.feed_together = feed_id.replace(':', '')
1919
self.token = token
20+
self.authorization = self.feed_together + ' ' + self.token
2021

2122
def add_activity(self, activity_data):
2223
'''
@@ -31,7 +32,7 @@ def add_activity(self, activity_data):
3132
activity_id = feed.add_activity(activity_data)
3233
'''
3334
result = self.client.post(
34-
self.feed_url, data=activity_data, feed=self.feed_id)
35+
self.feed_url, data=activity_data, authorization=self.authorization)
3536
return result
3637

3738
def remove_activity(self, activity_id):
@@ -42,7 +43,7 @@ def remove_activity(self, activity_id):
4243
(note this will also remove the activity from feeds which follow this feed)
4344
'''
4445
url = self.feed_url + '%s/' % activity_id
45-
result = self.client.delete(url, feed=self.feed_id)
46+
result = self.client.delete(url, authorization=self.authorization)
4647
return result
4748

4849
def follow(self, target_feed):
@@ -54,7 +55,7 @@ def follow(self, target_feed):
5455
validate_feed(target_feed)
5556
url = self.feed_url + 'follows/'
5657
response = self.client.post(
57-
url, data=dict(target=target_feed), feed=self.feed_id)
58+
url, data=dict(target=target_feed), authorization=self.authorization)
5859
return response
5960

6061
def unfollow(self, target_feed):
@@ -63,7 +64,7 @@ def unfollow(self, target_feed):
6364
'''
6465
validate_feed(target_feed)
6566
url = self.feed_url + 'follows/%s/' % target_feed
66-
response = self.client.delete(url, feed=self.feed_id)
67+
response = self.client.delete(url, authorization=self.authorization)
6768
return response
6869

6970
def get(self, **params):
@@ -79,5 +80,5 @@ def get(self, **params):
7980
feed.get(limit=10, offset=10)
8081
'''
8182
response = self.client.get(
82-
self.feed_url, params=params, feed=self.feed_id)
83+
self.feed_url, params=params, authorization=self.authorization)
8384
return response

‎stream/tests.py‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import random
66
from unittest.case import TestCase
77

8+
import os
89

910
def connect_debug():
1011
return stream.connect(
11-
u'5crf3bhfzesn',
12-
u'tfq2sdqpj9g446sbv653x3aqmgn33hsn8uzdc9jpskaw8mj6vsnhzswuwptuj9su'
12+
u'ahj2ndz7gsan',
13+
u'gthc2t9gh7pzq52f6cky8w4r4up9dr6rju9w3fjgmkv6cdvvav2ufe5fv7e2r9qy'
1314
)
1415

1516
client = connect_debug()
@@ -31,11 +32,20 @@ def setUp(self):
3132

3233
def test_heroku(self):
3334
url = 'https://thierry:pass@getstream.io/?site=1'
34-
client = stream.connect(url)
35+
os.environ['STREAM_URL'] = url
36+
client = stream.connect()
3537
self.assertEqual(client.api_key, 'thierry')
3638
self.assertEqual(client.api_secret, 'pass')
3739
self.assertEqual(client.site_id, '1')
3840

41+
def test_heroku_overwrite(self):
42+
url = 'https://thierry:pass@getstream.io/?site=1'
43+
os.environ['STREAM_URL'] = url
44+
client = stream.connect('a', 'b', 'c')
45+
self.assertEqual(client.api_key, 'a')
46+
self.assertEqual(client.api_secret, 'b')
47+
self.assertEqual(client.site_id, 'c')
48+
3949
def test_token_retrieval(self):
4050
token = self.user1.token
4151

@@ -58,7 +68,7 @@ def test_follow(self):
5868
activity_data = {'actor': actor_id, 'verb': 'tweet', 'object': 1}
5969
activity_id = self.user1.add_activity(activity_data)['id']
6070
self.aggregated2.follow('user:1')
61-
time.sleep(5)
71+
time.sleep(10)
6272
activities = self.aggregated2.get(limit=3)['results']
6373
activity = self._get_first_aggregated_activity(activities)
6474
activity_id_found = activity['id'] if activity is not None else None
@@ -68,7 +78,7 @@ def test_flat_follow(self):
6878
activity_data = {'actor': 1, 'verb': 'tweet', 'object': 1}
6979
activity_id = self.user1.add_activity(activity_data)['id']
7080
self.flat3.follow('user:1')
71-
time.sleep(5)
81+
time.sleep(10)
7282
activities = self.flat3.get(limit=3)['results']
7383
activity = self._get_first_activity(activities)
7484
activity_id_found = activity['id'] if activity is not None else None

0 commit comments

Comments
 (0)