-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.py
More file actions
514 lines (437 loc) · 15.1 KB
/
Copy pathhttp.py
File metadata and controls
514 lines (437 loc) · 15.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
"""HTTP transport layer for the cloudlayer.io Python SDK.
Handles authentication, retries, timeouts, error mapping, and response parsing.
"""
from __future__ import annotations
import asyncio
import contextlib
import json
import random
import re
import time
from typing import Any
import httpx
from cloudlayerio.errors import (
CloudLayerApiError,
CloudLayerAuthError,
CloudLayerNetworkError,
CloudLayerRateLimitError,
CloudLayerTimeoutError,
)
from cloudlayerio.types.response import CloudLayerResponseHeaders
# Status codes that trigger retry (when retryable=True)
_RETRYABLE_STATUS_CODES = {429, 500, 502, 503, 504}
# Status codes that are never retried (client errors)
_NON_RETRYABLE_STATUS_CODES = {400, 401, 403, 404, 422}
def _parse_cl_headers(headers: httpx.Headers) -> CloudLayerResponseHeaders:
"""Parse CloudLayer custom headers from an HTTP response."""
return CloudLayerResponseHeaders.from_headers(dict(headers))
def _parse_filename(headers: httpx.Headers) -> str | None:
"""Extract filename from Content-Disposition header."""
cd = headers.get("content-disposition")
if not cd:
return None
match = re.search(r'filename[*]?=["\']?([^"\';\s]+)', cd)
return match.group(1) if match else None
def _is_json_content_type(content_type: str | None) -> bool:
"""Check if the content type indicates JSON."""
if not content_type:
return False
ct = content_type.lower()
return "application/json" in ct or "text/json" in ct
def _parse_error_body(response: httpx.Response) -> Any:
"""Parse the error response body. Try JSON first, fall back to text."""
try:
return response.json()
except (json.JSONDecodeError, ValueError):
text = response.text
return text if text else None
def _make_error_message(status: int, status_text: str, body: Any) -> str:
"""Build a human-readable error message from response details."""
if isinstance(body, dict) and "message" in body:
return str(body["message"])
return f"API error: {status} {status_text}"
def _calculate_backoff(attempt: int) -> float:
"""Calculate retry delay with exponential backoff and jitter.
Base delays: 1s, 2s, 4s + 0-500ms random jitter.
"""
base = float(min(2**attempt, 4))
jitter = random.random() * 0.5
return base + jitter
def _map_error(
response: httpx.Response,
request_path: str,
request_method: str,
) -> CloudLayerApiError:
"""Map an HTTP error response to the appropriate exception type."""
status = response.status_code
status_text = response.reason_phrase or ""
body = _parse_error_body(response)
if status in (401, 403):
return CloudLayerAuthError(
status=status,
status_text=status_text,
body=body,
request_path=request_path,
request_method=request_method,
)
if status == 429:
retry_after: int | None = None
ra_header = response.headers.get("retry-after")
if ra_header:
with contextlib.suppress(ValueError, TypeError):
retry_after = int(ra_header)
return CloudLayerRateLimitError(
status=status,
status_text=status_text,
body=body,
request_path=request_path,
request_method=request_method,
retry_after=retry_after,
)
message = _make_error_message(status, status_text, body)
return CloudLayerApiError(
message,
status=status,
status_text=status_text,
body=body,
request_path=request_path,
request_method=request_method,
)
class HttpTransport:
"""Synchronous HTTP transport for the CloudLayer API."""
def __init__(
self,
*,
api_key: str,
base_url: str,
api_version: str,
timeout: int,
max_retries: int,
headers: dict[str, str] | None = None,
) -> None:
self._api_key = api_key
self._base_url = base_url.rstrip("/")
self._api_version = api_version
self._timeout = timeout
self._max_retries = max_retries
default_headers = {"X-API-Key": api_key}
if headers:
default_headers.update(headers)
self._client = httpx.Client(headers=default_headers)
def close(self) -> None:
"""Close the underlying HTTP client."""
self._client.close()
def __enter__(self) -> HttpTransport:
return self
def __exit__(self, *args: Any) -> None:
self.close()
def _build_url(self, path: str, *, absolute_path: bool = False) -> str:
"""Build the full URL for a request."""
if absolute_path:
return f"{self._base_url}{path}"
return f"{self._base_url}/{self._api_version}{path}"
def request(
self,
method: str,
path: str,
*,
json_body: Any = None,
files: Any = None,
timeout: int | None = None,
params: dict[str, Any] | None = None,
retryable: bool = False,
absolute_path: bool = False,
) -> httpx.Response:
"""Make an HTTP request with retry logic and error handling."""
url = self._build_url(path, absolute_path=absolute_path)
timeout_s = (timeout or self._timeout) / 1000.0
max_attempts = (self._max_retries + 1) if retryable else 1
# Filter None values from query params
clean_params: dict[str, Any] | None = None
if params:
clean_params = {k: v for k, v in params.items() if v is not None}
last_error: Exception | None = None
for attempt in range(max_attempts):
try:
request_kwargs: dict[str, Any] = {
"method": method,
"url": url,
"timeout": timeout_s,
}
if clean_params:
request_kwargs["params"] = clean_params
if files is not None:
request_kwargs["files"] = files
elif json_body is not None:
request_kwargs["json"] = json_body
response = self._client.request(**request_kwargs)
except httpx.TimeoutException as e:
raise CloudLayerTimeoutError(
timeout=timeout or self._timeout,
request_path=path,
request_method=method,
) from e
except (httpx.ConnectError, httpx.NetworkError) as e:
raise CloudLayerNetworkError(
f"Network error: {e}",
request_path=path,
request_method=method,
) from e
# Success
if response.is_success:
return response
# Map to typed error
error = _map_error(response, path, method)
# Don't retry client errors or non-retryable requests
if response.status_code in _NON_RETRYABLE_STATUS_CODES or not retryable:
raise error
# Retryable error — check if we have attempts left
if attempt < max_attempts - 1:
if isinstance(error, CloudLayerRateLimitError) and error.retry_after:
delay = float(error.retry_after)
else:
delay = _calculate_backoff(attempt)
time.sleep(delay)
last_error = error
else:
raise error
# Should not reach here, but satisfy type checker
if last_error: # pragma: no cover
raise last_error
raise RuntimeError("Unexpected state in retry loop") # pragma: no cover
def get(
self,
path: str,
*,
params: dict[str, Any] | None = None,
retryable: bool = False,
absolute_path: bool = False,
timeout: int | None = None,
) -> httpx.Response:
"""Make a GET request."""
return self.request(
"GET",
path,
params=params,
retryable=retryable,
absolute_path=absolute_path,
timeout=timeout,
)
def post(
self,
path: str,
body: Any = None,
*,
retryable: bool = False,
absolute_path: bool = False,
timeout: int | None = None,
) -> httpx.Response:
"""Make a POST request with JSON body."""
return self.request(
"POST",
path,
json_body=body,
retryable=retryable,
absolute_path=absolute_path,
timeout=timeout,
)
def post_multipart(
self,
path: str,
files: Any,
*,
retryable: bool = False,
absolute_path: bool = False,
timeout: int | None = None,
) -> httpx.Response:
"""Make a POST request with multipart form data."""
return self.request(
"POST",
path,
files=files,
retryable=retryable,
absolute_path=absolute_path,
timeout=timeout,
)
def delete(
self,
path: str,
*,
retryable: bool = False,
absolute_path: bool = False,
timeout: int | None = None,
) -> httpx.Response:
"""Make a DELETE request."""
return self.request(
"DELETE",
path,
retryable=retryable,
absolute_path=absolute_path,
timeout=timeout,
)
class AsyncHttpTransport:
"""Asynchronous HTTP transport for the CloudLayer API."""
def __init__(
self,
*,
api_key: str,
base_url: str,
api_version: str,
timeout: int,
max_retries: int,
headers: dict[str, str] | None = None,
) -> None:
self._api_key = api_key
self._base_url = base_url.rstrip("/")
self._api_version = api_version
self._timeout = timeout
self._max_retries = max_retries
default_headers = {"X-API-Key": api_key}
if headers:
default_headers.update(headers)
self._client = httpx.AsyncClient(headers=default_headers)
async def close(self) -> None:
"""Close the underlying async HTTP client."""
await self._client.aclose()
async def __aenter__(self) -> AsyncHttpTransport:
return self
async def __aexit__(self, *args: Any) -> None:
await self.close()
def _build_url(self, path: str, *, absolute_path: bool = False) -> str:
"""Build the full URL for a request."""
if absolute_path:
return f"{self._base_url}{path}"
return f"{self._base_url}/{self._api_version}{path}"
async def request(
self,
method: str,
path: str,
*,
json_body: Any = None,
files: Any = None,
timeout: int | None = None,
params: dict[str, Any] | None = None,
retryable: bool = False,
absolute_path: bool = False,
) -> httpx.Response:
"""Make an async HTTP request with retry logic and error handling."""
url = self._build_url(path, absolute_path=absolute_path)
timeout_s = (timeout or self._timeout) / 1000.0
max_attempts = (self._max_retries + 1) if retryable else 1
clean_params: dict[str, Any] | None = None
if params:
clean_params = {k: v for k, v in params.items() if v is not None}
last_error: Exception | None = None
for attempt in range(max_attempts):
try:
request_kwargs: dict[str, Any] = {
"method": method,
"url": url,
"timeout": timeout_s,
}
if clean_params:
request_kwargs["params"] = clean_params
if files is not None:
request_kwargs["files"] = files
elif json_body is not None:
request_kwargs["json"] = json_body
response = await self._client.request(**request_kwargs)
except httpx.TimeoutException as e:
raise CloudLayerTimeoutError(
timeout=timeout or self._timeout,
request_path=path,
request_method=method,
) from e
except (httpx.ConnectError, httpx.NetworkError) as e:
raise CloudLayerNetworkError(
f"Network error: {e}",
request_path=path,
request_method=method,
) from e
if response.is_success:
return response
error = _map_error(response, path, method)
if response.status_code in _NON_RETRYABLE_STATUS_CODES or not retryable:
raise error
if attempt < max_attempts - 1:
if isinstance(error, CloudLayerRateLimitError) and error.retry_after:
delay = float(error.retry_after)
else:
delay = _calculate_backoff(attempt)
await asyncio.sleep(delay)
last_error = error
else:
raise error
if last_error: # pragma: no cover
raise last_error
raise RuntimeError("Unexpected state in retry loop") # pragma: no cover
async def get(
self,
path: str,
*,
params: dict[str, Any] | None = None,
retryable: bool = False,
absolute_path: bool = False,
timeout: int | None = None,
) -> httpx.Response:
"""Make an async GET request."""
return await self.request(
"GET",
path,
params=params,
retryable=retryable,
absolute_path=absolute_path,
timeout=timeout,
)
async def post(
self,
path: str,
body: Any = None,
*,
retryable: bool = False,
absolute_path: bool = False,
timeout: int | None = None,
) -> httpx.Response:
"""Make an async POST request with JSON body."""
return await self.request(
"POST",
path,
json_body=body,
retryable=retryable,
absolute_path=absolute_path,
timeout=timeout,
)
async def post_multipart(
self,
path: str,
files: Any,
*,
retryable: bool = False,
absolute_path: bool = False,
timeout: int | None = None,
) -> httpx.Response:
"""Make an async POST request with multipart form data."""
return await self.request(
"POST",
path,
files=files,
retryable=retryable,
absolute_path=absolute_path,
timeout=timeout,
)
async def delete(
self,
path: str,
*,
retryable: bool = False,
absolute_path: bool = False,
timeout: int | None = None,
) -> httpx.Response:
"""Make an async DELETE request."""
return await self.request(
"DELETE",
path,
retryable=retryable,
absolute_path=absolute_path,
timeout=timeout,
)