Improve error handling and retries

1. Use the actual response code from the server on error, or 504 on
timeouts instead of 500.
2. Retry GET requests 3 times and only error if the last fails
This commit is contained in:
Joshua Boniface 2024-01-29 09:35:14 -05:00
parent 9b51fe9f10
commit db4f0881a2
1 changed files with 26 additions and 9 deletions

View File

@ -140,15 +140,31 @@ def call_api(
# Determine the request type and hit the API # Determine the request type and hit the API
disable_warnings() disable_warnings()
try: try:
response = None
if operation == "get": if operation == "get":
response = requests.get( retry_on_code = [429, 500, 502, 503, 504]
uri, for i in range(3):
timeout=timeout, failed = False
headers=headers, try:
params=params, response = requests.get(
data=data, uri,
verify=config["verify_ssl"], timeout=timeout,
) headers=headers,
params=params,
data=data,
verify=config["verify_ssl"],
)
if response.status_code in retry_on_code:
failed = True
continue
except requests.exceptions.ConnectionError:
failed = True
pass
if failed:
error = f"Code {response.status_code}" if response else "Timeout"
raise requests.exceptions.ConnectionError(
f"Failed to connect after 3 tries ({error})"
)
if operation == "post": if operation == "post":
response = requests.post( response = requests.post(
uri, uri,
@ -189,7 +205,8 @@ def call_api(
) )
except Exception as e: except Exception as e:
message = "Failed to connect to the API: {}".format(e) message = "Failed to connect to the API: {}".format(e)
response = ErrorResponse({"message": message}, 500) code = response.status_code if response else 504
response = ErrorResponse({"message": message}, code)
# Display debug output # Display debug output
if config["debug"]: if config["debug"]: