Skip to content

Commit

Permalink
Making the request timeout configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkeyes committed Feb 13, 2017
1 parent 37d8c97 commit d67d782
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
14 changes: 12 additions & 2 deletions intercom/request.py
Expand Up @@ -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)

Expand Down
25 changes: 22 additions & 3 deletions tests/unit/test_request.py
Expand Up @@ -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())

0 comments on commit d67d782

Please sign in to comment.