@@ -21,8 +21,11 @@ from {{ package }}.exceptions import (
21
21
logger = logging.getLogger(__name__)
22
22
23
23
24
+ RETRY_AFTER_STATUS_CODES = frozenset([413, 429, 500, 501, 502, 503, 504, 505, 506, 507, 509, 510, 511])
25
+
26
+
24
27
class ClientRetry(urllib3.util.Retry):
25
- RETRY_AFTER_STATUS_CODES = frozenset([413, 429, 500, 501, 502, 503, 504, 505, 506, 507, 509, 510, 511])
28
+ RETRY_AFTER_STATUS_CODES = RETRY_AFTER_STATUS_CODES
26
29
DEFAULT_ALLOWED_METHODS = frozenset(["GET", "PUT", "DELETE", "POST", "PATCH"])
27
30
28
31
def get_retry_after(self, response):
@@ -235,6 +238,17 @@ class AsyncRESTClientObject:
235
238
if configuration.proxy:
236
239
proxy = aiosonic.Proxy(configuration.proxy, configuration.proxy_headers)
237
240
self._client = aiosonic.HTTPClient(proxy=proxy)
241
+ self._configuration = configuration
242
+
243
+ def _retry(self, response, counter):
244
+ if (not self._configuration.enable_retry
245
+ or counter >= self._configuration.max_retries
246
+ or response.status_code not in RETRY_AFTER_STATUS_CODES):
247
+ return 0
248
+ retry_after = response.headers.get("X-Ratelimit-Reset")
249
+ if retry_after is None:
250
+ return self._configuration.retry_backoff_factor * (2 ** (counter))
251
+ return int(retry_after)
238
252
239
253
async def request(
240
254
self,
@@ -285,9 +299,23 @@ class AsyncRESTClientObject:
285
299
request_body = compress.compress(request_body.encode("utf-8")) + compress.flush()
286
300
elif headers.get("Content-Encoding") == "deflate":
287
301
request_body = zlib.compress(request_body.encode("utf-8"))
288
- response = await self._client.request(
289
- url, method, headers, query_params, request_body, timeouts=request_timeout
290
- )
302
+ elif headers.get("Content-Encoding") == "zstd1":
303
+ import zstandard as zstd
304
+
305
+ compressor = zstd.ZstdCompressor()
306
+ request_body = compressor.compress(request_body.encode("utf-8"))
307
+ counter = 0
308
+ while True:
309
+ response = await self._client.request(
310
+ url, method, headers, query_params, request_body, timeouts=request_timeout
311
+ )
312
+ retry = self._retry(response, counter)
313
+ if not retry:
314
+ break
315
+ import asyncio
316
+
317
+ await asyncio.sleep(retry)
318
+ counter += 1
291
319
292
320
if not 200 <= response.status_code <= 299:
293
321
data = b""
0 commit comments