Skip to content

Commit 6534f21

Browse files
authored
Merge pull request intercom#154 from jkeyes/increase-timeout
Making the request timeout configurable.
2 parents 37d8c97 + d67d782 commit 6534f21

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

intercom/request.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,31 @@
77
import certifi
88
import json
99
import logging
10+
import os
1011
import requests
1112

1213
logger = logging.getLogger('intercom.request')
1314

1415

16+
def configure_timeout():
17+
"""Configure the request timeout."""
18+
timeout = os.getenv('INTERCOM_REQUEST_TIMEOUT', '90')
19+
try:
20+
return int(timeout)
21+
except ValueError:
22+
logger.warning('%s is not a valid timeout value.', timeout)
23+
return 90
24+
25+
1526
class Request(object):
1627

17-
timeout = 10
28+
timeout = configure_timeout()
1829

1930
def __init__(self, http_method, path, http_session=None):
2031
self.http_method = http_method
2132
self.path = path
2233
self.http_session = http_session
2334

24-
2535
def execute(self, base_url, auth, params):
2636
return self.send_request_to_path(base_url, auth, params)
2737

tests/unit/test_request.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,25 @@ def it_needs_encoding_or_apparent_encoding(self):
332332
@istest
333333
def it_allows_the_timeout_to_be_changed(self):
334334
from intercom.request import Request
335-
eq_(10, Request.timeout)
336-
Request.timeout = 3
337-
eq_(3, Request.timeout)
335+
try:
336+
eq_(90, Request.timeout)
337+
Request.timeout = 3
338+
eq_(3, Request.timeout)
339+
finally:
340+
Request.timeout = 90
341+
342+
@istest
343+
def it_allows_the_timeout_to_be_configured(self):
344+
import os
345+
from intercom.request import configure_timeout
346+
347+
# check the default
348+
eq_(90, configure_timeout())
349+
350+
# override the default
351+
os.environ['INTERCOM_REQUEST_TIMEOUT'] = '20'
352+
eq_(20, configure_timeout())
353+
354+
# ignore bad timeouts, reset to default 90
355+
os.environ['INTERCOM_REQUEST_TIMEOUT'] = 'abc'
356+
eq_(90, configure_timeout())

0 commit comments

Comments
 (0)