diff --git a/intercom/request.py b/intercom/request.py index ca664c6..820f859 100644 --- a/intercom/request.py +++ b/intercom/request.py @@ -7,21 +7,31 @@ import certifi import json import logging +import os import requests logger = logging.getLogger('intercom.request') +def configure_timeout(): + """Configure the request timeout.""" + timeout = os.getenv('INTERCOM_REQUEST_TIMEOUT', '90') + try: + return int(timeout) + except ValueError: + logger.warning('%s is not a valid timeout value.', timeout) + return 90 + + class Request(object): - timeout = 10 + timeout = configure_timeout() 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) diff --git a/tests/unit/test_request.py b/tests/unit/test_request.py index bcd1240..fdfa779 100644 --- a/tests/unit/test_request.py +++ b/tests/unit/test_request.py @@ -332,6 +332,25 @@ def it_needs_encoding_or_apparent_encoding(self): @istest def it_allows_the_timeout_to_be_changed(self): from intercom.request import Request - eq_(10, Request.timeout) - Request.timeout = 3 - eq_(3, Request.timeout) + try: + eq_(90, Request.timeout) + Request.timeout = 3 + eq_(3, Request.timeout) + finally: + Request.timeout = 90 + + @istest + def it_allows_the_timeout_to_be_configured(self): + import os + from intercom.request import configure_timeout + + # check the default + eq_(90, configure_timeout()) + + # override the default + os.environ['INTERCOM_REQUEST_TIMEOUT'] = '20' + eq_(20, configure_timeout()) + + # ignore bad timeouts, reset to default 90 + os.environ['INTERCOM_REQUEST_TIMEOUT'] = 'abc' + eq_(90, configure_timeout())