Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
add timeout to the client
Browse files Browse the repository at this point in the history
  • Loading branch information
steve brazier committed Jun 25, 2020
1 parent 727b01a commit 89126ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/nexmo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Client:
provided by this library and can be used by Nexmo to track your app statistics.
:param str app_version: This optional value is added to the user-agent header
provided by this library and can be used by Nexmo to track your app statistics.
:param float timeout: This optional value sets the timeout value for calling the api.
"""

def __init__(
Expand All @@ -71,6 +72,7 @@ def __init__(
private_key=None,
app_name=None,
app_version=None,
timeout=None
):
self.api_key = key or os.environ.get("NEXMO_API_KEY", None)

Expand Down Expand Up @@ -99,6 +101,8 @@ def __init__(

self.api_host = "api.nexmo.com"

self.timeout = timeout

user_agent = "nexmo-python/{version} python/{python_version}".format(
version=__version__, python_version=python_version()
)
Expand All @@ -117,6 +121,7 @@ def __init__(
user_agent=user_agent,
api_key=self.api_key,
api_secret=self.api_secret,
timeout=self.timeout
)
self.application_v2 = ApplicationV2(api_server)

Expand Down Expand Up @@ -510,7 +515,7 @@ def get(self, host, request_uri, params=None, header_auth=False):
params or {}, api_key=self.api_key, api_secret=self.api_secret
)
logger.debug("GET to %r with params %r, headers %r", uri, params, headers)
return self.parse(host, self.session.get(uri, params=params, headers=headers))
return self.parse(host, self.session.get(uri, params=params, headers=headers, timeout=self.timeout))

def post(
self,
Expand Down Expand Up @@ -546,7 +551,7 @@ def post(
else:
params = dict(params, api_key=self.api_key, api_secret=self.api_secret)
logger.debug("POST to %r with params %r, headers %r", uri, params, headers)
return self.parse(host, self.session.post(uri, data=params, headers=headers))
return self.parse(host, self.session.post(uri, data=params, headers=headers, timeout=self.timeout))

def _post_json(self, host, request_uri, json):
"""
Expand All @@ -566,7 +571,7 @@ def _post_json(self, host, request_uri, json):
logger.debug(
"POST to %r with body: %r, headers: %r", request_uri, json, headers
)
return self.parse(host, self.session.post(uri, headers=headers, json=json))
return self.parse(host, self.session.post(uri, headers=headers, json=json, timeout=self.timeout))

def put(self, host, request_uri, params, header_auth=False):
uri = "https://{host}{request_uri}".format(host=host, request_uri=request_uri)
Expand All @@ -585,7 +590,7 @@ def put(self, host, request_uri, params, header_auth=False):
else:
params = dict(params, api_key=self.api_key, api_secret=self.api_secret)
logger.debug("PUT to %r with params %r, headers %r", uri, params, headers)
return self.parse(host, self.session.put(uri, json=params, headers=headers))
return self.parse(host, self.session.put(uri, json=params, headers=headers, timeout=self.timeout))

def delete(self, host, request_uri, header_auth=False):
uri = "https://{host}{request_uri}".format(host=host, request_uri=request_uri)
Expand All @@ -606,7 +611,7 @@ def delete(self, host, request_uri, header_auth=False):
params = {"api_key": self.api_key, "api_secret": self.api_secret}
logger.debug("DELETE to %r with params %r, headers %r", uri, params, headers)
return self.parse(
host, self.session.delete(uri, params=params, headers=headers)
host, self.session.delete(uri, params=params, headers=headers, timeout=self.timeout)
)

def parse(self, host, response):
Expand Down
11 changes: 6 additions & 5 deletions src/nexmo/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@


class BasicAuthenticatedServer(object):
def __init__(self, host, user_agent, api_key, api_secret):
def __init__(self, host, user_agent, api_key, api_secret, timeout=None):
self._host = host
self._session = session = Session()
self.timeout = None
session.auth = (api_key, api_secret) # Basic authentication.
session.headers.update({"User-Agent": user_agent})

Expand All @@ -24,22 +25,22 @@ def _uri(self, path):

def get(self, path, params=None, headers=None):
return self._parse(
self._session.get(self._uri(path), params=params, headers=headers)
self._session.get(self._uri(path), params=params, headers=headers, timeout=self.timeout)
)

def post(self, path, body=None, headers=None):
return self._parse(
self._session.post(self._uri(path), json=body, headers=headers)
self._session.post(self._uri(path), json=body, headers=headers, timeout=self.timeout)
)

def put(self, path, body=None, headers=None):
return self._parse(
self._session.put(self._uri(path), json=body, headers=headers)
self._session.put(self._uri(path), json=body, headers=headers, timeout=self.timeout)
)

def delete(self, path, body=None, headers=None):
return self._parse(
self._session.delete(self._uri(path), json=body, headers=headers)
self._session.delete(self._uri(path), json=body, headers=headers, timeout=self.timeout)
)

def _parse(self, response):
Expand Down

0 comments on commit 89126ce

Please sign in to comment.