@@ -455,44 +455,53 @@ def source (api, parm={}, stream=False, callbacks={}, path="/source"):
455455 if c not in callbacks :
456456 callbacks [c ] = __callbacks [c ]
457457 # Attempt Request
458- try :
459- # Construct Request URL and Authorization
460- if service_org :
461- url = 'https://%s.%s%s/%s' % (service_org , service_url , path , api )
462- headers = __build_auth_header ()
463- else :
464- url = 'http://%s%s/%s' % (service_url , path , api )
465- # Perform Request
466- if not stream :
467- data = session .get (url , data = rqst , headers = headers , timeout = request_timeout )
468- else :
469- data = session .post (url , data = rqst , headers = headers , timeout = request_timeout , stream = True )
470- data .raise_for_status ()
471- # Parse Response
472- format = data .headers ['Content-Type' ]
473- if format == 'text/plain' :
474- rsps = __parse_json (data )
475- elif format == 'application/json' :
476- rsps = __parse_json (data )
477- elif format == 'application/octet-stream' :
478- rsps = __parse_native (data , callbacks )
479- else :
480- raise FatalError ('unsupported content type: %s' % (format ))
481- except requests .exceptions .SSLError as e :
482- raise FatalError ("Unable to verify SSL certificate: {}" .format (e ))
483- except requests .ConnectionError as e :
484- raise FatalError ("Connection error to endpoint {}" .format (url ))
485- except requests .Timeout as e :
486- raise TransientError ("Timed-out waiting for response from endpoint {}" .format (url ))
487- except requests .exceptions .ChunkedEncodingError as e :
488- raise RuntimeError ("Unexpected termination of response from endpoint {}" .format (url ))
489- except requests .HTTPError as e :
490- if e .response .status_code == 503 :
491- raise TransientError ("Server experiencing heavy load, stalling on request to {}" .format (url ))
492- else :
493- raise FatalError ("HTTP error {} from endpoint {}" .format (e .response .status_code , url ))
494- except :
495- raise
458+ complete = False
459+ attempts = 3
460+ while not complete and attempts > 0 :
461+ attempts -= 1
462+ try :
463+ # Construct Request URL and Authorization
464+ if service_org :
465+ url = 'https://%s.%s%s/%s' % (service_org , service_url , path , api )
466+ headers = __build_auth_header ()
467+ else :
468+ url = 'http://%s%s/%s' % (service_url , path , api )
469+ # Perform Request
470+ if not stream :
471+ data = session .get (url , data = rqst , headers = headers , timeout = request_timeout )
472+ else :
473+ data = session .post (url , data = rqst , headers = headers , timeout = request_timeout , stream = True )
474+ data .raise_for_status ()
475+ # Parse Response
476+ format = data .headers ['Content-Type' ]
477+ if format == 'text/plain' :
478+ rsps = __parse_json (data )
479+ elif format == 'application/json' :
480+ rsps = __parse_json (data )
481+ elif format == 'application/octet-stream' :
482+ rsps = __parse_native (data , callbacks )
483+ else :
484+ raise FatalError ('unsupported content type: %s' % (format ))
485+ # Success
486+ complete = True
487+ except requests .exceptions .SSLError as e :
488+ logger .error ("Unable to verify SSL certificate: {} ...retrying request" .format (e ))
489+ except requests .ConnectionError as e :
490+ logger .error ("Connection error to endpoint {} ...retrying request" .format (url ))
491+ except requests .Timeout as e :
492+ logger .error ("Timed-out waiting for response from endpoint {} ...retrying request" .format (url ))
493+ except requests .exceptions .ChunkedEncodingError as e :
494+ logger .error ("Unexpected termination of response from endpoint {} ...retrying request" .format (url ))
495+ except requests .HTTPError as e :
496+ if e .response .status_code == 503 :
497+ raise TransientError ("Server experiencing heavy load, stalling on request to {}" .format (url ))
498+ else :
499+ raise FatalError ("HTTP error {} from endpoint {}" .format (e .response .status_code , url ))
500+ except :
501+ raise
502+ # Check Success
503+ if not complete :
504+ raise FatalError ("Unable to complete request due to errors" )
496505 # Return Response
497506 return rsps
498507
0 commit comments