Skip to content

Commit f911a5f

Browse files
committed
Adding support for no supplied response encoding (#91).
1 parent 8ca7d74 commit f911a5f

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

intercom/request.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,17 @@ def send_request_to_path(cls, method, url, auth, params=None):
5656
cls.raise_errors_on_failure(resp)
5757
cls.set_rate_limit_details(resp)
5858

59-
if resp.content:
59+
if resp.content and resp.content.strip():
60+
# parse non empty bodies
6061
return cls.parse_body(resp)
6162

6263
@classmethod
6364
def parse_body(cls, resp):
6465
try:
65-
# use supplied encoding to decode the response content
66-
decoded_body = resp.content.decode(resp.encoding)
67-
if not decoded_body: # return early for empty responses (issue-72)
68-
return
66+
# use supplied or inferred encoding to decode the
67+
# response content
68+
decoded_body = resp.content.decode(
69+
resp.encoding or resp.apparent_encoding)
6970
body = json.loads(decoded_body)
7071
if body.get('type') == 'error.list':
7172
cls.raise_application_errors_on_failure(body, resp.status_code)

tests/unit/test_request.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,35 @@ def it_raises_a_multiple_matching_users_error(self):
224224
mock_method.return_value = resp
225225
with assert_raises(intercom.MultipleMatchingUsersError):
226226
Intercom.get('/users')
227+
228+
@istest
229+
def it_handles_empty_responses(self):
230+
resp = mock_response('', status_code=202)
231+
with patch('requests.request') as mock_method:
232+
mock_method.return_value = resp
233+
Request.send_request_to_path('GET', 'events', ('x', 'y'), resp)
234+
235+
resp = mock_response(' ', status_code=202)
236+
with patch('requests.request') as mock_method:
237+
mock_method.return_value = resp
238+
Request.send_request_to_path('GET', 'events', ('x', 'y'), resp)
239+
240+
@istest
241+
def it_handles_no_encoding(self):
242+
resp = mock_response(
243+
' ', status_code=200, encoding=None, headers=None)
244+
resp.apparent_encoding = 'utf-8'
245+
246+
with patch('requests.request') as mock_method:
247+
mock_method.return_value = resp
248+
Request.send_request_to_path('GET', 'events', ('x', 'y'), resp)
249+
250+
@istest
251+
def it_needs_encoding_or_apparent_encoding(self):
252+
resp = mock_response(
253+
'{}', status_code=200, encoding=None, headers=None)
254+
255+
with patch('requests.request') as mock_method:
256+
mock_method.return_value = resp
257+
with assert_raises(TypeError):
258+
Request.send_request_to_path('GET', 'events', ('x', 'y'), resp)

0 commit comments

Comments
 (0)