Skip to content

Commit 8bcbad0

Browse files
committed
Merge pull request #75 from jkeyes/issue-72
Issue 72 – handle empty response bodies and avoid UnboundLocalErrors.
2 parents 96b4d8b + 18a312a commit 8bcbad0

File tree

6 files changed

+46
-25
lines changed

6 files changed

+46
-25
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
Changelog
33
=========
44

5+
* 2.0.beta (not yet released)
6+
* fixed `UnboundLocalError` in `Request.parse_body`.
7+
* added support for replies with an empty body.
58
* 2.0.alpha
69
* support for Intercom API v2
710
* support for Python 3

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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ 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)
49+
if body.get('type') == 'error.list':
50+
cls.raise_application_errors_on_failure(body, resp.status_code)
51+
return body
4652
except ValueError:
4753
cls.raise_errors_on_failure(resp)
48-
if body.get('type') == 'error.list':
49-
cls.raise_application_errors_on_failure(body, resp.status_code)
50-
return body
5154

5255
@classmethod
5356
def set_rate_limit_details(cls, resp):

tests/integration/issues/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-

tests/integration/issues/test_72.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import os
4+
import unittest
5+
import time
6+
from intercom import Intercom
7+
from intercom import Event
8+
from intercom import User
9+
10+
Intercom.app_id = os.environ.get('INTERCOM_APP_ID')
11+
Intercom.app_api_key = os.environ.get('INTERCOM_APP_API_KEY')
12+
13+
14+
class Issue72Test(unittest.TestCase):
15+
16+
def test(self):
17+
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)

tests/unit/test_event.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
# -*- coding: utf-8 -*-
22

33
import httpretty
4-
import json
5-
import re
64
import time
75
import unittest
86

97
from datetime import datetime
108
from intercom import User
9+
from intercom import Intercom
1110
from intercom import Event
12-
from nose.tools import eq_
13-
from nose.tools import ok_
11+
from mock import patch
1412
from nose.tools import istest
1513

16-
post = httpretty.POST
17-
r = re.compile
18-
1914

2015
class EventTest(unittest.TestCase):
2116

@@ -29,7 +24,6 @@ def setUp(self): # noqa
2924
self.created_time = now - 300
3025

3126
@istest
32-
@httpretty.activate
3327
def it_creates_an_event_with_metadata(self):
3428
data = {
3529
'event_name': 'Eventful 1',
@@ -41,13 +35,10 @@ def it_creates_an_event_with_metadata(self):
4135
'found_date': 12909364407
4236
}
4337
}
44-
httpretty.register_uri(
45-
post, r(r'/events/$'), body=json.dumps(data), status=202)
46-
event = Event.create(**data)
4738

48-
eq_('Eventful 1', event.event_name)
49-
ok_(hasattr(event, 'metadata'))
50-
eq_('pi@example.com', event.metadata['invitee_email'])
39+
with patch.object(Intercom, 'post', return_value=data) as mock_method:
40+
Event.create(**data)
41+
mock_method.assert_called_once_with('/events/', **data)
5142

5243
@istest
5344
@httpretty.activate
@@ -56,9 +47,6 @@ def it_creates_an_event_without_metadata(self):
5647
'event_name': 'sale of item',
5748
'email': 'joe@example.com',
5849
}
59-
httpretty.register_uri(
60-
post, r(r'/events/$'), body=json.dumps(data), status=202)
61-
event = Event.create(**data)
62-
63-
eq_('sale of item', event.event_name)
64-
ok_(not hasattr(event, 'metadata'))
50+
with patch.object(Intercom, 'post', return_value=data) as mock_method:
51+
Event.create(**data)
52+
mock_method.assert_called_once_with('/events/', **data)

0 commit comments

Comments
 (0)