10
10
import copy
11
11
import json
12
12
import typing
13
+ import http
13
14
14
15
import requests
15
16
from tenacity import (
16
17
retry ,
18
+ retry_if_exception ,
17
19
stop_after_attempt ,
18
20
wait_exponential ,
19
- retry_if_exception_type ,
20
21
)
21
22
from .utilities import parse_validation_response
22
23
25
26
RETRY_MIN = 4
26
27
RETRY_MAX = 10
27
28
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 )
28
37
29
38
30
39
def set_json_header (headers : dict [str , str ]) -> dict [str , str ]:
@@ -37,10 +46,18 @@ def set_json_header(headers: dict[str, str]) -> dict[str, str]:
37
46
return headers
38
47
39
48
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
+
40
57
@retry (
41
58
wait = wait_exponential (multiplier = RETRY_MULTIPLIER , min = RETRY_MIN , max = RETRY_MAX ),
42
59
stop = stop_after_attempt (RETRY_STOP ),
43
- retry = retry_if_exception_type ( RuntimeError ),
60
+ retry = retry_if_exception ( is_retryable_exception ),
44
61
reraise = True ,
45
62
)
46
63
def post (
@@ -96,7 +113,7 @@ def post(
96
113
97
114
@retry (
98
115
wait = wait_exponential (multiplier = RETRY_MULTIPLIER , min = RETRY_MIN , max = RETRY_MAX ),
99
- retry = retry_if_exception_type ( RuntimeError ),
116
+ retry = retry_if_exception ( is_retryable_exception ),
100
117
stop = stop_after_attempt (RETRY_STOP ),
101
118
reraise = True ,
102
119
)
@@ -119,7 +136,7 @@ def put(
119
136
data to put
120
137
is_json : bool, optional
121
138
send as JSON string, by default True
122
- timeout : _type_ , optional
139
+ timeout : int , optional
123
140
timeout of request, by default DEFAULT_API_TIMEOUT
124
141
125
142
Returns
@@ -140,6 +157,12 @@ def put(
140
157
return response
141
158
142
159
160
+ @retry (
161
+ wait = wait_exponential (multiplier = RETRY_MULTIPLIER , min = RETRY_MIN , max = RETRY_MAX ),
162
+ retry = retry_if_exception (is_retryable_exception ),
163
+ stop = stop_after_attempt (RETRY_STOP ),
164
+ reraise = True ,
165
+ )
143
166
def get (
144
167
url : str , headers : dict [str , str ], timeout : int = DEFAULT_API_TIMEOUT
145
168
) -> requests .Response :
@@ -151,7 +174,7 @@ def get(
151
174
URL to put to
152
175
headers : dict[str, str]
153
176
headers for the post request
154
- timeout : _type_ , optional
177
+ timeout : int , optional
155
178
timeout of request, by default DEFAULT_API_TIMEOUT
156
179
157
180
Returns
0 commit comments