Skip to content

Commit ca1fff0

Browse files
committed
Check allowed methods, remove 413
1 parent 86f1157 commit ca1fff0

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

.generator/src/generator/templates/rest.j2

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ from {{ package }}.exceptions import (
2121
logger = logging.getLogger(__name__)
2222

2323

24-
RETRY_AFTER_STATUS_CODES = frozenset([413, 429, 500, 501, 502, 503, 504, 505, 506, 507, 509, 510, 511])
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"])
2526

2627

2728
class ClientRetry(urllib3.util.Retry):
2829
RETRY_AFTER_STATUS_CODES = RETRY_AFTER_STATUS_CODES
29-
DEFAULT_ALLOWED_METHODS = frozenset(["GET", "PUT", "DELETE", "POST", "PATCH"])
30+
DEFAULT_ALLOWED_METHODS = RETRY_ALLOWED_METHODS
3031

3132
def get_retry_after(self, response):
3233
"""
@@ -240,9 +241,10 @@ class AsyncRESTClientObject:
240241
self._client = aiosonic.HTTPClient(proxy=proxy)
241242
self._configuration = configuration
242243

243-
def _retry(self, response, counter):
244+
def _retry(self, method, response, counter):
244245
if (not self._configuration.enable_retry
245246
or counter >= self._configuration.max_retries
247+
or method not in RETRY_ALLOWED_METHODS
246248
or response.status_code not in RETRY_AFTER_STATUS_CODES):
247249
return 0
248250
retry_after = response.headers.get("X-Ratelimit-Reset")
@@ -309,7 +311,7 @@ class AsyncRESTClientObject:
309311
response = await self._client.request(
310312
url, method, headers, query_params, request_body, timeouts=request_timeout
311313
)
312-
retry = self._retry(response, counter)
314+
retry = self._retry(method, response, counter)
313315
if not retry:
314316
break
315317
import asyncio

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ repos:
3131
- id: generator
3232
name: generator
3333
language: python
34-
entry: bash -c "cd .generator && poetry install && poetry run python -m generator ./schemas/v1/openapi.yaml ./schemas/v2/openapi.yaml -o ../src/"
34+
entry: bash -c "unset VIRTUAL_ENV && cd .generator && poetry install && poetry run python -m generator ./schemas/v1/openapi.yaml ./schemas/v2/openapi.yaml -o ../src/"
3535
files: "^.generator/(config|schemas/v1|src|poetry.lock|pyproject.toml)"
3636
stages: [manual]
3737
pass_filenames: false
@@ -40,7 +40,7 @@ repos:
4040
- id: examples
4141
name: examples
4242
language: python
43-
entry: bash -c "cd .generator && poetry install && poetry run pytest"
43+
entry: bash -c "unset VIRTUAL_ENV && cd .generator && poetry install && poetry run pytest"
4444
files: "^.generator/"
4545
stages: [manual]
4646
pass_filenames: false

src/datadog_api_client/rest.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
logger = logging.getLogger(__name__)
2424

2525

26-
RETRY_AFTER_STATUS_CODES = frozenset([413, 429, 500, 501, 502, 503, 504, 505, 506, 507, 509, 510, 511])
26+
RETRY_AFTER_STATUS_CODES = frozenset([429, 500, 501, 502, 503, 504, 505, 506, 507, 509, 510, 511])
27+
RETRY_ALLOWED_METHODS = frozenset(["GET", "PUT", "DELETE", "POST", "PATCH"])
2728

2829

2930
class ClientRetry(urllib3.util.Retry):
3031
RETRY_AFTER_STATUS_CODES = RETRY_AFTER_STATUS_CODES
31-
DEFAULT_ALLOWED_METHODS = frozenset(["GET", "PUT", "DELETE", "POST", "PATCH"])
32+
DEFAULT_ALLOWED_METHODS = RETRY_ALLOWED_METHODS
3233

3334
def get_retry_after(self, response):
3435
"""
@@ -242,10 +243,11 @@ def __init__(self, configuration):
242243
self._client = aiosonic.HTTPClient(proxy=proxy)
243244
self._configuration = configuration
244245

245-
def _retry(self, response, counter):
246+
def _retry(self, method, response, counter):
246247
if (
247248
not self._configuration.enable_retry
248249
or counter >= self._configuration.max_retries
250+
or method not in RETRY_ALLOWED_METHODS
249251
or response.status_code not in RETRY_AFTER_STATUS_CODES
250252
):
251253
return 0
@@ -313,7 +315,7 @@ async def request(
313315
response = await self._client.request(
314316
url, method, headers, query_params, request_body, timeouts=request_timeout
315317
)
316-
retry = self._retry(response, counter)
318+
retry = self._retry(method, response, counter)
317319
if not retry:
318320
break
319321
import asyncio

0 commit comments

Comments
 (0)