MCPcopy
hub / github.com/encode/httpx / QueryParams

Class QueryParams

httpx/_urls.py:420–641  ·  view source on GitHub ↗

URL query parameters, as a multi-dict.

Source from the content-addressed store, hash-verified

418
419
420class QueryParams(typing.Mapping[str, str]):
421 """
422 URL query parameters, as a multi-dict.
423 """
424
425 def __init__(self, *args: QueryParamTypes | None, **kwargs: typing.Any) -> None:
426 assert len(args) < 2, "Too many arguments."
427 assert not (args and kwargs), "Cannot mix named and unnamed arguments."
428
429 value = args[0] if args else kwargs
430
431 if value is None or isinstance(value, (str, bytes)):
432 value = value.decode("ascii") if isinstance(value, bytes) else value
433 self._dict = parse_qs(value, keep_blank_values=True)
434 elif isinstance(value, QueryParams):
435 self._dict = {k: list(v) for k, v in value._dict.items()}
436 else:
437 dict_value: dict[typing.Any, list[typing.Any]] = {}
438 if isinstance(value, (list, tuple)):
439 # Convert list inputs like:
440 # [("a", "123"), ("a", "456"), ("b", "789")]
441 # To a dict representation, like:
442 # {"a": ["123", "456"], "b": ["789"]}
443 for item in value:
444 dict_value.setdefault(item[0], []).append(item[1])
445 else:
446 # Convert dict inputs like:
447 # {"a": "123", "b": ["456", "789"]}
448 # To dict inputs where values are always lists, like:
449 # {"a": ["123"], "b": ["456", "789"]}
450 dict_value = {
451 k: list(v) if isinstance(v, (list, tuple)) else [v]
452 for k, v in value.items()
453 }
454
455 # Ensure that keys and values are neatly coerced to strings.
456 # We coerce values `True` and `False` to JSON-like "true" and "false"
457 # representations, and coerce `None` values to the empty string.
458 self._dict = {
459 str(k): [primitive_value_to_str(item) for item in v]
460 for k, v in dict_value.items()
461 }
462
463 def keys(self) -> typing.KeysView[str]:
464 """
465 Return all the keys in the query params.
466
467 Usage:
468
469 q = httpx.QueryParams("a=123&a=456&b=789")
470 assert list(q.keys()) == ["a", "b"]
471 """
472 return self._dict.keys()
473
474 def values(self) -> typing.ValuesView[str]:
475 """
476 Return all the values in the query params. If a key occurs more than once
477 only the first item for that key is returned.

Callers 9

__init__Method · 0.85
paramsMethod · 0.85
setMethod · 0.85
addMethod · 0.85
removeMethod · 0.85
mergeMethod · 0.85
__init__Method · 0.85
paramsMethod · 0.85
_merge_queryparamsMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected