Skip to content

Commit c921fb5

Browse files
committed
Merge branch 'v1.1' into dev
2 parents d65a46e + d1699f1 commit c921fb5

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

simvue/api.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import requests
1616
from tenacity import (
1717
retry,
18+
retry_if_exception,
1819
stop_after_attempt,
1920
wait_exponential,
20-
retry_if_exception_type,
2121
)
2222
from .utilities import parse_validation_response
2323

@@ -26,6 +26,14 @@
2626
RETRY_MIN = 4
2727
RETRY_MAX = 10
2828
RETRY_STOP = 5
29+
RETRY_STATUS_CODES = (
30+
http.HTTPStatus.BAD_REQUEST,
31+
http.HTTPStatus.SERVICE_UNAVAILABLE,
32+
http.HTTPStatus.GATEWAY_TIMEOUT,
33+
http.HTTPStatus.REQUEST_TIMEOUT,
34+
http.HTTPStatus.TOO_EARLY,
35+
)
36+
RETRY_EXCEPTION_TYPES = (RuntimeError, requests.exceptions.ConnectionError)
2937

3038

3139
def set_json_header(headers: dict[str, str]) -> dict[str, str]:
@@ -38,10 +46,18 @@ def set_json_header(headers: dict[str, str]) -> dict[str, str]:
3846
return headers
3947

4048

49+
def is_retryable_exception(exception: Exception) -> bool:
50+
"""Returns if the given exception should lead to a retry being called"""
51+
if isinstance(exception, requests.HTTPError):
52+
return exception.status_code in RETRY_STATUS_CODES
53+
54+
return isinstance(exception, RETRY_EXCEPTION_TYPES)
55+
56+
4157
@retry(
4258
wait=wait_exponential(multiplier=RETRY_MULTIPLIER, min=RETRY_MIN, max=RETRY_MAX),
4359
stop=stop_after_attempt(RETRY_STOP),
44-
retry=retry_if_exception_type(RuntimeError),
60+
retry=retry_if_exception(is_retryable_exception),
4561
reraise=True,
4662
)
4763
def post(
@@ -104,7 +120,7 @@ def post(
104120

105121
@retry(
106122
wait=wait_exponential(multiplier=RETRY_MULTIPLIER, min=RETRY_MIN, max=RETRY_MAX),
107-
retry=retry_if_exception_type(RuntimeError),
123+
retry=retry_if_exception(is_retryable_exception),
108124
stop=stop_after_attempt(RETRY_STOP),
109125
reraise=True,
110126
)
@@ -127,7 +143,7 @@ def put(
127143
data to put
128144
is_json : bool, optional
129145
send as JSON string, by default True
130-
timeout : _type_, optional
146+
timeout : int, optional
131147
timeout of request, by default DEFAULT_API_TIMEOUT
132148
133149
Returns
@@ -148,6 +164,12 @@ def put(
148164
return response
149165

150166

167+
@retry(
168+
wait=wait_exponential(multiplier=RETRY_MULTIPLIER, min=RETRY_MIN, max=RETRY_MAX),
169+
retry=retry_if_exception(is_retryable_exception),
170+
stop=stop_after_attempt(RETRY_STOP),
171+
reraise=True,
172+
)
151173
def get(
152174
url: str, headers: dict[str, str], timeout: int = DEFAULT_API_TIMEOUT
153175
) -> requests.Response:
@@ -159,7 +181,7 @@ def get(
159181
URL to put to
160182
headers : dict[str, str]
161183
headers for the post request
162-
timeout : _type_, optional
184+
timeout : int, optional
163185
timeout of request, by default DEFAULT_API_TIMEOUT
164186
165187
Returns

0 commit comments

Comments
 (0)