|
1 | 1 | from datetime import datetime |
2 | | -from requests.adapters import HTTPAdapter |
3 | | -from stream import exceptions, serializer |
4 | | -from stream.signing import sign |
5 | | -from stream.signing import jwt_scope_token |
| 2 | +import json |
6 | 3 | import logging |
7 | 4 | import os |
| 5 | + |
| 6 | +from httpsig.requests_auth import HTTPSignatureAuth |
| 7 | +import jwt |
8 | 8 | import requests |
| 9 | +from requests.adapters import HTTPAdapter |
| 10 | +from stream import exceptions, serializer |
| 11 | +from stream.signing import sign |
9 | 12 | from stream.utils import validate_feed_slug, validate_user_id |
10 | | -from httpsig.requests_auth import HTTPSignatureAuth |
| 13 | +from requests import Request |
11 | 14 |
|
12 | 15 |
|
13 | 16 | logger = logging.getLogger(__name__) |
@@ -55,6 +58,8 @@ def __init__(self, api_key, api_secret, app_id, version='v1.0', timeout=6.0, bas |
55 | 58 | self.base_url = base_url |
56 | 59 | elif location is not None: |
57 | 60 | self.base_url = 'https://%s-api.getstream.io/api/' % location |
| 61 | + |
| 62 | + self.base_analytics_url = 'https://analytics.getstream.io/analytics/' |
58 | 63 |
|
59 | 64 | self.session = requests.Session() |
60 | 65 | # TODO: turn this back on after we verify it doesnt retry on slower requests |
@@ -131,8 +136,21 @@ def _make_signed_request(self, method_name, relative_url, params=None, data=None |
131 | 136 | response.url, headers, data) |
132 | 137 | return self._parse_response(response) |
133 | 138 |
|
134 | | - def create_feed_jwt_token(self, feed, resource, action): |
135 | | - return jwt_scope_token(self.api_secret, feed.feed_together, resource, action) |
| 139 | + def create_jwt_token(self, resource, action, feed_id=None, user_id=None): |
| 140 | + ''' |
| 141 | + Setup the payload for the given resource, action, feed or user |
| 142 | + and encode it using jwt |
| 143 | + ''' |
| 144 | + payload = { |
| 145 | + 'action': action, |
| 146 | + 'resource': resource |
| 147 | + } |
| 148 | + if feed_id is not None: |
| 149 | + payload['feed_id'] = feed_id |
| 150 | + if user_id is not None: |
| 151 | + payload['user_id'] = user_id |
| 152 | + print payload |
| 153 | + return jwt.encode(payload, self.api_secret) |
136 | 154 |
|
137 | 155 | def _make_request(self, method, relative_url, signature, params=None, data=None): |
138 | 156 | params = params or {} |
@@ -217,3 +235,24 @@ def follow_many(self, follows): |
217 | 235 |
|
218 | 236 | ''' |
219 | 237 | self._make_signed_request('post', 'follow_many/', data=follows) |
| 238 | + |
| 239 | + def create_redirect_url(self, target_url, user_id, events): |
| 240 | + ''' |
| 241 | + Creates a redirect url for tracking the given events in the context |
| 242 | + of an email using Stream's analytics platform. Learn more at |
| 243 | + getstream.io/personalization |
| 244 | + ''' |
| 245 | + # generate the JWT token |
| 246 | + auth_token = self.create_jwt_token('redirect_and_track', '*', user_id=user_id) |
| 247 | + # setup the params |
| 248 | + params = dict(auth_type='jwt', authorization=auth_token, url=target_url) |
| 249 | + params['api_key'] = self.api_key |
| 250 | + params['events'] = json.dumps(events) |
| 251 | + url = self.base_analytics_url + 'redirect/' |
| 252 | + # we get the url from the prepare request, this skips issues with |
| 253 | + # python's urlencode implementation |
| 254 | + request = Request('GET', url, params=params) |
| 255 | + prepared_request = request.prepare() |
| 256 | + # validate the target url is valid |
| 257 | + Request('GET', target_url).prepare() |
| 258 | + return prepared_request.url |
0 commit comments