-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy path_httprequests.py
More file actions
317 lines (277 loc) · 10.7 KB
/
Copy path_httprequests.py
File metadata and controls
317 lines (277 loc) · 10.7 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
from __future__ import annotations
import json
import logging
import random
from typing import (
TYPE_CHECKING,
Any,
Callable,
List,
Mapping,
Optional,
Sequence,
Union,
)
import requests
from requests.packages import urllib3
from tenacity import (
Retrying,
retry_if_exception,
stop_after_attempt,
wait_exponential,
)
from feldera.rest.config import Config
from feldera.rest.errors import (
FelderaAPIError,
FelderaCommunicationError,
FelderaTimeoutError,
)
if TYPE_CHECKING:
from tenacity import RetryCallState
def json_serialize(body: Any) -> str:
# serialize as string if this object cannot be serialized (e.g. UUID)
return json.dumps(body, default=str) if body else "" if body == "" else "null"
def _is_502(exc: BaseException) -> bool:
return isinstance(exc, FelderaAPIError) and exc.status_code == 502
_SENSITIVE_HEADERS = {"authorization", "cookie", "proxy-authorization", "x-api-key"}
def _redact_headers(headers: dict) -> dict:
return {
key: "[REDACTED]" if key.lower() in _SENSITIVE_HEADERS else value
for key, value in headers.items()
}
class HttpRequests:
def __init__(self, config: Config) -> None:
self.config = config
self.headers = {"User-Agent": "feldera-python-sdk/v1"}
self.requests_verify = config.requests_verify
if isinstance(self.requests_verify, bool) and not self.requests_verify:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
if self.config.api_key:
self.headers["Authorization"] = f"Bearer {self.config.api_key}"
def _check_cluster_health(self) -> bool:
"""Check `/cluster_healthz`; return True iff `all_healthy` is reported."""
try:
health_path = (
self.config.url + "/" + self.config.version + "/cluster_healthz"
)
response = requests.get(
health_path,
timeout=(self.config.connection_timeout, self.config.timeout),
headers=self.headers,
verify=self.requests_verify,
)
if response.status_code == 200:
return bool(response.json().get("all_healthy", False))
logging.warning(
"Health check returned status %d; instance may be upgrading",
response.status_code,
)
return False
except Exception as e:
logging.error("Health check failed: %s", e)
return False
def _is_retryable(self, exc: BaseException) -> bool:
"""Define which exceptions are worth retrying."""
if isinstance(exc, requests.exceptions.Timeout):
return True
if isinstance(exc, FelderaAPIError):
return exc.status_code in self.config.retry_config.retryable_status_codes
return False
def _custom_wait(self, retry_state: "RetryCallState") -> float:
"""
Compute the wait between retries. Branches by exception type:
- `Retry-After` header (if any) wins, capped at `max_backoff`.
- 502: probe `/cluster_healthz`. If the cluster is healthy the 502
is treated as spurious — return 0 so the retry runs immediately.
Otherwise return the configured `unhealthy_backoff` (a flat wait
while an upgrade or restart completes).
- Everything else: exponential backoff plus optional jitter.
"""
cfg = self.config.retry_config
exc = retry_state.outcome.exception() if retry_state.outcome else None
retry_after = getattr(exc, "retry_after", None)
if retry_after is not None:
return min(float(retry_after), cfg.max_backoff)
if _is_502(exc):
if self._check_cluster_health():
logging.info("Cluster healthy — treating 502 as spurious")
return 0.0
logging.info(
"Cluster unhealthy; backing off %.1fs before retrying 502",
cfg.unhealthy_backoff,
)
return cfg.unhealthy_backoff
backoff = wait_exponential(
multiplier=cfg.initial_backoff,
exp_base=cfg.multiplier,
max=cfg.max_backoff,
)(retry_state)
if cfg.jitter > 0:
backoff += random.uniform(0, cfg.jitter)
return backoff
def _do_single_request(
self,
http_method: Callable,
request_path: str,
data: Any,
params: Optional[Mapping[str, Any]],
stream: bool,
) -> Any:
response = http_method(
request_path,
data=data,
timeout=(self.config.connection_timeout, self.config.timeout),
headers=self.headers,
params=params,
stream=stream,
verify=self.requests_verify,
)
resp = self.__validate(response, stream=stream)
logging.debug("got response: %s", str(resp))
return resp
def send_request(
self,
http_method: Callable,
path: str,
body: Optional[
Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str]
] = None,
content_type: str = "application/json",
params: Optional[Mapping[str, Any]] = None,
stream: bool = False,
serialize: bool = True,
) -> Any:
"""
:param http_method: The HTTP method to use. Takes the equivalent `requests.*` module. (Example: `requests.get`)
:param path: The path to send the request to.
:param body: The HTTP request body.
:param content_type: The value for `Content-Type` HTTP header. "application/json" by default.
:param params: The query parameters part of this request.
:param stream: True if the response is expected to be a HTTP stream.
:param serialize: True if the body needs to be serialized to JSON.
Send an HTTP request, retrying transient failures per the client's
`RetryConfig`.
Retry policy:
- Status codes in `retry_config.retryable_status_codes` (default
408, 429, 502, 503, 504) and connection/read timeouts retry.
- 502 probes `/cluster_healthz` to distinguish a spurious gateway
error (cluster healthy → retry immediately) from a real outage
(cluster unhealthy → wait `unhealthy_backoff` seconds before
retrying).
- Other retryable failures use exponential backoff with optional
jitter; a server-supplied `Retry-After` header overrides it
(capped at `max_backoff`).
- All other errors are raised immediately.
"""
self.headers["Content-Type"] = content_type
request_path = self.config.url + "/" + self.config.version + path
# Serialize the body once, not per retry. None / bytes / `serialize=False`
# all pass through unchanged.
if body is None or isinstance(body, bytes) or not serialize:
data = body
else:
data = json_serialize(body)
logging.debug(
"sending %s request to: %s with headers: %s, and params: %s",
http_method.__name__,
request_path,
_redact_headers(self.headers),
str(params),
)
cfg = self.config.retry_config
retryer = Retrying(
retry=retry_if_exception(self._is_retryable),
wait=self._custom_wait,
stop=stop_after_attempt(cfg.max_retries + 1),
reraise=True,
)
try:
for attempt in retryer:
with attempt:
return self._do_single_request(
http_method, request_path, data, params, stream
)
except requests.exceptions.Timeout as err:
raise FelderaTimeoutError(str(err)) from err
except requests.exceptions.ConnectionError as err:
raise FelderaCommunicationError(str(err)) from err
def get(
self,
path: str,
params: Optional[Mapping[str, Any]] = None,
stream: bool = False,
) -> Any:
return self.send_request(requests.get, path, params=params, stream=stream)
def post(
self,
path: str,
body: Optional[
Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str]
] = None,
content_type: str = "application/json",
params: Optional[Mapping[str, Any]] = None,
stream: bool = False,
serialize: bool = True,
) -> Any:
return self.send_request(
requests.post,
path,
body,
content_type,
params,
stream=stream,
serialize=serialize,
)
def patch(
self,
path: str,
body: Optional[
Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str]
] = None,
content_type: str = "application/json",
params: Optional[Mapping[str, Any]] = None,
) -> Any:
return self.send_request(requests.patch, path, body, content_type, params)
def put(
self,
path: str,
body: Optional[
Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str]
] = None,
content_type: str = "application/json",
params: Optional[Mapping[str, Any]] = None,
) -> Any:
return self.send_request(requests.put, path, body, content_type, params)
def delete(
self,
path: str,
body: Optional[
Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str]]
] = None,
params: Optional[Mapping[str, Any]] = None,
) -> Any:
return self.send_request(requests.delete, path, body, params=params)
@staticmethod
def __to_json(request: requests.Response) -> Any:
if request.content == b"":
return request
return request.json()
@staticmethod
def __validate(request: requests.Response, stream=False) -> Any:
try:
request.raise_for_status()
if request is None:
# This shouldn't ever be the case, but we've seen it happen
return FelderaCommunicationError(
"Failed to Communicate with Feldera Received None as Response",
)
if stream:
return request
if request.headers.get("content-type") == "text/plain":
return request.text
elif request.headers.get("content-type") == "application/octet-stream":
return request.content
resp = HttpRequests.__to_json(request)
return resp
except requests.exceptions.HTTPError as err:
raise FelderaAPIError(str(err), request) from err