Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for HTTP keep-alive. #146

Merged
merged 1 commit into from
Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Changelog
=========

* 3.0.1
* Added support for HTTP keep-alive. (`#146 <https://github.com/jkeyes/python-intercom/pull/146>`_)
* 3.0
* 3.0b4
* Added conversation.mark_read method. (`#136 <https://github.com/jkeyes/python-intercom/pull/136>`_)
* 3.0b3
Expand Down
2 changes: 1 addition & 1 deletion intercom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
11 changes: 7 additions & 4 deletions intercom/client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# -*- coding: utf-8 -*-

import requests


class Client(object):

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):
Expand Down Expand Up @@ -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)
15 changes: 11 additions & 4 deletions intercom/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', {})
Expand All @@ -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', {})
Expand All @@ -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', {})
Expand All @@ -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', {})
Expand All @@ -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', {})
Expand All @@ -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', {})
Expand All @@ -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', {})
Expand All @@ -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', {})
Expand All @@ -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', {})
Expand All @@ -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', {})
Expand All @@ -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', {})
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', {})
Expand All @@ -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='[email protected]')
try:
Expand Down