Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def retry_request_till_success(
- request: requests.models.Request
- ) -> requests.models.Response:
- def wrapper(*args, **kwargs):
- retry_time_count = 1
- while True:
- retry_in_seconds = define_retry_delay_seconds(retry_time_count)
- try:
- if ( response := request(*args, **kwargs) ) == None: # doc.id#13
- p("There is no request to retry")
- break
- while True:
- retry_in_seconds = define_retry_delay_seconds(
- retry_time_count
- )
- if response.status_code == 200:
- break
- else:
- if (
- response := request(*args, **kwargs)
- ).status_code != 200:
- log_and_send_message_to_telegram(
- f"Request in {request.__name__} failed. "
- f"Status_code – {response.status_code} "
- f"Will retry again in {retry_in_seconds} seconds"
- )
- time.sleep(retry_in_seconds)
- retry_time_count += 1
- else:
- break
- return response
- except (
- requests.ConnectTimeout,
- requests.exceptions.ReadTimeout
- ) as e:
- error = get_caught_error()
- log_and_send_message_to_telegram(
- f"Request in {request.__name__} failed. "
- f"Timed out with error {error}"
- f"Will retry again in {retry_in_seconds} seconds"
- )
- time.sleep(retry_in_seconds)
- retry_time_count += 1
- except requests.exceptions.ConnectionError as e:
- p(
- f"Request in {request.__name__} failed. "
- "No connection to internet... "
- "Will retry again in 1 second"
- )
- time.sleep(1)
- return wrapper
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement