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

Add connect/read timeout option #215

Merged
merged 4 commits into from
May 12, 2020
Merged
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
12 changes: 8 additions & 4 deletions auth0/v3/authentication/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ class AuthenticationBase(object):
Args:
telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)
timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, telemetry=True):
def __init__(self, domain, telemetry=True, timeout=5.0):
self.domain = domain

self.timeout = timeout
self.base_headers = {'Content-Type': 'application/json'}

if telemetry:
Expand All @@ -43,13 +47,13 @@ def __init__(self, domain, telemetry=True):
def post(self, url, data=None, headers=None):
request_headers = self.base_headers.copy()
request_headers.update(headers or {})
response = requests.post(url=url, json=data, headers=request_headers)
response = requests.post(url=url, json=data, headers=request_headers, timeout=self.timeout)
return self._process_response(response)

def get(self, url, params=None, headers=None):
request_headers = self.base_headers.copy()
request_headers.update(headers or {})
response = requests.get(url=url, params=params, headers=request_headers)
response = requests.get(url=url, params=params, headers=request_headers, timeout=self.timeout)
return self._process_response(response)

def _process_response(self, response):
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/blacklists.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ class Blacklists(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.url = 'https://{}/api/v2/blacklists/tokens'.format(domain)
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def get(self, aud=None):
"""Retrieves the jti and aud of all tokens in the blacklist.
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/client_grants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ class ClientGrants(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.domain = domain
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def _url(self, id=None):
url = 'https://{}/api/v2/client-grants'.format(self.domain)
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ class Clients(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.domain = domain
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def _url(self, id=None):
url = 'https://{}/api/v2/clients'.format(self.domain)
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ class Connections(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.domain = domain
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def _url(self, id=None):
url = 'https://{}/api/v2/connections'.format(self.domain)
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/custom_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ class CustomDomains(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.domain = domain
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def _url(self, id=None):
url = 'https://%s/api/v2/custom-domains' % self.domain
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/device_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ class DeviceCredentials(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.domain = domain
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def _url(self, id=None):
url = 'https://{}/api/v2/device-credentials'.format(self.domain)
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/email_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ class EmailTemplates(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.domain = domain
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def _url(self, id=None):
url = 'https://{}/api/v2/email-templates'.format(self.domain)
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ class Emails(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.domain = domain
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def _url(self, id=None):
url = 'https://{}/api/v2/emails/provider'.format(self.domain)
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/grants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ class Grants(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.domain = domain
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def _url(self, id=None):
url = 'https://%s/api/v2/grants' % self.domain
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/guardian.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ class Guardian(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.domain = domain
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def _url(self, id=None):
url = 'https://{}/api/v2/guardian'.format(self.domain)
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ class Jobs(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.domain = domain
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def _url(self, path=None):
url = 'https://{}/api/v2/jobs'.format(self.domain)
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ class Logs(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.domain = domain
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def _url(self, id=None):
url = 'https://{}/api/v2/logs'.format(self.domain)
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/resource_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ class ResourceServers(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.domain = domain
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def _url(self, id=None):
url = 'https://{}/api/v2/resource-servers'.format(self.domain)
Expand Down
27 changes: 18 additions & 9 deletions auth0/v3/management/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@

class RestClient(object):

"""Provides simple methods for handling all RESTful api endpoints. """

def __init__(self, jwt, telemetry=True):
"""Provides simple methods for handling all RESTful api endpoints.

Args:
telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)
timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""
def __init__(self, jwt, telemetry=True, timeout=5.0):
self.jwt = jwt
self.timeout = timeout

self.base_headers = {
'Authorization': 'Bearer {}'.format(self.jwt),
Expand All @@ -40,38 +49,38 @@ def __init__(self, jwt, telemetry=True):
def get(self, url, params=None):
headers = self.base_headers.copy()

response = requests.get(url, params=params, headers=headers)
response = requests.get(url, params=params, headers=headers, timeout=self.timeout)
return self._process_response(response)

def post(self, url, data=None):
headers = self.base_headers.copy()

response = requests.post(url, json=data, headers=headers)
response = requests.post(url, json=data, headers=headers, timeout=self.timeout)
return self._process_response(response)

def file_post(self, url, data=None, files=None):
headers = self.base_headers.copy()
headers.pop('Content-Type', None)

response = requests.post(url, data=data, files=files, headers=headers)
response = requests.post(url, data=data, files=files, headers=headers, timeout=self.timeout)
return self._process_response(response)

def patch(self, url, data=None):
headers = self.base_headers.copy()

response = requests.patch(url, json=data, headers=headers)
response = requests.patch(url, json=data, headers=headers, timeout=self.timeout)
return self._process_response(response)

def put(self, url, data=None):
headers = self.base_headers.copy()

response = requests.put(url, json=data, headers=headers)
response = requests.put(url, json=data, headers=headers, timeout=self.timeout)
return self._process_response(response)

def delete(self, url, params=None, data=None):
headers = self.base_headers.copy()

response = requests.delete(url, headers=headers, params=params or {}, json=data)
response = requests.delete(url, headers=headers, params=params or {}, json=data, timeout=self.timeout)
return self._process_response(response)

def _process_response(self, response):
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ class Roles(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.domain = domain
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def _url(self, id=None):
url = 'https://{}/api/v2/roles'.format(self.domain)
Expand Down
9 changes: 7 additions & 2 deletions auth0/v3/management/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ class Rules(object):

telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)

timeout (float or tuple, optional): Change the requests
connect and read timeout. Pass a tuple to specify
both values separately or a float to set both to it.
(defaults to 5.0 for both)
"""

def __init__(self, domain, token, telemetry=True):
def __init__(self, domain, token, telemetry=True, timeout=5.0):
self.domain = domain
self.client = RestClient(jwt=token, telemetry=telemetry)
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)

def _url(self, id=None):
url = 'https://{}/api/v2/rules'.format(self.domain)
Expand Down
Loading