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

Configure request timeout with parameter instead of an env variable #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions intercom/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

class Client(object):

def __init__(self, personal_access_token='my_personal_access_token'):
def __init__(self, personal_access_token='my_personal_access_token', timeout=90):
self.personal_access_token = personal_access_token
self.base_url = 'https://api.intercom.io'
self.rate_limit_details = {}
self.http_session = requests.Session()
self.timeout = timeout

@property
def _auth(self):
Expand Down Expand Up @@ -87,20 +88,20 @@ def _execute_request(self, request, params):

def get(self, path, params):
from intercom import request
req = request.Request('GET', path, self.http_session)
req = request.Request('GET', path, self.http_session, timeout=self.timeout)
return self._execute_request(req, params)

def post(self, path, params):
from intercom import request
req = request.Request('POST', path, self.http_session)
req = request.Request('POST', path, self.http_session, timeout=self.timeout)
return self._execute_request(req, params)

def put(self, path, params):
from intercom import request
req = request.Request('PUT', path, self.http_session)
req = request.Request('PUT', path, self.http_session, timeout=self.timeout)
return self._execute_request(req, params)

def delete(self, path, params):
from intercom import request
req = request.Request('DELETE', path, self.http_session)
req = request.Request('DELETE', path, self.http_session, timeout=self.timeout)
return self._execute_request(req, params)
15 changes: 2 additions & 13 deletions intercom/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,13 @@
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 = configure_timeout()

def __init__(self, http_method, path, http_session=None):
def __init__(self, http_method, path, http_session=None, timeout=90):
self.http_method = http_method
self.path = path
self.http_session = http_session
self.timeout = timeout

def execute(self, base_url, auth, params):
return self.send_request_to_path(base_url, auth, params)
Expand Down
26 changes: 4 additions & 22 deletions tests/unit/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,25 +332,7 @@ def it_needs_encoding_or_apparent_encoding(self):
@istest
def it_allows_the_timeout_to_be_changed(self):
from intercom.request import Request
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())
request = Request('GET', '/')
eq_(90, request.timeout)
request = Request('GET', '/', timeout=3)
eq_(3, request.timeout)