Skip to content

Commit 87d8986

Browse files
feat(client): add follow_redirects request option
1 parent 9f8db4a commit 87d8986

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

src/openlayer/_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/openlayer/_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/openlayer/_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
@@ -921,6 +921,33 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
921921

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

924+
@pytest.mark.respx(base_url=base_url)
925+
def test_follow_redirects(self, respx_mock: MockRouter) -> None:
926+
# Test that the default follow_redirects=True allows following redirects
927+
respx_mock.post("/redirect").mock(
928+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
929+
)
930+
respx_mock.get("/redirected").mock(return_value=httpx.Response(200, json={"status": "ok"}))
931+
932+
response = self.client.post("/redirect", body={"key": "value"}, cast_to=httpx.Response)
933+
assert response.status_code == 200
934+
assert response.json() == {"status": "ok"}
935+
936+
@pytest.mark.respx(base_url=base_url)
937+
def test_follow_redirects_disabled(self, respx_mock: MockRouter) -> None:
938+
# Test that follow_redirects=False prevents following redirects
939+
respx_mock.post("/redirect").mock(
940+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
941+
)
942+
943+
with pytest.raises(APIStatusError) as exc_info:
944+
self.client.post(
945+
"/redirect", body={"key": "value"}, options={"follow_redirects": False}, cast_to=httpx.Response
946+
)
947+
948+
assert exc_info.value.response.status_code == 302
949+
assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected"
950+
924951

925952
class TestAsyncOpenlayer:
926953
client = AsyncOpenlayer(base_url=base_url, api_key=api_key, _strict_response_validation=True)
@@ -1847,3 +1874,30 @@ async def test_main() -> None:
18471874
raise AssertionError("calling get_platform using asyncify resulted in a hung process")
18481875

18491876
time.sleep(0.1)
1877+
1878+
@pytest.mark.respx(base_url=base_url)
1879+
async def test_follow_redirects(self, respx_mock: MockRouter) -> None:
1880+
# Test that the default follow_redirects=True allows following redirects
1881+
respx_mock.post("/redirect").mock(
1882+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
1883+
)
1884+
respx_mock.get("/redirected").mock(return_value=httpx.Response(200, json={"status": "ok"}))
1885+
1886+
response = await self.client.post("/redirect", body={"key": "value"}, cast_to=httpx.Response)
1887+
assert response.status_code == 200
1888+
assert response.json() == {"status": "ok"}
1889+
1890+
@pytest.mark.respx(base_url=base_url)
1891+
async def test_follow_redirects_disabled(self, respx_mock: MockRouter) -> None:
1892+
# Test that follow_redirects=False prevents following redirects
1893+
respx_mock.post("/redirect").mock(
1894+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
1895+
)
1896+
1897+
with pytest.raises(APIStatusError) as exc_info:
1898+
await self.client.post(
1899+
"/redirect", body={"key": "value"}, options={"follow_redirects": False}, cast_to=httpx.Response
1900+
)
1901+
1902+
assert exc_info.value.response.status_code == 302
1903+
assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected"

0 commit comments

Comments
 (0)