|
17 | 17 | from pydantic import ValidationError |
18 | 18 |
|
19 | 19 | from asktable import Asktable, AsyncAsktable, APIResponseValidationError |
| 20 | +from asktable._types import Omit |
20 | 21 | from asktable._models import BaseModel, FinalRequestOptions |
21 | 22 | from asktable._constants import RAW_RESPONSE_HEADER |
22 | 23 | from asktable._exceptions import APIStatusError, APITimeoutError, APIResponseValidationError |
@@ -714,6 +715,56 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: |
714 | 715 | assert response.retries_taken == failures_before_success |
715 | 716 | assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success |
716 | 717 |
|
| 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 | + |
717 | 768 |
|
718 | 769 | class TestAsyncAsktable: |
719 | 770 | client = AsyncAsktable(base_url=base_url, _strict_response_validation=True) |
@@ -1389,3 +1440,55 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: |
1389 | 1440 |
|
1390 | 1441 | assert response.retries_taken == failures_before_success |
1391 | 1442 | 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