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

#629 Implement default request timeouts #630

Open
wants to merge 3 commits into
base: develop
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
8 changes: 6 additions & 2 deletions canvasapi/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ class Canvas(object):
The main class to be instantiated to provide access to Canvas's API.
"""

def __init__(self, base_url, access_token):
def __init__(self, base_url, access_token, default_timeout=None):
"""
:param base_url: The base URL of the Canvas instance's API.
:type base_url: str
:param access_token: The API key to authenticate requests with.
:type access_token: str
:param default_timeout: The default timeout for all requests in seconds.
:type default_timeout: int
"""
if "api/v1" in base_url:
raise ValueError(
Expand Down Expand Up @@ -70,7 +72,9 @@ def __init__(self, base_url, access_token):
access_token = access_token.strip()
base_url = get_institution_url(base_url)

self.__requester = Requester(base_url, access_token)
self.__requester = Requester(
base_url, access_token, default_timeout=default_timeout
)

def clear_course_nicknames(self, **kwargs):
"""
Expand Down
29 changes: 22 additions & 7 deletions canvasapi/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ class Requester(object):
Responsible for handling HTTP requests.
"""

def __init__(self, base_url, access_token):
def __init__(self, base_url, access_token, default_timeout=None):
"""
:param base_url: The base URL of the Canvas instance's API.
:type base_url: str
:param access_token: The API key to authenticate requests with.
:type access_token: str
:param default_timeout: The default timeout for all requests in seconds.
:type default_timeout: int
"""
# Preserve the original base url and add "/api/v1" to it
self.original_url = base_url
Expand All @@ -40,6 +42,7 @@ def __init__(self, base_url, access_token):
self.access_token = access_token
self._session = requests.Session()
self._cache = []
self._default_timeout = default_timeout

def _delete_request(self, url, headers, data=None, **kwargs):
"""
Expand All @@ -52,7 +55,9 @@ def _delete_request(self, url, headers, data=None, **kwargs):
:param data: The data to send with this request.
:type data: dict
"""
return self._session.delete(url, headers=headers, data=data)
return self._session.delete(
url, headers=headers, data=data, timeout=self._default_timeout
)

def _get_request(self, url, headers, params=None, **kwargs):
"""
Expand All @@ -65,7 +70,9 @@ def _get_request(self, url, headers, params=None, **kwargs):
:param params: The parameters to send with this request.
:type params: dict
"""
return self._session.get(url, headers=headers, params=params)
return self._session.get(
url, headers=headers, params=params, timeout=self._default_timeout
)

def _patch_request(self, url, headers, data=None, **kwargs):
"""
Expand All @@ -78,7 +85,9 @@ def _patch_request(self, url, headers, data=None, **kwargs):
:param data: The data to send with this request.
:type data: dict
"""
return self._session.patch(url, headers=headers, data=data)
return self._session.patch(
url, headers=headers, data=data, timeout=self._default_timeout
)

def _post_request(self, url, headers, data=None, json=False):
"""
Expand All @@ -94,7 +103,9 @@ def _post_request(self, url, headers, data=None, json=False):
:type json: bool
"""
if json:
return self._session.post(url, headers=headers, json=dict(data))
return self._session.post(
url, headers=headers, json=dict(data), timeout=self._default_timeout
)

# Grab file from data.
files = None
Expand All @@ -109,7 +120,9 @@ def _post_request(self, url, headers, data=None, json=False):
# Remove file entry from data.
data[:] = [tup for tup in data if tup[0] != "file"]

return self._session.post(url, headers=headers, data=data, files=files)
return self._session.post(
url, headers=headers, data=data, files=files, timeout=self._default_timeout
)

def _put_request(self, url, headers, data=None, **kwargs):
"""
Expand All @@ -122,7 +135,9 @@ def _put_request(self, url, headers, data=None, **kwargs):
:param data: The data to send with this request.
:type data: dict
"""
return self._session.put(url, headers=headers, data=data)
return self._session.put(
url, headers=headers, data=data, timeout=self._default_timeout
)

def request(
self,
Expand Down
Loading