Skip to content

Commit 839af8a

Browse files
committed
giving travis another chance
1 parent 87c3706 commit 839af8a

2 files changed

Lines changed: 33 additions & 20 deletions

File tree

stream/client.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,28 +96,39 @@ def _make_request(self, method, relative_url, signature, params=None, data=None)
9696
params=default_params, timeout=self.timeout)
9797
logger.debug('stream api call %s, headers %s data %s',
9898
response.url, headers, data)
99-
result = serializer.loads(response.text)
100-
if result.get('exception'):
101-
self.raise_exception(result, status_code=response.status_code)
102-
return result
99+
try:
100+
parsed_result = serializer.loads(response.text)
101+
except ValueError:
102+
parsed_result = None
103+
if parsed_result.get('exception') or response.status_code >= 500:
104+
self.raise_exception(parsed_result, status_code=response.status_code)
105+
return parsed_result
103106

104107
def raise_exception(self, result, status_code):
105108
'''
106109
Map the exception code to an exception class and raise it
110+
If result.exception and result.detail are available use that
111+
Otherwise just raise a generic error
107112
'''
108113
from stream.exceptions import get_exception_dict
109-
error_message = result['detail']
110-
exception_fields = result.get('exception_fields')
111-
if exception_fields is not None:
112-
errors = []
113-
for field, errors in exception_fields.items():
114-
errors.append('Field "%s" errors: %s' %
115-
(field, repr(errors)))
116-
error_message = '\n'.join(errors)
117-
error_code = result.get('code')
118-
exception_dict = get_exception_dict()
119-
exception_class = exception_dict.get(
120-
error_code, exceptions.StreamApiException)
114+
exception_class = exceptions.StreamApiException
115+
116+
if result is not None:
117+
error_message = result['detail']
118+
exception_fields = result.get('exception_fields')
119+
if exception_fields is not None:
120+
errors = []
121+
for field, errors in exception_fields.items():
122+
errors.append('Field "%s" errors: %s' %
123+
(field, repr(errors)))
124+
error_message = '\n'.join(errors)
125+
error_code = result.get('code')
126+
exception_dict = get_exception_dict()
127+
exception_class = exception_dict.get(
128+
error_code, exceptions.StreamApiException)
129+
else:
130+
error_message = 'GetStreamAPI%s' % status_code
131+
121132
exception = exception_class(error_message, status_code=status_code)
122133
raise exception
123134

stream/tests.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,16 @@ def test_uniqueness(self):
342342
a.) The same time and activity data
343343
b.) The same time and foreign id
344344
'''
345+
from pprint import pprint
345346
utcnow = datetime.datetime.utcnow()
346347
activity_data = {
347348
'actor': 1, 'verb': 'tweet', 'object': 1, 'time': utcnow}
348349
response = self.user1.add_activity(activity_data)
349350
response = self.user1.add_activity(activity_data)
350351
activities = self.user1.get(limit=2)['results']
351352
self.assertDatetimeAlmostEqual(activities[0]['time'], utcnow)
352-
self.assertClearlyNotEqual(activities[1]['time'], utcnow)
353+
if (len(activities) > 1):
354+
self.assertClearlyNotEqual(activities[1]['time'], utcnow)
353355

354356
def test_uniqueness_topic(self):
355357
'''
@@ -358,11 +360,11 @@ def test_uniqueness_topic(self):
358360
b.) The same time and foreign id
359361
'''
360362
# follow both the topic and the user
361-
self.flat3.follow('topic', '1')
362-
self.flat3.follow('user', '1')
363+
self.flat3.follow('topic', self.topic1.user_id)
364+
self.flat3.follow('user', self.user1.user_id)
363365
# add the same activity twice
364366
now = datetime.datetime.now(tzlocal())
365-
tweet = 'My Way %s' % random.randint(10, 100000)
367+
tweet = 'My Way %s' % random_postfix
366368
activity_data = {
367369
'actor': 1, 'verb': 'tweet', 'object': 1, 'time': now, 'tweet': tweet}
368370
self.topic1.add_activity(activity_data)

0 commit comments

Comments
 (0)