Skip to content

Commit

Permalink
Update restsession types and type-checking
Browse files Browse the repository at this point in the history
  • Loading branch information
cmlccie committed Mar 4, 2018
1 parent 339a372 commit c3095ed
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions ciscosparkapi/restsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
)

from future import standard_library


standard_library.install_aliases()

from builtins import *
Expand All @@ -26,11 +28,10 @@
)
from .response_codes import EXPECTED_RESPONSE_CODE
from .utils import (
check_response_code, extract_and_parse_json, validate_base_url,
check_type, check_response_code, extract_and_parse_json, validate_base_url,
)



__author__ = "Chris Lunsford"
__author_email__ = "chrlunsf@cisco.com"
__copyright__ = "Copyright (c) 2016-2018 Cisco and/or its affiliates."
Expand Down Expand Up @@ -101,18 +102,20 @@ def __init__(self, access_token, base_url, timeout=None,
base_url(basestring): The base URL that will be suffixed onto API
endpoint relative URLs to produce a callable absolute URL.
timeout: [Deprecated] The timeout (seconds) for an API request.
single_request_timeout(float): The timeout (seconds) for a single
single_request_timeout(int): The timeout (seconds) for a single
HTTP REST API request.
wait_on_rate_limit(bool): Enable or disable automatic rate-limit
handling.
Raises:
TypeError: If the parameter types are incorrect.
"""
assert isinstance(access_token, basestring)
assert isinstance(base_url, basestring)
assert timeout is None or isinstance(timeout, (int, float))
assert (single_request_timeout is None or
isinstance(single_request_timeout, (int, float)))
assert isinstance(wait_on_rate_limit, bool)
check_type(access_token, basestring, may_be_none=False)
check_type(base_url, basestring, may_be_none=False)
check_type(timeout, int)
check_type(single_request_timeout, int)
check_type(wait_on_rate_limit, bool, may_be_none=False)

super(RestSession, self).__init__()

Expand Down Expand Up @@ -165,8 +168,9 @@ def timeout(self, value):
warnings.warn("The 'timeout' property is being deprecated. Please use "
"the 'single_request_timeout' instead.",
DeprecationWarning)
check_type(value, int)
assert value is None or value > 0
self._single_request_timeout = float(value)
self._single_request_timeout = value

@property
def single_request_timeout(self):
Expand All @@ -176,9 +180,9 @@ def single_request_timeout(self):
@single_request_timeout.setter
def single_request_timeout(self, value):
"""The timeout (seconds) for a single HTTP REST API request."""
assert (value is None or
(isinstance(value, (int, float)) and value > 0.0))
self._single_request_timeout = float(value)
check_type(value, int)
assert value is None or value > 0
self._single_request_timeout = value

@property
def wait_on_rate_limit(self):
Expand All @@ -195,7 +199,7 @@ def wait_on_rate_limit(self):
@wait_on_rate_limit.setter
def wait_on_rate_limit(self, value):
"""Enable or disable automatic rate-limit handling."""
assert isinstance(value, bool)
check_type(value, bool, may_be_none=False)
self._wait_on_rate_limit = value

@property
Expand All @@ -215,7 +219,7 @@ def update_headers(self, headers):
headers(dict): Updates to the current session headers.
"""
assert isinstance(headers, dict)
check_type(headers, dict, may_be_none=False)
self._req_session.headers.update(headers)

def abs_url(self, url):
Expand Down Expand Up @@ -257,8 +261,6 @@ def request(self, method, url, erc, **kwargs):
returned by the Cisco Spark API endpoint.
"""
logger = logging.getLogger(__name__)

# Ensure the url is an absolute URL
abs_url = self.abs_url(url)

Expand All @@ -272,20 +274,16 @@ def request(self, method, url, erc, **kwargs):
try:
# Check the response code for error conditions
check_response_code(response, erc)

except SparkRateLimitError as e:
# Catch rate-limit errors

# Wait and retry if automatic rate-limit handling is enabled
if self.wait_on_rate_limit and e.retry_after:
warnings.warn(SparkRateLimitWarning(response))
time.sleep(e.retry_after)
continue

else:
# Re-raise the SparkRateLimitError
raise

else:
return response

Expand All @@ -304,8 +302,8 @@ def get(self, url, params=None, **kwargs):
returned by the Cisco Spark API endpoint.
"""
assert isinstance(url, basestring)
assert params is None or isinstance(params, dict)
check_type(url, basestring, may_be_none=False)
check_type(params, dict)

# Expected response code
erc = kwargs.pop('erc', EXPECTED_RESPONSE_CODE['GET'])
Expand All @@ -330,8 +328,8 @@ def get_pages(self, url, params=None, **kwargs):
returned by the Cisco Spark API endpoint.
"""
assert isinstance(url, basestring)
assert params is None or isinstance(params, dict)
check_type(url, basestring, may_be_none=False)
check_type(params, dict)

# Expected response code
erc = kwargs.pop('erc', EXPECTED_RESPONSE_CODE['GET'])
Expand Down Expand Up @@ -412,7 +410,7 @@ def post(self, url, json=None, data=None, **kwargs):
returned by the Cisco Spark API endpoint.
"""
assert isinstance(url, basestring)
check_type(url, basestring, may_be_none=False)

# Expected response code
erc = kwargs.pop('erc', EXPECTED_RESPONSE_CODE['POST'])
Expand All @@ -437,7 +435,7 @@ def put(self, url, json=None, data=None, **kwargs):
returned by the Cisco Spark API endpoint.
"""
assert isinstance(url, basestring)
check_type(url, basestring, may_be_none=False)

# Expected response code
erc = kwargs.pop('erc', EXPECTED_RESPONSE_CODE['PUT'])
Expand All @@ -460,7 +458,7 @@ def delete(self, url, **kwargs):
returned by the Cisco Spark API endpoint.
"""
assert isinstance(url, basestring)
check_type(url, basestring, may_be_none=False)

# Expected response code
erc = kwargs.pop('erc', EXPECTED_RESPONSE_CODE['DELETE'])
Expand Down

0 comments on commit c3095ed

Please sign in to comment.