Skip to content

Commit 55b10c1

Browse files
feat(client): add follow_redirects request option
1 parent ef26230 commit 55b10c1

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

src/hubmap_search_sdk/_base_client.py

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

966+
if options.follow_redirects is not None:
967+
kwargs["follow_redirects"] = options.follow_redirects
968+
966969
log.debug("Sending HTTP Request: %s %s", request.method, request.url)
967970

968971
response = None
@@ -1472,6 +1475,9 @@ async def request(
14721475
if self.custom_auth is not None:
14731476
kwargs["auth"] = self.custom_auth
14741477

1478+
if options.follow_redirects is not None:
1479+
kwargs["follow_redirects"] = options.follow_redirects
1480+
14751481
log.debug("Sending HTTP Request: %s %s", request.method, request.url)
14761482

14771483
response = None

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

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

836+
@pytest.mark.respx(base_url=base_url)
837+
def test_follow_redirects(self, respx_mock: MockRouter) -> None:
838+
# Test that the default follow_redirects=True allows following redirects
839+
respx_mock.post("/redirect").mock(
840+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
841+
)
842+
respx_mock.get("/redirected").mock(return_value=httpx.Response(200, json={"status": "ok"}))
843+
844+
response = self.client.post("/redirect", body={"key": "value"}, cast_to=httpx.Response)
845+
assert response.status_code == 200
846+
assert response.json() == {"status": "ok"}
847+
848+
@pytest.mark.respx(base_url=base_url)
849+
def test_follow_redirects_disabled(self, respx_mock: MockRouter) -> None:
850+
# Test that follow_redirects=False prevents following redirects
851+
respx_mock.post("/redirect").mock(
852+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
853+
)
854+
855+
with pytest.raises(APIStatusError) as exc_info:
856+
self.client.post(
857+
"/redirect", body={"key": "value"}, options={"follow_redirects": False}, cast_to=httpx.Response
858+
)
859+
860+
assert exc_info.value.response.status_code == 302
861+
assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected"
862+
836863

837864
class TestAsyncHubmapSearchSDK:
838865
client = AsyncHubmapSearchSDK(base_url=base_url, bearer_token=bearer_token, _strict_response_validation=True)
@@ -1669,3 +1696,30 @@ async def test_main() -> None:
16691696
raise AssertionError("calling get_platform using asyncify resulted in a hung process")
16701697

16711698
time.sleep(0.1)
1699+
1700+
@pytest.mark.respx(base_url=base_url)
1701+
async def test_follow_redirects(self, respx_mock: MockRouter) -> None:
1702+
# Test that the default follow_redirects=True allows following redirects
1703+
respx_mock.post("/redirect").mock(
1704+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
1705+
)
1706+
respx_mock.get("/redirected").mock(return_value=httpx.Response(200, json={"status": "ok"}))
1707+
1708+
response = await self.client.post("/redirect", body={"key": "value"}, cast_to=httpx.Response)
1709+
assert response.status_code == 200
1710+
assert response.json() == {"status": "ok"}
1711+
1712+
@pytest.mark.respx(base_url=base_url)
1713+
async def test_follow_redirects_disabled(self, respx_mock: MockRouter) -> None:
1714+
# Test that follow_redirects=False prevents following redirects
1715+
respx_mock.post("/redirect").mock(
1716+
return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"})
1717+
)
1718+
1719+
with pytest.raises(APIStatusError) as exc_info:
1720+
await self.client.post(
1721+
"/redirect", body={"key": "value"}, options={"follow_redirects": False}, cast_to=httpx.Response
1722+
)
1723+
1724+
assert exc_info.value.response.status_code == 302
1725+
assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected"

0 commit comments

Comments
 (0)