Skip to content

Commit 35e3181

Browse files
feat(client): add follow_redirects request option
1 parent 3375bf9 commit 35e3181

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/lithic/_base_client.py

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

964+
if options.follow_redirects is not None:
965+
kwargs["follow_redirects"] = options.follow_redirects
966+
964967
log.debug("Sending HTTP Request: %s %s", request.method, request.url)
965968

966969
response = None
@@ -1475,6 +1478,9 @@ async def request(
14751478
if self.custom_auth is not None:
14761479
kwargs["auth"] = self.custom_auth
14771480

1481+
if options.follow_redirects is not None:
1482+
kwargs["follow_redirects"] = options.follow_redirects
1483+
14781484
log.debug("Sending HTTP Request: %s %s", request.method, request.url)
14791485

14801486
response = None

src/lithic/_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/lithic/_types.py

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

105106

106107
# Sentinel class used until PEP 0661 is accepted
@@ -217,3 +218,4 @@ class _GenericAlias(Protocol):
217218

218219
class HttpxSendArgs(TypedDict, total=False):
219220
auth: httpx.Auth
221+
follow_redirects: bool

tests/test_client.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,33 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
846846
assert response.retries_taken == failures_before_success
847847
assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success
848848

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

850877
class TestAsyncLithic:
851878
client = AsyncLithic(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)