Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions httpx/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@
is_known_encoding,
normalize_header_key,
normalize_header_value,
str_query_param
)

PrimitiveData = typing.Union[str, int, float, bool, type(None)]

URLTypes = typing.Union["URL", str]

QueryParamTypes = typing.Union[
"QueryParams",
typing.Mapping[str, str],
typing.List[typing.Tuple[typing.Any, typing.Any]],
typing.Mapping[str, PrimitiveData],
typing.List[typing.Tuple[str, PrimitiveData]],
str,
]

Expand Down Expand Up @@ -259,8 +262,8 @@ def __init__(self, *args: QueryParamTypes, **kwargs: typing.Any) -> None:
else:
items = value.items() # type: ignore

self._list = [(str(k), str(v)) for k, v in items]
self._dict = {str(k): str(v) for k, v in items}
self._list = [(str(k), str_query_param(v)) for k, v in items]
self._dict = {str(k): str_query_param(v) for k, v in items}

def getlist(self, key: typing.Any) -> typing.List[str]:
return [item_value for item_key, item_value in self._list if item_key == key]
Expand Down
15 changes: 15 additions & 0 deletions httpx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ def normalize_header_value(value: typing.AnyStr, encoding: str = None) -> bytes:
return value.encode(encoding or "ascii")


def str_query_param(value: typing.Union[str, int, float, bool, type(None)]) -> str:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there any obstacles to using PrimitiveData? for consistency

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and no. We'd need some tweaking to avoid a cyclical dependency.

"""
Coerce a primitive data type into a string value for query params.

Note that we prefer JSON-style 'true'/'false' for boolean values here.
"""
if value is True:
return "true"
elif value is False:
return "false"
elif value is None:
return ""
return str(value)


def is_known_encoding(encoding: str) -> bool:
"""
Return `True` if `encoding` is a known codec.
Expand Down
20 changes: 20 additions & 0 deletions tests/models/test_queryparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,23 @@ def test_queryparams():

q = QueryParams([("a", "123"), ("a", "456")])
assert QueryParams(q) == q


def test_queryparam_types():
q = QueryParams({"a": True})
assert str(q) == "a=true"

q = QueryParams({"a": False})
assert str(q) == "a=false"

q = QueryParams({"a": ""})
assert str(q) == "a="

q = QueryParams({"a": None})
assert str(q) == "a="

q = QueryParams({"a": 1.23})
assert str(q) == "a=1.23"

q = QueryParams({"a": 123})
assert str(q) == "a=123"