Skip to content

Commit c3095ed

Browse files
committed
Update restsession types and type-checking
1 parent 339a372 commit c3095ed

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

ciscosparkapi/restsession.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
)
1111

1212
from future import standard_library
13+
14+
1315
standard_library.install_aliases()
1416

1517
from builtins import *
@@ -26,11 +28,10 @@
2628
)
2729
from .response_codes import EXPECTED_RESPONSE_CODE
2830
from .utils import (
29-
check_response_code, extract_and_parse_json, validate_base_url,
31+
check_type, check_response_code, extract_and_parse_json, validate_base_url,
3032
)
3133

3234

33-
3435
__author__ = "Chris Lunsford"
3536
__author_email__ = "[email protected]"
3637
__copyright__ = "Copyright (c) 2016-2018 Cisco and/or its affiliates."
@@ -101,18 +102,20 @@ def __init__(self, access_token, base_url, timeout=None,
101102
base_url(basestring): The base URL that will be suffixed onto API
102103
endpoint relative URLs to produce a callable absolute URL.
103104
timeout: [Deprecated] The timeout (seconds) for an API request.
104-
single_request_timeout(float): The timeout (seconds) for a single
105+
single_request_timeout(int): The timeout (seconds) for a single
105106
HTTP REST API request.
106107
wait_on_rate_limit(bool): Enable or disable automatic rate-limit
107108
handling.
108109
110+
Raises:
111+
TypeError: If the parameter types are incorrect.
112+
109113
"""
110-
assert isinstance(access_token, basestring)
111-
assert isinstance(base_url, basestring)
112-
assert timeout is None or isinstance(timeout, (int, float))
113-
assert (single_request_timeout is None or
114-
isinstance(single_request_timeout, (int, float)))
115-
assert isinstance(wait_on_rate_limit, bool)
114+
check_type(access_token, basestring, may_be_none=False)
115+
check_type(base_url, basestring, may_be_none=False)
116+
check_type(timeout, int)
117+
check_type(single_request_timeout, int)
118+
check_type(wait_on_rate_limit, bool, may_be_none=False)
116119

117120
super(RestSession, self).__init__()
118121

@@ -165,8 +168,9 @@ def timeout(self, value):
165168
warnings.warn("The 'timeout' property is being deprecated. Please use "
166169
"the 'single_request_timeout' instead.",
167170
DeprecationWarning)
171+
check_type(value, int)
168172
assert value is None or value > 0
169-
self._single_request_timeout = float(value)
173+
self._single_request_timeout = value
170174

171175
@property
172176
def single_request_timeout(self):
@@ -176,9 +180,9 @@ def single_request_timeout(self):
176180
@single_request_timeout.setter
177181
def single_request_timeout(self, value):
178182
"""The timeout (seconds) for a single HTTP REST API request."""
179-
assert (value is None or
180-
(isinstance(value, (int, float)) and value > 0.0))
181-
self._single_request_timeout = float(value)
183+
check_type(value, int)
184+
assert value is None or value > 0
185+
self._single_request_timeout = value
182186

183187
@property
184188
def wait_on_rate_limit(self):
@@ -195,7 +199,7 @@ def wait_on_rate_limit(self):
195199
@wait_on_rate_limit.setter
196200
def wait_on_rate_limit(self, value):
197201
"""Enable or disable automatic rate-limit handling."""
198-
assert isinstance(value, bool)
202+
check_type(value, bool, may_be_none=False)
199203
self._wait_on_rate_limit = value
200204

201205
@property
@@ -215,7 +219,7 @@ def update_headers(self, headers):
215219
headers(dict): Updates to the current session headers.
216220
217221
"""
218-
assert isinstance(headers, dict)
222+
check_type(headers, dict, may_be_none=False)
219223
self._req_session.headers.update(headers)
220224

221225
def abs_url(self, url):
@@ -257,8 +261,6 @@ def request(self, method, url, erc, **kwargs):
257261
returned by the Cisco Spark API endpoint.
258262
259263
"""
260-
logger = logging.getLogger(__name__)
261-
262264
# Ensure the url is an absolute URL
263265
abs_url = self.abs_url(url)
264266

@@ -272,20 +274,16 @@ def request(self, method, url, erc, **kwargs):
272274
try:
273275
# Check the response code for error conditions
274276
check_response_code(response, erc)
275-
276277
except SparkRateLimitError as e:
277278
# Catch rate-limit errors
278-
279279
# Wait and retry if automatic rate-limit handling is enabled
280280
if self.wait_on_rate_limit and e.retry_after:
281281
warnings.warn(SparkRateLimitWarning(response))
282282
time.sleep(e.retry_after)
283283
continue
284-
285284
else:
286285
# Re-raise the SparkRateLimitError
287286
raise
288-
289287
else:
290288
return response
291289

@@ -304,8 +302,8 @@ def get(self, url, params=None, **kwargs):
304302
returned by the Cisco Spark API endpoint.
305303
306304
"""
307-
assert isinstance(url, basestring)
308-
assert params is None or isinstance(params, dict)
305+
check_type(url, basestring, may_be_none=False)
306+
check_type(params, dict)
309307

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

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

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

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

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

0 commit comments

Comments
 (0)