-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- default_stop_condition stops retrying after 3 attempts.
- default_wait_condition waits 1 second between attempts. - If no stop_condition or wait_condition is provided, the defaults will be used.
- Loading branch information
1 parent
400ea2f
commit 5505325
Showing
7 changed files
with
74 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import requests | ||
from retry import Retry, stop_after_attempt, wait_exponential | ||
|
||
# Define a function to make an HTTP request | ||
@Retry( | ||
# stop_condition=stop_after_attempt(5), # Retry up to 5 times | ||
# wait_condition=wait_exponential(multiplier=1, min_wait=1, max_wait=10), # Exponential backoff | ||
retry_on_exceptions=(requests.RequestException,), # Retry on any requests exception | ||
retry_on_result=lambda result: result.status_code != 200 # Retry if the status code is not 200 | ||
) | ||
def fetch_data_from_api(url): | ||
print(f"Trying to fetch data from {url}") | ||
response = requests.get(url) | ||
response.raise_for_status() # Raise an HTTPError on bad status | ||
return response | ||
|
||
# Use the function with a simulated unreliable endpoint | ||
try: | ||
# Simulating a service that returns 500 Internal Server Error 50% of the time | ||
data = fetch_data_from_api("https://httpbin.org/status/500") | ||
print("Data fetched successfully:", data) | ||
except Exception as e: | ||
print(f"Failed to fetch data after retries. Error: {e}") | ||
|
||
# Use the function with a simulated successful endpoint | ||
try: | ||
# Simulating a service that returns 200 OK | ||
data = fetch_data_from_api("https://httpbin.org/status/200") | ||
print("Data fetched successfully:", data) | ||
except Exception as e: | ||
print(f"Failed to fetch data after retries. Error: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters