Skip to content

Commit 066e5fe

Browse files
committed
chore(internal): codegen related update
1 parent 71dd372 commit 066e5fe

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

src/asktable/_base_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,10 @@ def _build_headers(self, options: FinalRequestOptions, *, retries_taken: int = 0
412412
if idempotency_header and options.method.lower() != "get" and idempotency_header not in headers:
413413
headers[idempotency_header] = options.idempotency_key or self._idempotency_key()
414414

415-
headers.setdefault("x-stainless-retry-count", str(retries_taken))
415+
# Don't set the retry count header if it was already set or removed by the caller. We check
416+
# `custom_headers`, which can contain `Omit()`, instead of `headers` to account for the removal case.
417+
if "x-stainless-retry-count" not in (header.lower() for header in custom_headers):
418+
headers["x-stainless-retry-count"] = str(retries_taken)
416419

417420
return headers
418421

tests/test_client.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pydantic import ValidationError
1818

1919
from asktable import Asktable, AsyncAsktable, APIResponseValidationError
20+
from asktable._types import Omit
2021
from asktable._models import BaseModel, FinalRequestOptions
2122
from asktable._constants import RAW_RESPONSE_HEADER
2223
from asktable._exceptions import APIStatusError, APITimeoutError, APIResponseValidationError
@@ -714,6 +715,56 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
714715
assert response.retries_taken == failures_before_success
715716
assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success
716717

718+
@pytest.mark.parametrize("failures_before_success", [0, 2, 4])
719+
@mock.patch("asktable._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
720+
@pytest.mark.respx(base_url=base_url)
721+
def test_omit_retry_count_header(
722+
self, client: Asktable, failures_before_success: int, respx_mock: MockRouter
723+
) -> None:
724+
client = client.with_options(max_retries=4)
725+
726+
nb_retries = 0
727+
728+
def retry_handler(_request: httpx.Request) -> httpx.Response:
729+
nonlocal nb_retries
730+
if nb_retries < failures_before_success:
731+
nb_retries += 1
732+
return httpx.Response(500)
733+
return httpx.Response(200)
734+
735+
respx_mock.post("/sys/projects").mock(side_effect=retry_handler)
736+
737+
response = client.sys.projects.with_raw_response.create(
738+
name="name", extra_headers={"x-stainless-retry-count": Omit()}
739+
)
740+
741+
assert len(response.http_request.headers.get_list("x-stainless-retry-count")) == 0
742+
743+
@pytest.mark.parametrize("failures_before_success", [0, 2, 4])
744+
@mock.patch("asktable._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
745+
@pytest.mark.respx(base_url=base_url)
746+
def test_overwrite_retry_count_header(
747+
self, client: Asktable, failures_before_success: int, respx_mock: MockRouter
748+
) -> None:
749+
client = client.with_options(max_retries=4)
750+
751+
nb_retries = 0
752+
753+
def retry_handler(_request: httpx.Request) -> httpx.Response:
754+
nonlocal nb_retries
755+
if nb_retries < failures_before_success:
756+
nb_retries += 1
757+
return httpx.Response(500)
758+
return httpx.Response(200)
759+
760+
respx_mock.post("/sys/projects").mock(side_effect=retry_handler)
761+
762+
response = client.sys.projects.with_raw_response.create(
763+
name="name", extra_headers={"x-stainless-retry-count": "42"}
764+
)
765+
766+
assert response.http_request.headers.get("x-stainless-retry-count") == "42"
767+
717768

718769
class TestAsyncAsktable:
719770
client = AsyncAsktable(base_url=base_url, _strict_response_validation=True)
@@ -1389,3 +1440,55 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
13891440

13901441
assert response.retries_taken == failures_before_success
13911442
assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success
1443+
1444+
@pytest.mark.parametrize("failures_before_success", [0, 2, 4])
1445+
@mock.patch("asktable._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
1446+
@pytest.mark.respx(base_url=base_url)
1447+
@pytest.mark.asyncio
1448+
async def test_omit_retry_count_header(
1449+
self, async_client: AsyncAsktable, failures_before_success: int, respx_mock: MockRouter
1450+
) -> None:
1451+
client = async_client.with_options(max_retries=4)
1452+
1453+
nb_retries = 0
1454+
1455+
def retry_handler(_request: httpx.Request) -> httpx.Response:
1456+
nonlocal nb_retries
1457+
if nb_retries < failures_before_success:
1458+
nb_retries += 1
1459+
return httpx.Response(500)
1460+
return httpx.Response(200)
1461+
1462+
respx_mock.post("/sys/projects").mock(side_effect=retry_handler)
1463+
1464+
response = await client.sys.projects.with_raw_response.create(
1465+
name="name", extra_headers={"x-stainless-retry-count": Omit()}
1466+
)
1467+
1468+
assert len(response.http_request.headers.get_list("x-stainless-retry-count")) == 0
1469+
1470+
@pytest.mark.parametrize("failures_before_success", [0, 2, 4])
1471+
@mock.patch("asktable._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
1472+
@pytest.mark.respx(base_url=base_url)
1473+
@pytest.mark.asyncio
1474+
async def test_overwrite_retry_count_header(
1475+
self, async_client: AsyncAsktable, failures_before_success: int, respx_mock: MockRouter
1476+
) -> None:
1477+
client = async_client.with_options(max_retries=4)
1478+
1479+
nb_retries = 0
1480+
1481+
def retry_handler(_request: httpx.Request) -> httpx.Response:
1482+
nonlocal nb_retries
1483+
if nb_retries < failures_before_success:
1484+
nb_retries += 1
1485+
return httpx.Response(500)
1486+
return httpx.Response(200)
1487+
1488+
respx_mock.post("/sys/projects").mock(side_effect=retry_handler)
1489+
1490+
response = await client.sys.projects.with_raw_response.create(
1491+
name="name", extra_headers={"x-stainless-retry-count": "42"}
1492+
)
1493+
1494+
assert response.http_request.headers.get("x-stainless-retry-count") == "42"

0 commit comments

Comments
 (0)