diff --git a/CHANGES.rst b/CHANGES.rst index 879ed9e2..054e2957 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,9 @@ Changelog ========= +* 3.0.1 + * Added support for HTTP keep-alive. (`#146 `_) +* 3.0 * 3.0b4 * Added conversation.mark_read method. (`#136 `_) * 3.0b3 diff --git a/intercom/__init__.py b/intercom/__init__.py index 776470fb..accbc662 100644 --- a/intercom/__init__.py +++ b/intercom/__init__.py @@ -6,7 +6,7 @@ MultipleMatchingUsersError, RateLimitExceeded, ResourceNotFound, ServerError, ServiceUnavailableError, UnexpectedError, TokenUnauthorizedError) -__version__ = '3.0' +__version__ = '3.0.1' RELATED_DOCS_TEXT = "See https://github.com/jkeyes/python-intercom \ diff --git a/intercom/client.py b/intercom/client.py index f7e92899..ce37617b 100644 --- a/intercom/client.py +++ b/intercom/client.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +import requests + class Client(object): @@ -7,6 +9,7 @@ def __init__(self, personal_access_token='my_personal_access_token'): self.personal_access_token = personal_access_token self.base_url = 'https://api.intercom.io' self.rate_limit_details = {} + self.http_session = requests.Session() @property def _auth(self): @@ -84,20 +87,20 @@ def _execute_request(self, request, params): def get(self, path, params): from intercom import request - req = request.Request('GET', path) + req = request.Request('GET', path, self.http_session) return self._execute_request(req, params) def post(self, path, params): from intercom import request - req = request.Request('POST', path) + req = request.Request('POST', path, self.http_session) return self._execute_request(req, params) def put(self, path, params): from intercom import request - req = request.Request('PUT', path) + req = request.Request('PUT', path, self.http_session) return self._execute_request(req, params) def delete(self, path, params): from intercom import request - req = request.Request('DELETE', path) + req = request.Request('DELETE', path, self.http_session) return self._execute_request(req, params) diff --git a/intercom/request.py b/intercom/request.py index 97161490..ca664c6e 100644 --- a/intercom/request.py +++ b/intercom/request.py @@ -16,9 +16,11 @@ class Request(object): timeout = 10 - def __init__(self, http_method, path): + def __init__(self, http_method, path, http_session=None): self.http_method = http_method self.path = path + self.http_session = http_session + def execute(self, base_url, auth, params): return self.send_request_to_path(base_url, auth, params) @@ -53,9 +55,14 @@ def send_request_to_path(self, base_url, auth, params=None): else: logger.debug(" params: %s", req_params['data']) - resp = requests.request( - self.http_method, url, timeout=self.timeout, - auth=auth, verify=certifi.where(), **req_params) + if self.http_session is None: + resp = requests.request( + self.http_method, url, timeout=self.timeout, + auth=auth, verify=certifi.where(), **req_params) + else: + resp = self.http_session.request( + self.http_method, url, timeout=self.timeout, + auth=auth, verify=certifi.where(), **req_params) # response logging if logger.isEnabledFor(logging.DEBUG): diff --git a/tests/unit/test_request.py b/tests/unit/test_request.py index 09beaf07..bcd12405 100644 --- a/tests/unit/test_request.py +++ b/tests/unit/test_request.py @@ -87,7 +87,7 @@ def it_raises_an_unexpected_typed_error(self): } content = json.dumps(payload).encode('utf-8') resp = mock_response(content) - with patch('requests.request') as mock_method: + with patch('requests.sessions.Session.request') as mock_method: mock_method.return_value = resp try: self.client.get('/users', {}) @@ -109,7 +109,7 @@ def it_raises_an_unexpected_untyped_error(self): } content = json.dumps(payload).encode('utf-8') resp = mock_response(content) - with patch('requests.request') as mock_method: + with patch('requests.sessions.Session.request') as mock_method: mock_method.return_value = resp try: self.client.get('/users', {}) @@ -135,7 +135,7 @@ def it_raises_a_bad_request_error(self): content = json.dumps(payload).encode('utf-8') resp = mock_response(content) - with patch('requests.request') as mock_method: + with patch('requests.sessions.Session.request') as mock_method: mock_method.return_value = resp with assert_raises(intercom.BadRequestError): self.client.get('/users', {}) @@ -156,7 +156,7 @@ def it_raises_an_authentication_error(self): content = json.dumps(payload).encode('utf-8') resp = mock_response(content) - with patch('requests.request') as mock_method: + with patch('requests.sessions.Session.request') as mock_method: mock_method.return_value = resp with assert_raises(intercom.AuthenticationError): self.client.get('/users', {}) @@ -174,7 +174,7 @@ def it_raises_resource_not_found_by_type(self): } content = json.dumps(payload).encode('utf-8') resp = mock_response(content) - with patch('requests.request') as mock_method: + with patch('requests.sessions.Session.request') as mock_method: mock_method.return_value = resp with assert_raises(intercom.ResourceNotFound): self.client.get('/users', {}) @@ -192,7 +192,7 @@ def it_raises_rate_limit_exceeded(self): } content = json.dumps(payload).encode('utf-8') resp = mock_response(content) - with patch('requests.request') as mock_method: + with patch('requests.sessions.Session.request') as mock_method: mock_method.return_value = resp with assert_raises(intercom.RateLimitExceeded): self.client.get('/users', {}) @@ -210,7 +210,7 @@ def it_raises_a_service_unavailable_error(self): } content = json.dumps(payload).encode('utf-8') resp = mock_response(content) - with patch('requests.request') as mock_method: + with patch('requests.sessions.Session.request') as mock_method: mock_method.return_value = resp with assert_raises(intercom.ServiceUnavailableError): self.client.get('/users', {}) @@ -228,7 +228,7 @@ def it_raises_a_multiple_matching_users_error(self): } content = json.dumps(payload).encode('utf-8') resp = mock_response(content) - with patch('requests.request') as mock_method: + with patch('requests.sessions.Session.request') as mock_method: mock_method.return_value = resp with assert_raises(intercom.MultipleMatchingUsersError): self.client.get('/users', {}) @@ -246,7 +246,7 @@ def it_raises_token_unauthorized(self): } content = json.dumps(payload).encode('utf-8') resp = mock_response(content) - with patch('requests.request') as mock_method: + with patch('requests.sessions.Session.request') as mock_method: mock_method.return_value = resp with assert_raises(intercom.TokenUnauthorizedError): self.client.get('/users', {}) @@ -265,7 +265,7 @@ def it_handles_no_error_type(self): } content = json.dumps(payload).encode('utf-8') resp = mock_response(content) - with patch('requests.request') as mock_method: + with patch('requests.sessions.Session.request') as mock_method: mock_method.return_value = resp with assert_raises(intercom.MultipleMatchingUsersError): self.client.get('/users', {}) @@ -282,7 +282,7 @@ def it_handles_no_error_type(self): } content = json.dumps(payload).encode('utf-8') resp = mock_response(content) - with patch('requests.request') as mock_method: + with patch('requests.sessions.Session.request') as mock_method: mock_method.return_value = resp with assert_raises(intercom.BadRequestError): self.client.get('/users', {}) diff --git a/tests/unit/test_user.py b/tests/unit/test_user.py index 8d0ca16c..52860868 100644 --- a/tests/unit/test_user.py +++ b/tests/unit/test_user.py @@ -368,7 +368,7 @@ def it_raises_a_multiple_matching_users_error_when_receiving_a_conflict(self): content = json.dumps(payload).encode('utf-8') # create mock response resp = mock_response(content) - with patch('requests.request') as mock_method: + with patch('requests.sessions.Session.request') as mock_method: mock_method.return_value = resp with assert_raises(MultipleMatchingUsersError): self.client.get('/users', {}) @@ -381,7 +381,7 @@ def it_handles_accented_characters(self): content = json.dumps(payload).encode('utf-8') # create mock response resp = mock_response(content) - with patch('requests.request') as mock_method: + with patch('requests.sessions.Session.request') as mock_method: mock_method.return_value = resp user = self.client.users.find(email='bob@example.com') try: