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