GeezerVison is showing github.com — feat: add 'strict' to flatten_query_params to lower-case bools (#433) · googleapis/python-api-core@83678e9
This repository was archived by the owner on Feb 23, 2026. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: google/api_core/rest_helpers.py +32-17Lines changed: 32 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 | import operator | 19 | 19 |
| 20 | 20 |
| 21 | | -def flatten_query_params(obj): | 22 | | - """Flatten a nested dict into a list of (name,value) tuples. | | 21 | +def flatten_query_params(obj, strict=False): | | 22 | + """Flatten a dict into a list of (name,value) tuples. | 23 | 23 |
| 24 | 24 | The result is suitable for setting query params on an http request. | 25 | 25 |
| @@ -28,17 +28,20 @@ def flatten_query_params(obj): | 28 | 28 | >>> obj = {'a': | 29 | 29 | ... {'b': | 30 | 30 | ... {'c': ['x', 'y', 'z']} }, | 31 | | - ... 'd': 'uvw', } | 32 | | - >>> flatten_query_params(obj) | 33 | | - [('a.b.c', 'x'), ('a.b.c', 'y'), ('a.b.c', 'z'), ('d', 'uvw')] | | 31 | + ... 'd': 'uvw', | | 32 | + ... 'e': True, } | | 33 | + >>> flatten_query_params(obj, strict=True) | | 34 | + [('a.b.c', 'x'), ('a.b.c', 'y'), ('a.b.c', 'z'), ('d', 'uvw'), ('e', 'true')] | 34 | 35 |
| 35 | 36 | Note that, as described in | 36 | 37 | https://github.com/googleapis/googleapis/blob/48d9fb8c8e287c472af500221c6450ecd45d7d39/google/api/http.proto#L117, | 37 | 38 | repeated fields (i.e. list-valued fields) may only contain primitive types (not lists or dicts). | 38 | 39 | This is enforced in this function. | 39 | 40 |
| 40 | 41 | Args: | 41 | | - obj: a nested dictionary (from json), or None | | 42 | + obj: a possibly nested dictionary (from json), or None | | 43 | + strict: a bool, defaulting to False, to enforce that all values in the | | 44 | + result tuples be strings and, if boolean, lower-cased. | 42 | 45 |
| 43 | 46 | Returns: a list of tuples, with each tuple having a (possibly) multi-part name | 44 | 47 | and a scalar value. | @@ -51,17 +54,17 @@ def flatten_query_params(obj): | 51 | 54 | if obj is not None and not isinstance(obj, dict): | 52 | 55 | raise TypeError("flatten_query_params must be called with dict object") | 53 | 56 |
| 54 | | - return _flatten(obj, key_path=[]) | | 57 | + return _flatten(obj, key_path=[], strict=strict) | 55 | 58 |
| 56 | 59 |
| 57 | | -def _flatten(obj, key_path): | | 60 | +def _flatten(obj, key_path, strict=False): | 58 | 61 | if obj is None: | 59 | 62 | return [] | 60 | 63 | if isinstance(obj, dict): | 61 | | - return _flatten_dict(obj, key_path=key_path) | | 64 | + return _flatten_dict(obj, key_path=key_path, strict=strict) | 62 | 65 | if isinstance(obj, list): | 63 | | - return _flatten_list(obj, key_path=key_path) | 64 | | - return _flatten_value(obj, key_path=key_path) | | 66 | + return _flatten_list(obj, key_path=key_path, strict=strict) | | 67 | + return _flatten_value(obj, key_path=key_path, strict=strict) | 65 | 68 |
| 66 | 69 |
| 67 | 70 | def _is_primitive_value(obj): | @@ -74,21 +77,33 @@ def _is_primitive_value(obj): | 74 | 77 | return True | 75 | 78 |
| 76 | 79 |
| 77 | | -def _flatten_value(obj, key_path): | 78 | | - return [(".".join(key_path), obj)] | | 80 | +def _flatten_value(obj, key_path, strict=False): | | 81 | + return [(".".join(key_path), _canonicalize(obj, strict=strict))] | 79 | 82 |
| 80 | 83 |
| 81 | | -def _flatten_dict(obj, key_path): | 82 | | - items = (_flatten(value, key_path=key_path + [key]) for key, value in obj.items()) | | 84 | +def _flatten_dict(obj, key_path, strict=False): | | 85 | + items = ( | | 86 | + _flatten(value, key_path=key_path + [key], strict=strict) | | 87 | + for key, value in obj.items() | | 88 | + ) | 83 | 89 | return functools.reduce(operator.concat, items, []) | 84 | 90 |
| 85 | 91 |
| 86 | | -def _flatten_list(elems, key_path): | | 92 | +def _flatten_list(elems, key_path, strict=False): | 87 | 93 | # Only lists of scalar values are supported. | 88 | 94 | # The name (key_path) is repeated for each value. | 89 | 95 | items = ( | 90 | | - _flatten_value(elem, key_path=key_path) | | 96 | + _flatten_value(elem, key_path=key_path, strict=strict) | 91 | 97 | for elem in elems | 92 | 98 | if _is_primitive_value(elem) | 93 | 99 | ) | 94 | 100 | return functools.reduce(operator.concat, items, []) | | 101 | + | | 102 | + | | 103 | +def _canonicalize(obj, strict=False): | | 104 | + if strict: | | 105 | + value = str(obj) | | 106 | + if isinstance(obj, bool): | | 107 | + value = value.lower() | | 108 | + return value | | 109 | + return obj |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/test_rest_helpers.py +18-1Lines changed: 18 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,9 +36,26 @@ def test_flatten_empty_dict(): | 36 | 36 |
| 37 | 37 |
| 38 | 38 | def test_flatten_simple_dict(): | 39 | | - assert rest_helpers.flatten_query_params({"a": "abc", "b": "def"}) == [ | | 39 | + obj = {"a": "abc", "b": "def", "c": True, "d": False, "e": 10, "f": -3.76} | | 40 | + assert rest_helpers.flatten_query_params(obj) == [ | 40 | 41 | ("a", "abc"), | 41 | 42 | ("b", "def"), | | 43 | + ("c", True), | | 44 | + ("d", False), | | 45 | + ("e", 10), | | 46 | + ("f", -3.76), | | 47 | + ] | | 48 | + | | 49 | + | | 50 | +def test_flatten_simple_dict_strict(): | | 51 | + obj = {"a": "abc", "b": "def", "c": True, "d": False, "e": 10, "f": -3.76} | | 52 | + assert rest_helpers.flatten_query_params(obj, strict=True) == [ | | 53 | + ("a", "abc"), | | 54 | + ("b", "def"), | | 55 | + ("c", "true"), | | 56 | + ("d", "false"), | | 57 | + ("e", "10"), | | 58 | + ("f", "-3.76"), | 42 | 59 | ] | 43 | 60 |
| 44 | 61 |
| |
|