Skip to content

Commit 40221d5

Browse files
feat(client): add follow_redirects request option
1 parent 96a0239 commit 40221d5

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

src/isaacus/_base_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,9 @@ def request(
935935
if self.custom_auth is not None:
936936
kwargs["auth"] = self.custom_auth
937937

938+
if options.follow_redirects is not None:
939+
kwargs["follow_redirects"] = options.follow_redirects
940+
938941
log.debug("Sending HTTP Request: %s %s", request.method, request.url)
939942

940943
response = None
@@ -1435,6 +1438,9 @@ async def request(
14351438
if self.custom_auth is not None:
14361439
kwargs["auth"] = self.custom_auth
14371440

1441+
if options.follow_redirects is not None:
1442+
kwargs["follow_redirects"] = options.follow_redirects
1443+
14381444
log.debug("Sending HTTP Request: %s %s", request.method, request.url)
14391445

14401446
response = None

src/isaacus/_models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ class FinalRequestOptionsInput(TypedDict, total=False):
737737
idempotency_key: str
738738
json_data: Body
739739
extra_json: AnyMapping
740+
follow_redirects: bool
740741

741742

742743
@final
@@ -750,6 +751,7 @@ class FinalRequestOptions(pydantic.BaseModel):
750751
files: Union[HttpxRequestFiles, None] = None
751752
idempotency_key: Union[str, None] = None
752753
post_parser: Union[Callable[[Any], Any], NotGiven] = NotGiven()
754+
follow_redirects: Union[bool, None] = None
753755

754756
# It should be noted that we cannot use `json` here as that would override
755757
# a BaseModel method in an incompatible fashion.

src/isaacus/_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class RequestOptions(TypedDict, total=False):
100100
params: Query
101101
extra_json: AnyMapping
102102
idempotency_key: str
103+
follow_redirects: bool
103104

104105

105106
# Sentinel class used until PEP 0661 is accepted
@@ -215,3 +216,4 @@ class _GenericAlias(Protocol):
215216

216217
class HttpxSendArgs(TypedDict, total=False):
217218
auth: httpx.Auth
219+
follow_redirects: bool

tests/test_client.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,33 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
850850

851851
assert response.http_request.headers.get("x-stainless-retry-count") == "42"
852852

853+
@pytest.mark.respx(base_url=base_url)
854+
def test_follow_redirects(self, respx_mock: MockRouter) -> None:
855+
# Test that the default follow_redirects=True allows following redirects
856+
respx_mock.post("/redirect").mock(
857+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
858+
)
859+
respx_mock.get("/redirected").mock(return_value=httpx.Response(200, json={"status": "ok"}))
860+
861+
response = self.client.post("/redirect", body={"key": "value"}, cast_to=httpx.Response)
862+
assert response.status_code == 200
863+
assert response.json() == {"status": "ok"}
864+
865+
@pytest.mark.respx(base_url=base_url)
866+
def test_follow_redirects_disabled(self, respx_mock: MockRouter) -> None:
867+
# Test that follow_redirects=False prevents following redirects
868+
respx_mock.post("/redirect").mock(
869+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
870+
)
871+
872+
with pytest.raises(APIStatusError) as exc_info:
873+
self.client.post(
874+
"/redirect", body={"key": "value"}, options={"follow_redirects": False}, cast_to=httpx.Response
875+
)
876+
877+
assert exc_info.value.response.status_code == 302
878+
assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected"
879+
853880

854881
class TestAsyncIsaacus:
855882
client = AsyncIsaacus(base_url=base_url, api_key=api_key, _strict_response_validation=True)
@@ -1707,3 +1734,30 @@ async def test_main() -> None:
17071734
raise AssertionError("calling get_platform using asyncify resulted in a hung process")
17081735

17091736
time.sleep(0.1)
1737+
1738+
@pytest.mark.respx(base_url=base_url)
1739+
async def test_follow_redirects(self, respx_mock: MockRouter) -> None:
1740+
# Test that the default follow_redirects=True allows following redirects
1741+
respx_mock.post("/redirect").mock(
1742+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
1743+
)
1744+
respx_mock.get("/redirected").mock(return_value=httpx.Response(200, json={"status": "ok"}))
1745+
1746+
response = await self.client.post("/redirect", body={"key": "value"}, cast_to=httpx.Response)
1747+
assert response.status_code == 200
1748+
assert response.json() == {"status": "ok"}
1749+
1750+
@pytest.mark.respx(base_url=base_url)
1751+
async def test_follow_redirects_disabled(self, respx_mock: MockRouter) -> None:
1752+
# Test that follow_redirects=False prevents following redirects
1753+
respx_mock.post("/redirect").mock(
1754+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
1755+
)
1756+
1757+
with pytest.raises(APIStatusError) as exc_info:
1758+
await self.client.post(
1759+
"/redirect", body={"key": "value"}, options={"follow_redirects": False}, cast_to=httpx.Response
1760+
)
1761+
1762+
assert exc_info.value.response.status_code == 302
1763+
assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected"

0 commit comments

Comments
 (0)