Skip to content

Commit d9705e1

Browse files
authored
Merge pull request #146 from jkeyes/http-keep-alive
Adding support for HTTP keep-alive.
2 parents 760f735 + b39806b commit d9705e1

File tree

6 files changed

+35
-22
lines changed

6 files changed

+35
-22
lines changed

CHANGES.rst

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

4+
* 3.0.1
5+
* Added support for HTTP keep-alive. (`#146 <https://github.com/jkeyes/python-intercom/pull/146>`_)
6+
* 3.0
47
* 3.0b4
58
* Added conversation.mark_read method. (`#136 <https://github.com/jkeyes/python-intercom/pull/136>`_)
69
* 3.0b3

intercom/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
MultipleMatchingUsersError, RateLimitExceeded, ResourceNotFound,
77
ServerError, ServiceUnavailableError, UnexpectedError, TokenUnauthorizedError)
88

9-
__version__ = '3.0'
9+
__version__ = '3.0.1'
1010

1111

1212
RELATED_DOCS_TEXT = "See https://github.com/jkeyes/python-intercom \

intercom/client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# -*- coding: utf-8 -*-
22

3+
import requests
4+
35

46
class Client(object):
57

68
def __init__(self, personal_access_token='my_personal_access_token'):
79
self.personal_access_token = personal_access_token
810
self.base_url = 'https://api.intercom.io'
911
self.rate_limit_details = {}
12+
self.http_session = requests.Session()
1013

1114
@property
1215
def _auth(self):
@@ -84,20 +87,20 @@ def _execute_request(self, request, params):
8487

8588
def get(self, path, params):
8689
from intercom import request
87-
req = request.Request('GET', path)
90+
req = request.Request('GET', path, self.http_session)
8891
return self._execute_request(req, params)
8992

9093
def post(self, path, params):
9194
from intercom import request
92-
req = request.Request('POST', path)
95+
req = request.Request('POST', path, self.http_session)
9396
return self._execute_request(req, params)
9497

9598
def put(self, path, params):
9699
from intercom import request
97-
req = request.Request('PUT', path)
100+
req = request.Request('PUT', path, self.http_session)
98101
return self._execute_request(req, params)
99102

100103
def delete(self, path, params):
101104
from intercom import request
102-
req = request.Request('DELETE', path)
105+
req = request.Request('DELETE', path, self.http_session)
103106
return self._execute_request(req, params)

intercom/request.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ class Request(object):
1616

1717
timeout = 10
1818

19-
def __init__(self, http_method, path):
19+
def __init__(self, http_method, path, http_session=None):
2020
self.http_method = http_method
2121
self.path = path
22+
self.http_session = http_session
23+
2224

2325
def execute(self, base_url, auth, params):
2426
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):
5355
else:
5456
logger.debug(" params: %s", req_params['data'])
5557

56-
resp = requests.request(
57-
self.http_method, url, timeout=self.timeout,
58-
auth=auth, verify=certifi.where(), **req_params)
58+
if self.http_session is None:
59+
resp = requests.request(
60+
self.http_method, url, timeout=self.timeout,
61+
auth=auth, verify=certifi.where(), **req_params)
62+
else:
63+
resp = self.http_session.request(
64+
self.http_method, url, timeout=self.timeout,
65+
auth=auth, verify=certifi.where(), **req_params)
5966

6067
# response logging
6168
if logger.isEnabledFor(logging.DEBUG):

tests/unit/test_request.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def it_raises_an_unexpected_typed_error(self):
8787
}
8888
content = json.dumps(payload).encode('utf-8')
8989
resp = mock_response(content)
90-
with patch('requests.request') as mock_method:
90+
with patch('requests.sessions.Session.request') as mock_method:
9191
mock_method.return_value = resp
9292
try:
9393
self.client.get('/users', {})
@@ -109,7 +109,7 @@ def it_raises_an_unexpected_untyped_error(self):
109109
}
110110
content = json.dumps(payload).encode('utf-8')
111111
resp = mock_response(content)
112-
with patch('requests.request') as mock_method:
112+
with patch('requests.sessions.Session.request') as mock_method:
113113
mock_method.return_value = resp
114114
try:
115115
self.client.get('/users', {})
@@ -135,7 +135,7 @@ def it_raises_a_bad_request_error(self):
135135

136136
content = json.dumps(payload).encode('utf-8')
137137
resp = mock_response(content)
138-
with patch('requests.request') as mock_method:
138+
with patch('requests.sessions.Session.request') as mock_method:
139139
mock_method.return_value = resp
140140
with assert_raises(intercom.BadRequestError):
141141
self.client.get('/users', {})
@@ -156,7 +156,7 @@ def it_raises_an_authentication_error(self):
156156

157157
content = json.dumps(payload).encode('utf-8')
158158
resp = mock_response(content)
159-
with patch('requests.request') as mock_method:
159+
with patch('requests.sessions.Session.request') as mock_method:
160160
mock_method.return_value = resp
161161
with assert_raises(intercom.AuthenticationError):
162162
self.client.get('/users', {})
@@ -174,7 +174,7 @@ def it_raises_resource_not_found_by_type(self):
174174
}
175175
content = json.dumps(payload).encode('utf-8')
176176
resp = mock_response(content)
177-
with patch('requests.request') as mock_method:
177+
with patch('requests.sessions.Session.request') as mock_method:
178178
mock_method.return_value = resp
179179
with assert_raises(intercom.ResourceNotFound):
180180
self.client.get('/users', {})
@@ -192,7 +192,7 @@ def it_raises_rate_limit_exceeded(self):
192192
}
193193
content = json.dumps(payload).encode('utf-8')
194194
resp = mock_response(content)
195-
with patch('requests.request') as mock_method:
195+
with patch('requests.sessions.Session.request') as mock_method:
196196
mock_method.return_value = resp
197197
with assert_raises(intercom.RateLimitExceeded):
198198
self.client.get('/users', {})
@@ -210,7 +210,7 @@ def it_raises_a_service_unavailable_error(self):
210210
}
211211
content = json.dumps(payload).encode('utf-8')
212212
resp = mock_response(content)
213-
with patch('requests.request') as mock_method:
213+
with patch('requests.sessions.Session.request') as mock_method:
214214
mock_method.return_value = resp
215215
with assert_raises(intercom.ServiceUnavailableError):
216216
self.client.get('/users', {})
@@ -228,7 +228,7 @@ def it_raises_a_multiple_matching_users_error(self):
228228
}
229229
content = json.dumps(payload).encode('utf-8')
230230
resp = mock_response(content)
231-
with patch('requests.request') as mock_method:
231+
with patch('requests.sessions.Session.request') as mock_method:
232232
mock_method.return_value = resp
233233
with assert_raises(intercom.MultipleMatchingUsersError):
234234
self.client.get('/users', {})
@@ -246,7 +246,7 @@ def it_raises_token_unauthorized(self):
246246
}
247247
content = json.dumps(payload).encode('utf-8')
248248
resp = mock_response(content)
249-
with patch('requests.request') as mock_method:
249+
with patch('requests.sessions.Session.request') as mock_method:
250250
mock_method.return_value = resp
251251
with assert_raises(intercom.TokenUnauthorizedError):
252252
self.client.get('/users', {})
@@ -265,7 +265,7 @@ def it_handles_no_error_type(self):
265265
}
266266
content = json.dumps(payload).encode('utf-8')
267267
resp = mock_response(content)
268-
with patch('requests.request') as mock_method:
268+
with patch('requests.sessions.Session.request') as mock_method:
269269
mock_method.return_value = resp
270270
with assert_raises(intercom.MultipleMatchingUsersError):
271271
self.client.get('/users', {})
@@ -282,7 +282,7 @@ def it_handles_no_error_type(self):
282282
}
283283
content = json.dumps(payload).encode('utf-8')
284284
resp = mock_response(content)
285-
with patch('requests.request') as mock_method:
285+
with patch('requests.sessions.Session.request') as mock_method:
286286
mock_method.return_value = resp
287287
with assert_raises(intercom.BadRequestError):
288288
self.client.get('/users', {})

tests/unit/test_user.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def it_raises_a_multiple_matching_users_error_when_receiving_a_conflict(self):
368368
content = json.dumps(payload).encode('utf-8')
369369
# create mock response
370370
resp = mock_response(content)
371-
with patch('requests.request') as mock_method:
371+
with patch('requests.sessions.Session.request') as mock_method:
372372
mock_method.return_value = resp
373373
with assert_raises(MultipleMatchingUsersError):
374374
self.client.get('/users', {})
@@ -381,7 +381,7 @@ def it_handles_accented_characters(self):
381381
content = json.dumps(payload).encode('utf-8')
382382
# create mock response
383383
resp = mock_response(content)
384-
with patch('requests.request') as mock_method:
384+
with patch('requests.sessions.Session.request') as mock_method:
385385
mock_method.return_value = resp
386386
user = self.client.users.find(email='bob@example.com')
387387
try:

0 commit comments

Comments
 (0)