-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest.py
More file actions
333 lines (288 loc) · 9.1 KB
/
request.py
File metadata and controls
333 lines (288 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""
Simvue API Connection
=====================
Provides methods for interacting with a Simvue server which include retry
policies. In cases where JSON is the expected form the data is firstly converted
to a JSON string
"""
import copy
import json as json_module
import typing
import logging
import http
import requests
from tenacity import (
retry,
retry_if_exception,
stop_after_attempt,
wait_exponential,
)
from simvue.utilities import parse_validation_response
DEFAULT_API_TIMEOUT = 10
RETRY_MULTIPLIER = 1
RETRY_MIN = 4
RETRY_MAX = 10
RETRY_STOP = 5
MAX_ENTRIES_PER_PAGE: int = 100
RETRY_STATUS_CODES = (
http.HTTPStatus.BAD_REQUEST,
http.HTTPStatus.SERVICE_UNAVAILABLE,
http.HTTPStatus.GATEWAY_TIMEOUT,
http.HTTPStatus.REQUEST_TIMEOUT,
http.HTTPStatus.TOO_EARLY,
)
RETRY_EXCEPTION_TYPES = (RuntimeError, requests.exceptions.ConnectionError)
def set_json_header(headers: dict[str, str]) -> dict[str, str]:
"""
Return a copy of the headers with Content-Type set to
application/json
"""
headers = copy.deepcopy(headers)
headers["Content-Type"] = "application/json"
return headers
def is_retryable_exception(exception: Exception) -> bool:
"""Returns if the given exception should lead to a retry being called"""
if isinstance(exception, requests.HTTPError):
return exception.response.status_code in RETRY_STATUS_CODES
return isinstance(exception, RETRY_EXCEPTION_TYPES)
@retry(
wait=wait_exponential(multiplier=RETRY_MULTIPLIER, min=RETRY_MIN, max=RETRY_MAX),
stop=stop_after_attempt(RETRY_STOP),
retry=retry_if_exception(is_retryable_exception),
reraise=True,
)
def post(
url: str,
headers: dict[str, str],
params: dict[str, str],
data: typing.Any,
is_json: bool = True,
timeout: int | None = None,
files: dict[str, typing.Any] | None = None,
) -> requests.Response:
"""HTTP POST with retries
Parameters
----------
url : str
URL to post to
headers : dict[str, str]
headers for the post request
params : dict[str, str]
query parameters for the post request
data : dict[str, typing.Any]
data to post
is_json : bool, optional
send as JSON string, by default True
Returns
-------
requests.Response
response from post to server
"""
if is_json:
data_sent: str | dict[str, typing.Any] = json_module.dumps(data)
headers = set_json_header(headers)
else:
data_sent = data
response = requests.post(
url,
headers=headers,
params=params,
data=data_sent,
timeout=timeout,
files=files,
)
if response.status_code == http.HTTPStatus.UNPROCESSABLE_ENTITY:
_parsed_response = parse_validation_response(response.json())
raise ValueError(
f"Validation error for '{url}' [{response.status_code}]:\n{_parsed_response}"
)
return response
@retry(
wait=wait_exponential(multiplier=RETRY_MULTIPLIER, min=RETRY_MIN, max=RETRY_MAX),
retry=retry_if_exception(is_retryable_exception),
stop=stop_after_attempt(RETRY_STOP),
reraise=True,
)
def put(
url: str,
headers: dict[str, str],
data: dict[str, typing.Any] | None = None,
json: dict[str, typing.Any] | None = None,
is_json: bool = True,
timeout: int = DEFAULT_API_TIMEOUT,
) -> requests.Response:
"""HTTP PUT with retries
Parameters
----------
url : str
URL to put to
headers : dict[str, str]
headers for the post request
data : dict[str, typing.Any]
data to put
json : dict | None
json data to send
is_json : bool, optional
send as JSON string, by default True
timeout : int, optional
timeout of request, by default DEFAULT_API_TIMEOUT
Returns
-------
requests.Response
response from executing PUT
"""
if is_json and data:
data_sent: str | dict[str, typing.Any] = json_module.dumps(data)
headers = set_json_header(headers)
else:
data_sent = data
logging.debug(f"PUT: {url}\n\tdata={data_sent}\n\tjson={json}")
return requests.put(
url, headers=headers, data=data_sent, timeout=timeout, json=json
)
@retry(
wait=wait_exponential(multiplier=RETRY_MULTIPLIER, min=RETRY_MIN, max=RETRY_MAX),
retry=retry_if_exception(is_retryable_exception),
stop=stop_after_attempt(RETRY_STOP),
reraise=True,
)
def get(
url: str,
headers: dict[str, str] | None = None,
params: dict[str, str | int | float | None] | None = None,
timeout: int = DEFAULT_API_TIMEOUT,
json: dict[str, typing.Any] | None = None,
) -> requests.Response:
"""HTTP GET
Parameters
----------
url : str
URL to put to
headers : dict[str, str]
headers for the post request
timeout : int, optional
timeout of request, by default DEFAULT_API_TIMEOUT
json : dict[str, Any] | None, optional
any json to send in request
Returns
-------
requests.Response
response from executing GET
"""
logging.debug(f"GET: {url}\n\tparams={params}")
return requests.get(url, headers=headers, timeout=timeout, params=params, json=json)
@retry(
wait=wait_exponential(multiplier=RETRY_MULTIPLIER, min=RETRY_MIN, max=RETRY_MAX),
retry=retry_if_exception(is_retryable_exception),
stop=stop_after_attempt(RETRY_STOP),
reraise=True,
)
def delete(
url: str,
headers: dict[str, str],
timeout: int = DEFAULT_API_TIMEOUT,
params: dict[str, typing.Any] | None = None,
) -> requests.Response:
"""HTTP DELETE
Parameters
----------
url : str
URL to put to
headers : dict[str, str]
headers for the post request
timeout : int, optional
timeout of request, by default DEFAULT_API_TIMEOUT
params : dict, optional
parameters for deletion
Returns
-------
requests.Response
response from executing DELETE
"""
logging.debug(f"DELETE: {url}\n\tparams={params}")
return requests.delete(url, headers=headers, timeout=timeout, params=params)
def get_json_from_response(
expected_status: list[int],
scenario: str,
response: requests.Response,
allow_parse_failure: bool = False,
expected_type: typing.Type[dict | list] = dict,
) -> dict | list:
try:
json_response = response.json()
json_response = json_response or ({} if expected_type is dict else [])
decode_error = ""
except json_module.JSONDecodeError as e:
json_response = {} if allow_parse_failure else None
decode_error = f"{e}"
error_str = f"{scenario} failed for url '{response.url}'"
details: str | None = None
if (_status_code := response.status_code) in expected_status:
if not isinstance(json_response, expected_type):
details = f"expected type '{expected_type.__name__}' but got '{type(json_response).__name__}'"
elif json_response is not None:
return json_response
else:
details = f"could not request JSON response: {decode_error}"
elif isinstance(json_response, dict):
error_str += f" with status {_status_code}"
details = (json_response or {}).get("detail")
try:
txt_response = response.text
except UnicodeDecodeError:
txt_response = None
if details:
error_str += f": {details}"
elif txt_response:
error_str += f": {txt_response}"
raise RuntimeError(error_str)
def get_paginated(
url: str,
headers: dict[str, str] | None = None,
timeout: int = DEFAULT_API_TIMEOUT,
json: dict[str, typing.Any] | None = None,
offset: int | None = None,
count: int | None = None,
**params,
) -> typing.Generator[requests.Response, None, None]:
"""Paginate results of a server query.
Parameters
----------
url : str
URL to put to
headers : dict[str, str]
headers for the post request
timeout : int, optional
timeout of request, by default DEFAULT_API_TIMEOUT
json : dict[str, Any] | None, optional
any json to send in request
Yield
-----
requests.Response
server response
"""
_offset: int = offset or 0
# Restrict the number of entries retrieved to be paginated,
# if the count requested is below page limit use this value
# else if undefined or greater than the page limit use the limit
_request_count: int = min(count or MAX_ENTRIES_PER_PAGE, MAX_ENTRIES_PER_PAGE)
try:
while (
_response := get(
url=url,
headers=headers,
params=(params or {}) | {"count": _request_count, "start": _offset},
timeout=timeout,
json=json,
)
).json():
yield _response
_offset += MAX_ENTRIES_PER_PAGE
if (count and _offset > count) or (
_response.json().get("count", 0) < _offset
):
break
except json_module.JSONDecodeError:
raise RuntimeError(
f"[{_response.status_code}] Failed to retrieve content from server: {_response.text}"
)