@@ -279,7 +279,8 @@ def _raise_on_unsuccessful_request(network_response: 'NetworkResponse', request:
279
279
context_info = response_json .get ('context_info' , None ),
280
280
network_response = network_response
281
281
)
282
- if request .expect_json_response and not is_json_response (network_response ):
282
+
283
+ if not Session ._is_json_response_if_expected (network_response , request ):
283
284
raise BoxAPIException (
284
285
status = network_response .status_code ,
285
286
headers = network_response .headers ,
@@ -289,6 +290,18 @@ def _raise_on_unsuccessful_request(network_response: 'NetworkResponse', request:
289
290
network_response = network_response ,
290
291
)
291
292
293
+ @staticmethod
294
+ def _is_json_response_if_expected (network_response : 'NetworkResponse' , request : '_BoxRequest' ) -> bool :
295
+ """
296
+ Validate that the response is json if the request expects json response.
297
+
298
+ :param network_response:
299
+ The network response which is being tested for success.
300
+ :param request:
301
+ The API request that could be unsuccessful.
302
+ """
303
+ return not request .expect_json_response or is_json_response (network_response )
304
+
292
305
def _prepare_and_send_request (
293
306
self ,
294
307
method : str ,
@@ -391,13 +404,15 @@ def _get_retry_request_callable(
391
404
Callable that, when called, will retry the request. Takes the same parameters as :meth:`_send_request`.
392
405
"""
393
406
# pylint:disable=unused-argument
394
- if network_response is None :
407
+ # pylint:disable=line-too-long
408
+ if network_response is None or (network_response .ok and request .method == 'GET' and not self ._is_json_response_if_expected (network_response , request )):
395
409
return partial (
396
410
self ._network_layer .retry_after ,
397
411
self .get_retry_after_time (attempt_number , None ),
398
412
self ._send_request ,
399
413
)
400
414
code = network_response .status_code
415
+
401
416
if (code in (202 , 429 ) or code >= 500 ) and code not in skip_retry_codes and not self ._is_server_auth_type (kwargs ):
402
417
return partial (
403
418
self ._network_layer .retry_after ,
@@ -541,6 +556,7 @@ def _get_retry_request_callable(
541
556
self ._renew_session (request .access_token )
542
557
request .auto_session_renewal = False
543
558
return self ._send_request
559
+
544
560
return super ()._get_retry_request_callable (
545
561
network_response ,
546
562
attempt_number ,
0 commit comments