Skip to content

Commit d98f10a

Browse files
committed
Handling 202 empty body responses gracefully.
1 parent e5bfa34 commit d98f10a

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

intercom/api_operations/save.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def create(cls, **params):
1010
from intercom import Intercom
1111
collection = utils.resource_class_to_collection_name(cls)
1212
response = Intercom.post("/%s/" % (collection), **params)
13-
return cls(**response)
13+
if response: # may be empty if we received a 202
14+
return cls(**response)
1415

1516
def from_dict(self, pdict):
1617
for key, value in list(pdict.items()):

intercom/request.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ def send_request_to_path(cls, method, url, auth, params=None):
4242
@classmethod
4343
def parse_body(cls, resp):
4444
try:
45-
body = json.loads(resp.content.decode())
45+
decoded_body = resp.content.decode()
46+
if not decoded_body: # return early for empty responses (issue-72)
47+
return
48+
body = json.loads(decoded_body)
4649
if body.get('type') == 'error.list':
4750
cls.raise_application_errors_on_failure(body, resp.status_code)
4851
return body

tests/integration/issues/test_72.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import os
44
import unittest
5+
import time
56
from intercom import Intercom
7+
from intercom import Event
68
from intercom import User
79

810
Intercom.app_id = os.environ.get('INTERCOM_APP_ID')
@@ -13,3 +15,11 @@ class Issue72Test(unittest.TestCase):
1315

1416
def test(self):
1517
User.create(email='me@example.com')
18+
# no exception here as empty response expected
19+
data = {
20+
'event_name': 'Eventful 1',
21+
'created_at': int(time.time()),
22+
'email': 'me@example.com'
23+
}
24+
print data
25+
Event.create(**data)

0 commit comments

Comments
 (0)