Introduction
Your job is taking longer than expected to complete. When you review the code, the job appears to be stuck during an external API call step.
Instructions
The API endpoint is down, experiencing network issues or other external factors affecting responsiveness. In your code, implement an API call timeout threshold and wrap API calls in try/except blocks for robust error handling.
The following code demonstrates how to use a timeout and a try/except block to cover requests.Timeout
and other potential request exceptions.
try:
response = requests.put(url, json=data, timeout=60)
response.raise_for_status() # Raise an error for non-2xx responses
except requests.Timeout:
print("Request timed out")
except requests.RequestException as e:
print(f"Request failed: {e}")