Skip to content

Commit e14bd4b

Browse files
chore(internal): add support for parsing bool response content (#121)
1 parent b1e4a4f commit e14bd4b

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/onebusaway/_response.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T:
192192
if cast_to == float:
193193
return cast(R, float(response.text))
194194

195+
if cast_to == bool:
196+
return cast(R, response.text.lower() == "true")
197+
195198
origin = get_origin(cast_to) or cast_to
196199

197200
if origin == APIResponse:

tests/test_response.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,56 @@ async def test_async_response_parse_annotated_type(async_client: AsyncOnebusaway
190190
assert obj.bar == 2
191191

192192

193+
@pytest.mark.parametrize(
194+
"content, expected",
195+
[
196+
("false", False),
197+
("true", True),
198+
("False", False),
199+
("True", True),
200+
("TrUe", True),
201+
("FalSe", False),
202+
],
203+
)
204+
def test_response_parse_bool(client: OnebusawaySDK, content: str, expected: bool) -> None:
205+
response = APIResponse(
206+
raw=httpx.Response(200, content=content),
207+
client=client,
208+
stream=False,
209+
stream_cls=None,
210+
cast_to=str,
211+
options=FinalRequestOptions.construct(method="get", url="/foo"),
212+
)
213+
214+
result = response.parse(to=bool)
215+
assert result is expected
216+
217+
218+
@pytest.mark.parametrize(
219+
"content, expected",
220+
[
221+
("false", False),
222+
("true", True),
223+
("False", False),
224+
("True", True),
225+
("TrUe", True),
226+
("FalSe", False),
227+
],
228+
)
229+
async def test_async_response_parse_bool(client: AsyncOnebusawaySDK, content: str, expected: bool) -> None:
230+
response = AsyncAPIResponse(
231+
raw=httpx.Response(200, content=content),
232+
client=client,
233+
stream=False,
234+
stream_cls=None,
235+
cast_to=str,
236+
options=FinalRequestOptions.construct(method="get", url="/foo"),
237+
)
238+
239+
result = await response.parse(to=bool)
240+
assert result is expected
241+
242+
193243
class OtherModel(BaseModel):
194244
a: str
195245

0 commit comments

Comments
 (0)