Skip to content

Commit

Permalink
chore(internal): add support for parsing bool response content (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed Oct 4, 2024
1 parent 025b0ba commit fd1db20
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/studio_sdk/_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T:
if cast_to == float:
return cast(R, float(response.text))

if cast_to == bool:
return cast(R, response.text.lower() == "true")

origin = get_origin(cast_to) or cast_to

if origin == APIResponse:
Expand Down
50 changes: 50 additions & 0 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,56 @@ async def test_async_response_parse_annotated_type(async_client: AsyncStudioSDK)
assert obj.bar == 2


@pytest.mark.parametrize(
"content, expected",
[
("false", False),
("true", True),
("False", False),
("True", True),
("TrUe", True),
("FalSe", False),
],
)
def test_response_parse_bool(client: StudioSDK, content: str, expected: bool) -> None:
response = APIResponse(
raw=httpx.Response(200, content=content),
client=client,
stream=False,
stream_cls=None,
cast_to=str,
options=FinalRequestOptions.construct(method="get", url="/foo"),
)

result = response.parse(to=bool)
assert result is expected


@pytest.mark.parametrize(
"content, expected",
[
("false", False),
("true", True),
("False", False),
("True", True),
("TrUe", True),
("FalSe", False),
],
)
async def test_async_response_parse_bool(client: AsyncStudioSDK, content: str, expected: bool) -> None:
response = AsyncAPIResponse(
raw=httpx.Response(200, content=content),
client=client,
stream=False,
stream_cls=None,
cast_to=str,
options=FinalRequestOptions.construct(method="get", url="/foo"),
)

result = await response.parse(to=bool)
assert result is expected


class OtherModel(BaseModel):
a: str

Expand Down

0 comments on commit fd1db20

Please sign in to comment.