10
10
)
11
11
12
12
from future import standard_library
13
+
14
+
13
15
standard_library .install_aliases ()
14
16
15
17
from builtins import *
26
28
)
27
29
from .response_codes import EXPECTED_RESPONSE_CODE
28
30
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 ,
30
32
)
31
33
32
34
33
-
34
35
__author__ = "Chris Lunsford"
35
36
__author_email__ = "[email protected] "
36
37
__copyright__ = "Copyright (c) 2016-2018 Cisco and/or its affiliates."
@@ -101,18 +102,20 @@ def __init__(self, access_token, base_url, timeout=None,
101
102
base_url(basestring): The base URL that will be suffixed onto API
102
103
endpoint relative URLs to produce a callable absolute URL.
103
104
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
105
106
HTTP REST API request.
106
107
wait_on_rate_limit(bool): Enable or disable automatic rate-limit
107
108
handling.
108
109
110
+ Raises:
111
+ TypeError: If the parameter types are incorrect.
112
+
109
113
"""
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 )
116
119
117
120
super (RestSession , self ).__init__ ()
118
121
@@ -165,8 +168,9 @@ def timeout(self, value):
165
168
warnings .warn ("The 'timeout' property is being deprecated. Please use "
166
169
"the 'single_request_timeout' instead." ,
167
170
DeprecationWarning )
171
+ check_type (value , int )
168
172
assert value is None or value > 0
169
- self ._single_request_timeout = float ( value )
173
+ self ._single_request_timeout = value
170
174
171
175
@property
172
176
def single_request_timeout (self ):
@@ -176,9 +180,9 @@ def single_request_timeout(self):
176
180
@single_request_timeout .setter
177
181
def single_request_timeout (self , value ):
178
182
"""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
182
186
183
187
@property
184
188
def wait_on_rate_limit (self ):
@@ -195,7 +199,7 @@ def wait_on_rate_limit(self):
195
199
@wait_on_rate_limit .setter
196
200
def wait_on_rate_limit (self , value ):
197
201
"""Enable or disable automatic rate-limit handling."""
198
- assert isinstance (value , bool )
202
+ check_type (value , bool , may_be_none = False )
199
203
self ._wait_on_rate_limit = value
200
204
201
205
@property
@@ -215,7 +219,7 @@ def update_headers(self, headers):
215
219
headers(dict): Updates to the current session headers.
216
220
217
221
"""
218
- assert isinstance (headers , dict )
222
+ check_type (headers , dict , may_be_none = False )
219
223
self ._req_session .headers .update (headers )
220
224
221
225
def abs_url (self , url ):
@@ -257,8 +261,6 @@ def request(self, method, url, erc, **kwargs):
257
261
returned by the Cisco Spark API endpoint.
258
262
259
263
"""
260
- logger = logging .getLogger (__name__ )
261
-
262
264
# Ensure the url is an absolute URL
263
265
abs_url = self .abs_url (url )
264
266
@@ -272,20 +274,16 @@ def request(self, method, url, erc, **kwargs):
272
274
try :
273
275
# Check the response code for error conditions
274
276
check_response_code (response , erc )
275
-
276
277
except SparkRateLimitError as e :
277
278
# Catch rate-limit errors
278
-
279
279
# Wait and retry if automatic rate-limit handling is enabled
280
280
if self .wait_on_rate_limit and e .retry_after :
281
281
warnings .warn (SparkRateLimitWarning (response ))
282
282
time .sleep (e .retry_after )
283
283
continue
284
-
285
284
else :
286
285
# Re-raise the SparkRateLimitError
287
286
raise
288
-
289
287
else :
290
288
return response
291
289
@@ -304,8 +302,8 @@ def get(self, url, params=None, **kwargs):
304
302
returned by the Cisco Spark API endpoint.
305
303
306
304
"""
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 )
309
307
310
308
# Expected response code
311
309
erc = kwargs .pop ('erc' , EXPECTED_RESPONSE_CODE ['GET' ])
@@ -330,8 +328,8 @@ def get_pages(self, url, params=None, **kwargs):
330
328
returned by the Cisco Spark API endpoint.
331
329
332
330
"""
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 )
335
333
336
334
# Expected response code
337
335
erc = kwargs .pop ('erc' , EXPECTED_RESPONSE_CODE ['GET' ])
@@ -412,7 +410,7 @@ def post(self, url, json=None, data=None, **kwargs):
412
410
returned by the Cisco Spark API endpoint.
413
411
414
412
"""
415
- assert isinstance (url , basestring )
413
+ check_type (url , basestring , may_be_none = False )
416
414
417
415
# Expected response code
418
416
erc = kwargs .pop ('erc' , EXPECTED_RESPONSE_CODE ['POST' ])
@@ -437,7 +435,7 @@ def put(self, url, json=None, data=None, **kwargs):
437
435
returned by the Cisco Spark API endpoint.
438
436
439
437
"""
440
- assert isinstance (url , basestring )
438
+ check_type (url , basestring , may_be_none = False )
441
439
442
440
# Expected response code
443
441
erc = kwargs .pop ('erc' , EXPECTED_RESPONSE_CODE ['PUT' ])
@@ -460,7 +458,7 @@ def delete(self, url, **kwargs):
460
458
returned by the Cisco Spark API endpoint.
461
459
462
460
"""
463
- assert isinstance (url , basestring )
461
+ check_type (url , basestring , may_be_none = False )
464
462
465
463
# Expected response code
466
464
erc = kwargs .pop ('erc' , EXPECTED_RESPONSE_CODE ['DELETE' ])
0 commit comments