Skip to content

Commit c76d62a

Browse files
feat(client): add follow_redirects request option
1 parent cf2e05b commit c76d62a

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

src/conductor/_base_client.py

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

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

965968
response = None
@@ -1460,6 +1463,9 @@ async def request(
14601463
if self.custom_auth is not None:
14611464
kwargs["auth"] = self.custom_auth
14621465

1466+
if options.follow_redirects is not None:
1467+
kwargs["follow_redirects"] = options.follow_redirects
1468+
14631469
log.debug("Sending HTTP Request: %s %s", request.method, request.url)
14641470

14651471
response = None

src/conductor/_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/conductor/_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: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from conductor import Conductor, AsyncConductor, APIResponseValidationError
2525
from conductor._types import Omit
2626
from conductor._models import BaseModel, FinalRequestOptions
27-
from conductor._exceptions import ConductorError, APIResponseValidationError
27+
from conductor._exceptions import APIStatusError, ConductorError, APIResponseValidationError
2828
from conductor._base_client import (
2929
DEFAULT_TIMEOUT,
3030
HTTPX_DEFAULT_TIMEOUT,
@@ -781,6 +781,33 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
781781

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

784+
@pytest.mark.respx(base_url=base_url)
785+
def test_follow_redirects(self, respx_mock: MockRouter) -> None:
786+
# Test that the default follow_redirects=True allows following redirects
787+
respx_mock.post("/redirect").mock(
788+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
789+
)
790+
respx_mock.get("/redirected").mock(return_value=httpx.Response(200, json={"status": "ok"}))
791+
792+
response = self.client.post("/redirect", body={"key": "value"}, cast_to=httpx.Response)
793+
assert response.status_code == 200
794+
assert response.json() == {"status": "ok"}
795+
796+
@pytest.mark.respx(base_url=base_url)
797+
def test_follow_redirects_disabled(self, respx_mock: MockRouter) -> None:
798+
# Test that follow_redirects=False prevents following redirects
799+
respx_mock.post("/redirect").mock(
800+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
801+
)
802+
803+
with pytest.raises(APIStatusError) as exc_info:
804+
self.client.post(
805+
"/redirect", body={"key": "value"}, options={"follow_redirects": False}, cast_to=httpx.Response
806+
)
807+
808+
assert exc_info.value.response.status_code == 302
809+
assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected"
810+
784811

785812
class TestAsyncConductor:
786813
client = AsyncConductor(base_url=base_url, api_key=api_key, _strict_response_validation=True)
@@ -1578,3 +1605,30 @@ async def test_main() -> None:
15781605
raise AssertionError("calling get_platform using asyncify resulted in a hung process")
15791606

15801607
time.sleep(0.1)
1608+
1609+
@pytest.mark.respx(base_url=base_url)
1610+
async def test_follow_redirects(self, respx_mock: MockRouter) -> None:
1611+
# Test that the default follow_redirects=True allows following redirects
1612+
respx_mock.post("/redirect").mock(
1613+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
1614+
)
1615+
respx_mock.get("/redirected").mock(return_value=httpx.Response(200, json={"status": "ok"}))
1616+
1617+
response = await self.client.post("/redirect", body={"key": "value"}, cast_to=httpx.Response)
1618+
assert response.status_code == 200
1619+
assert response.json() == {"status": "ok"}
1620+
1621+
@pytest.mark.respx(base_url=base_url)
1622+
async def test_follow_redirects_disabled(self, respx_mock: MockRouter) -> None:
1623+
# Test that follow_redirects=False prevents following redirects
1624+
respx_mock.post("/redirect").mock(
1625+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
1626+
)
1627+
1628+
with pytest.raises(APIStatusError) as exc_info:
1629+
await self.client.post(
1630+
"/redirect", body={"key": "value"}, options={"follow_redirects": False}, cast_to=httpx.Response
1631+
)
1632+
1633+
assert exc_info.value.response.status_code == 302
1634+
assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected"

0 commit comments

Comments
 (0)