Skip to content

Commit

Permalink
Merge pull request gmr#31 from fizyk/timeouts
Browse files Browse the repository at this point in the history
add timeouts for adatapter and pass it from Consul client - refs gmr#30
  • Loading branch information
gmr committed May 13, 2015
2 parents 44e792e + fc18d41 commit c8c44ef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
7 changes: 5 additions & 2 deletions consulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ class Consul(object):
"""
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT,
datacenter=None, token=None):
datacenter=None, token=None, adapter=None):
"""Create a new instance of the Consul class"""
base_uri = self._base_uri(host, port)
self._adapter = adapters.Request()
if adapter:
self._adapter = adapter
else:
self._adapter = adapters.Request()
self._acl = api.ACL(base_uri, self._adapter, datacenter, token)
self._agent = api.Agent(base_uri, self._adapter, datacenter, token)
self._catalog = api.Catalog(base_uri, self._adapter, datacenter, token)
Expand Down
20 changes: 15 additions & 5 deletions consulate/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ def inner(*args, **kwargs):


class Request(object):

"""The Request adapter class"""
def __init__(self):
"""Create a new request adapter instance"""

def __init__(self, timeout=None):
"""
Create a new request adapter instance.
:param int timeout: [optional] timeout to use while sending requests
to consul.
"""
self.session = requests.Session()
self._timeout = timeout

def delete(self, uri):
"""Perform a HTTP delete
Expand All @@ -54,7 +62,7 @@ def delete(self, uri):
"""
LOGGER.debug("DELETE %s", uri)
response = self.session.delete(uri)
response = self.session.delete(uri, timeout=self._timeout)
return api.Response(response.status_code,
response.content,
response.headers)
Expand All @@ -67,7 +75,7 @@ def get(self, uri):
"""
LOGGER.debug("GET %s", uri)
response = self.session.get(uri)
response = self.session.get(uri, timeout=self._timeout)
return api.Response(response.status_code,
response.content,
response.headers)
Expand All @@ -88,7 +96,9 @@ def put(self, uri, data=None):
headers = {'Content-Type': CONTENT_JSON}
if not utils.PYTHON3 and data:
data = data.encode('utf-8')
response = self.session.put(uri, data=data, headers=headers)
response = self.session.put(
uri, data=data, headers=headers, timeout=self._timeout
)
return api.Response(response.status_code,
response.content,
response.headers)

0 comments on commit c8c44ef

Please sign in to comment.