Skip to content

Commit 88572d2

Browse files
authored
Merge pull request GetStream#67 from GetStream/feature/clean-urls
Feature/clean urls
2 parents 0a38299 + 036e69f commit 88572d2

4 files changed

Lines changed: 162 additions & 50 deletions

File tree

‎stream/client.py‎

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
from stream.utils import validate_feed_slug, validate_user_id
1313
from requests import Request
1414

15+
try:
16+
from urllib.parse import urlparse
17+
except ImportError:
18+
from urlparse import urlparse
1519

1620
logger = logging.getLogger(__name__)
1721

1822

1923
class StreamClient(object):
20-
base_url = 'https://api.stream-io-api.com/api/'
2124

2225
def __init__(self, api_key, api_secret, app_id, version='v1.0', timeout=6.0, base_url=None, location=None):
2326
'''
@@ -51,19 +54,28 @@ def __init__(self, api_key, api_secret, app_id, version='v1.0', timeout=6.0, bas
5154
self.timeout = timeout
5255
self.location = location
5356

57+
self.base_domain_name = 'stream-io-api.com'
58+
self.api_location = location
59+
self.custom_api_port = None
60+
self.protocol = 'https'
61+
5462
if os.environ.get('LOCAL'):
55-
self.base_url = 'http://localhost:8000/api/'
63+
self.base_domain_name = 'localhost'
64+
self.protocol = 'http'
65+
self.custom_api_port = 8000
5666
self.timeout = 20
5767
elif base_url is not None:
58-
self.base_url = base_url
68+
parsed_url = urlparse(base_url)
69+
self.base_domain_name = parsed_url.hostname
70+
self.protocol = parsed_url.scheme
71+
self.custom_api_port = parsed_url.port
72+
self.api_location = ""
5973
elif location is not None:
60-
self.base_url = 'https://%s-api.stream-io-api.com/api/' % location
74+
self.location = location
6175

6276
self.base_analytics_url = 'https://analytics.stream-io-api.com/analytics/'
6377

6478
self.session = requests.Session()
65-
# TODO: turn this back on after we verify it doesnt retry on slower requests
66-
self.session.mount(self.base_url, HTTPAdapter(max_retries=0))
6779
self.auth = HTTPSignatureAuth(api_key, secret=api_secret)
6880

6981
# setup personalization
@@ -107,23 +119,23 @@ def get_default_header(self):
107119
}
108120
return base_headers
109121

110-
def get_full_url(self, relative_url):
111-
url = self.base_url + self.version + '/' + relative_url
112-
return url
113-
114-
def get_full_personal_url(self, relative_url):
115-
base_url = self.base_url.split('.') # company.getstream.io
116-
if len(base_url) > 1:
117-
DNS_change = base_url[0] + '-personalization'
118-
base_url[0] = DNS_change
119-
base_url = '.'.join(base_url)
122+
def get_full_url(self, service_name, relative_url):
123+
if self.api_location:
124+
hostname = '%s-%s.%s' % (self.api_location, service_name, self.base_domain_name)
125+
elif service_name:
126+
hostname = '%s.%s' % (service_name, self.base_domain_name)
120127
else:
121-
base_url = self.base_url # if running on localhost
122-
url = base_url + 'personalization/' + relative_url + '/'
123-
return url
128+
hostname = self.base_domain_name
129+
130+
if self.base_domain_name == 'localhost':
131+
hostname = 'localhost'
132+
133+
base_url = "%s://%s" % (self.protocol, hostname)
124134

125-
def get_full_meta_url(self):
126-
url = self.base_url + 'personalization/' + self.version + '/meta/'
135+
if self.custom_api_port:
136+
base_url = "%s:%s" % (base_url, self.custom_api_port)
137+
138+
url = base_url + '/' + service_name + '/' + self.version + '/' + relative_url
127139
return url
128140

129141
def get_user_agent(self):
@@ -150,7 +162,7 @@ def _make_signed_request(self, method_name, relative_url, params=None, data=None
150162
headers['Date'] = date_header
151163
default_params = self.get_default_params()
152164
default_params.update(params)
153-
url = self.get_full_url(relative_url)
165+
url = self.get_full_url('api', relative_url)
154166
serialized = serializer.dumps(data)
155167
method = getattr(self.session, method_name)
156168
if method_name in ['post', 'put']:
@@ -176,7 +188,7 @@ def create_jwt_token(self, resource, action, feed_id=None, user_id=None):
176188
payload['user_id'] = user_id
177189
return jwt.encode(payload, self.api_secret).decode("utf-8")
178190

179-
def _make_request(self, method, relative_url, signature, personal=None, params=None, data=None):
191+
def _make_request(self, method, relative_url, signature, service_name='api', params=None, data=None):
180192
params = params or {}
181193
data = data or {}
182194
serialized = None
@@ -185,15 +197,12 @@ def _make_request(self, method, relative_url, signature, personal=None, params=N
185197
headers = self.get_default_header()
186198
headers['Authorization'] = signature
187199
headers['stream-auth-type'] = 'jwt'
188-
if personal is not None:
189-
if personal == 'personal':
190-
url = self.get_full_personal_url(relative_url)
191-
elif personal == 'meta':
192-
url = self.get_full_meta_url()
193-
else:
194-
raise Exception("keyword 'personal' must be None, personal, or meta")
195-
else:
196-
url = self.get_full_url(relative_url)
200+
201+
if not relative_url.endswith('/'):
202+
relative_url += '/'
203+
204+
url = self.get_full_url(service_name, relative_url)
205+
197206
if method.__name__ in ['post', 'put', 'delete']:
198207
serialized = serializer.dumps(data)
199208
response = method(url, data=serialized, headers=headers,

‎stream/collections.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Collections(object):
2+
23
def __init__(self, client, token):
34
"""
45
Used to manipulate data at the 'meta' endpoint
@@ -26,7 +27,7 @@ def upsert(self, collection_name, data):
2627

2728
data_json = {collection_name: data}
2829

29-
response = self.client.post('meta', personal='meta',
30+
response = self.client.post('meta/', service_name='api',
3031
signature=self.token, data={'data': data_json})
3132
return response
3233

@@ -52,7 +53,7 @@ def select(self, collection_name, ids):
5253
foreign_ids.append('%s:%s' % (collection_name, ids[i]))
5354
foreign_ids = ','.join(foreign_ids)
5455

55-
response = self.client.get('meta', personal='meta', params={'foreign_ids': foreign_ids},
56+
response = self.client.get('meta/', service_name='api', params={'foreign_ids': foreign_ids},
5657
signature=self.token)
5758

5859
return response
@@ -75,7 +76,7 @@ def delete(self, collection_name, ids):
7576

7677
data = {'collection_name': collection_name, 'ids': ids}
7778

78-
response = self.client.delete('meta', personal='meta', data=data,
79+
response = self.client.delete('meta/', service_name='api', data=data,
7980
signature=self.token)
8081

8182
return response

‎stream/personalization.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def get(self, resource, **params):
2020
personalization.get('follow_recommendations', user_id=123, limit=10, offset=10)
2121
"""
2222

23-
response = self.client.get(resource, personal='personal', params=params,
23+
response = self.client.get(resource, service_name='personalization', params=params,
2424
signature=self.token)
2525
return response
2626

@@ -39,7 +39,7 @@ def post(self, resource, **params):
3939

4040
data = params['data'] or None
4141

42-
response = self.client.post(resource, personal='personal', params=params,
42+
response = self.client.post(resource, service_name='personalization', params=params,
4343
signature=self.token, data=data)
4444
return response
4545

@@ -51,7 +51,7 @@ def delete(self, resource, **params):
5151
:return: data that was deleted if if successful or not.
5252
"""
5353

54-
response = self.client.delete(resource, personal='personal', params=params,
54+
response = self.client.delete(resource, service_name='personalization', params=params,
5555
signature=self.token)
5656

5757
return response

‎stream/tests.py‎

Lines changed: 114 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ def connect_debug():
4242
return stream.connect(
4343
key,
4444
secret,
45-
location='us-east',
45+
location='qa',
4646
timeout=30,
47-
base_url='https://qa.stream-io-api.com/api/',
48-
# base_url='http://localhost-api.getstream.io:8000/api/',
4947
)
5048

5149
client = connect_debug()
@@ -120,6 +118,105 @@ def _test_sleep(self, production_wait, local_wait):
120118
sleep_time = local_wait
121119
time.sleep(sleep_time)
122120

121+
def test_collections_url(self):
122+
feed_url = client.get_full_url(relative_url='meta/', service_name='api')
123+
124+
if self.local_tests:
125+
self.assertEqual(
126+
feed_url, 'http://localhost:8000/api/v1.0/meta/')
127+
else:
128+
self.assertEqual(
129+
feed_url, 'https://qa-api.stream-io-api.com/api/v1.0/meta/')
130+
131+
def test_personalization_url(self):
132+
feed_url = client.get_full_url(relative_url='recommended', service_name='personalization')
133+
134+
if self.local_tests:
135+
self.assertEqual(
136+
feed_url, 'http://localhost:8000/personalization/v1.0/recommended')
137+
else:
138+
self.assertEqual(
139+
feed_url, 'https://qa-personalization.stream-io-api.com/personalization/v1.0/recommended')
140+
141+
def test_api_url(self):
142+
feed_url = client.get_full_url(service_name='api', relative_url='feed/')
143+
144+
if self.local_tests:
145+
self.assertEqual(
146+
feed_url, 'http://localhost:8000/api/v1.0/feed/')
147+
else:
148+
self.assertEqual(
149+
feed_url, 'https://qa-api.stream-io-api.com/api/v1.0/feed/')
150+
151+
def test_collections_url_default(self):
152+
client = stream.connect(
153+
'key',
154+
'secret',
155+
)
156+
feed_url = client.get_full_url(relative_url='meta/', service_name='api')
157+
158+
if not self.local_tests:
159+
self.assertEqual(
160+
feed_url, 'https://api.stream-io-api.com/api/v1.0/meta/')
161+
162+
def test_personalization_url_default(self):
163+
client = stream.connect(
164+
'key',
165+
'secret',
166+
)
167+
feed_url = client.get_full_url(relative_url='recommended', service_name='personalization')
168+
169+
if not self.local_tests:
170+
self.assertEqual(
171+
feed_url, 'https://personalization.stream-io-api.com/personalization/v1.0/recommended')
172+
173+
def test_api_url_default(self):
174+
client = stream.connect(
175+
'key',
176+
'secret',
177+
)
178+
feed_url = client.get_full_url(service_name='api', relative_url='feed/')
179+
180+
if not self.local_tests:
181+
self.assertEqual(
182+
feed_url, 'https://api.stream-io-api.com/api/v1.0/feed/')
183+
184+
def test_collections_url_location(self):
185+
client = stream.connect(
186+
'key',
187+
'secret',
188+
location='tokyo',
189+
)
190+
feed_url = client.get_full_url(relative_url='meta/', service_name='api')
191+
192+
if not self.local_tests:
193+
self.assertEqual(
194+
feed_url, 'https://tokyo-api.stream-io-api.com/api/v1.0/meta/')
195+
196+
def test_personalization_url_location(self):
197+
client = stream.connect(
198+
'key',
199+
'secret',
200+
location='tokyo',
201+
)
202+
feed_url = client.get_full_url(relative_url='recommended', service_name='personalization')
203+
204+
if not self.local_tests:
205+
self.assertEqual(
206+
feed_url, 'https://tokyo-personalization.stream-io-api.com/personalization/v1.0/recommended')
207+
208+
def test_api_url_location(self):
209+
client = stream.connect(
210+
'key',
211+
'secret',
212+
location='tokyo',
213+
)
214+
feed_url = client.get_full_url(service_name='api', relative_url='feed/')
215+
216+
if not self.local_tests:
217+
self.assertEqual(
218+
feed_url, 'https://tokyo-api.stream-io-api.com/api/v1.0/feed/')
219+
123220
def test_update_activities_create(self):
124221
activities = [{
125222
'actor': 'user:1',
@@ -181,13 +278,14 @@ def test_heroku_no_location(self):
181278
self.assertEqual(
182279
client.api_secret, 'twc5ywfste5bm2ngqkzs7ukxk3pn96yweghjrxcmcrarnt3j4dqj3tucbhym5wfd')
183280
self.assertEqual(client.app_id, '669')
281+
feed_url = client.get_full_url('api', 'feed/')
184282

185283
if self.local_tests:
186284
self.assertEqual(
187-
client.base_url, 'http://localhost:8000/api/')
285+
feed_url, 'http://localhost:8000/api/v1.0/feed/')
188286
else:
189287
self.assertEqual(
190-
client.base_url, 'https://api.stream-io-api.com/api/')
288+
feed_url, 'https://api.stream-io-api.com/api/v1.0/feed/')
191289

192290
def test_heroku_location_compat(self):
193291
url = 'https://ahj2ndz7gsan:gthc2t9gh7pzq52f6cky8w4r4up9dr6rju9w3fjgmkv6cdvvav2ufe5fv7e2r9qy@us-east.getstream.io/?app_id=1'
@@ -197,12 +295,14 @@ def test_heroku_location_compat(self):
197295
self.assertEqual(
198296
client.api_secret, 'gthc2t9gh7pzq52f6cky8w4r4up9dr6rju9w3fjgmkv6cdvvav2ufe5fv7e2r9qy')
199297

298+
feed_url = client.get_full_url('api', 'feed/')
200299
if self.local_tests:
201300
self.assertEqual(
202-
client.base_url, 'http://localhost:8000/api/')
301+
feed_url, 'http://localhost:8000/api/v1.0/feed/')
203302
else:
204303
self.assertEqual(
205-
client.base_url, 'https://us-east-api.stream-io-api.com/api/')
304+
feed_url, 'https://us-east-api.stream-io-api.com/api/v1.0/feed/')
305+
206306
self.assertEqual(client.app_id, '1')
207307

208308
def test_heroku_location(self):
@@ -213,12 +313,13 @@ def test_heroku_location(self):
213313
self.assertEqual(
214314
client.api_secret, 'gthc2t9gh7pzq52f6cky8w4r4up9dr6rju9w3fjgmkv6cdvvav2ufe5fv7e2r9qy')
215315

316+
feed_url = client.get_full_url('api', 'feed/')
216317
if self.local_tests:
217318
self.assertEqual(
218-
client.base_url, 'http://localhost:8000/api/')
319+
feed_url, 'http://localhost:8000/api/v1.0/feed/')
219320
else:
220321
self.assertEqual(
221-
client.base_url, 'https://us-east-api.stream-io-api.com/api/')
322+
feed_url, 'https://us-east-api.stream-io-api.com/api/v1.0/feed/')
222323
self.assertEqual(client.app_id, '1')
223324

224325
def test_heroku_overwrite(self):
@@ -232,12 +333,13 @@ def test_heroku_overwrite(self):
232333
def test_location_support(self):
233334
client = stream.connect('a', 'b', 'c', location='us-east')
234335

235-
full_location = 'https://us-east-api.stream-io-api.com/api/'
336+
full_location = 'https://us-east-api.stream-io-api.com/api/v1.0/feed/'
236337
if self.local_tests:
237-
full_location = 'http://localhost:8000/api/'
338+
full_location = 'http://localhost:8000/api/v1.0/feed/'
238339

239340
self.assertEqual(client.location, 'us-east')
240-
self.assertEqual(client.base_url, full_location)
341+
feed_url = client.get_full_url('api', 'feed/')
342+
self.assertEqual(feed_url, full_location)
241343

242344
# test a wrong location, can only work on non-local test running
243345
if not self.local_tests:

0 commit comments

Comments
 (0)