Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1c5eb4d

Browse files
kbandesKenneth Bandes
and
Kenneth Bandes
authored
feat: Add helper function to format query_params for rest transport. (#275)
Co-authored-by: Kenneth Bandes <kbandes@google.com>
1 parentafe0fa1 commit1c5eb4d

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

‎google/api_core/rest_helpers.py‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Helpers for rest transports."""
16+
17+
importfunctools
18+
importoperator
19+
20+
21+
defflatten_query_params(obj):
22+
"""Flatten a nested dict into a list of (name,value) tuples.
23+
24+
The result is suitable for setting query params on an http request.
25+
26+
.. code-block:: python
27+
28+
>>> obj = {'a':
29+
... {'b':
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')]
34+
35+
Note that, as described in
36+
https://github.com/googleapis/googleapis/blob/48d9fb8c8e287c472af500221c6450ecd45d7d39/google/api/http.proto#L117,
37+
repeated fields (i.e. list-valued fields) may only contain primitive types (not lists or dicts).
38+
This is enforced in this function.
39+
40+
Args:
41+
obj: a nested dictionary (from json), or None
42+
43+
Returns: a list of tuples, with each tuple having a (possibly) multi-part name
44+
and a scalar value.
45+
46+
Raises:
47+
TypeError if obj is not a dict or None
48+
ValueError if obj contains a list of non-primitive values.
49+
"""
50+
51+
ifobjisnotNoneandnotisinstance(obj,dict):
52+
raiseTypeError("flatten_query_params must be called with dict object")
53+
54+
return_flatten(obj,key_path=[])
55+
56+
57+
def_flatten(obj,key_path):
58+
ifobjisNone:
59+
return []
60+
ifisinstance(obj,dict):
61+
return_flatten_dict(obj,key_path=key_path)
62+
ifisinstance(obj,list):
63+
return_flatten_list(obj,key_path=key_path)
64+
return_flatten_value(obj,key_path=key_path)
65+
66+
67+
def_is_primitive_value(obj):
68+
ifobjisNone:
69+
returnFalse
70+
71+
ifisinstance(obj, (list,dict)):
72+
raiseValueError("query params may not contain repeated dicts or lists")
73+
74+
returnTrue
75+
76+
77+
def_flatten_value(obj,key_path):
78+
return [(".".join(key_path),obj)]
79+
80+
81+
def_flatten_dict(obj,key_path):
82+
items= (_flatten(value,key_path=key_path+ [key])forkey,valueinobj.items())
83+
returnfunctools.reduce(operator.concat,items, [])
84+
85+
86+
def_flatten_list(elems,key_path):
87+
# Only lists of scalar values are supported.
88+
# The name (key_path) is repeated for each value.
89+
items= (
90+
_flatten_value(elem,key_path=key_path)
91+
foreleminelems
92+
if_is_primitive_value(elem)
93+
)
94+
returnfunctools.reduce(operator.concat,items, [])

‎tests/unit/test_rest_helpers.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
importpytest
16+
17+
fromgoogle.api_coreimportrest_helpers
18+
19+
20+
deftest_flatten_simple_value():
21+
withpytest.raises(TypeError):
22+
rest_helpers.flatten_query_params("abc")
23+
24+
25+
deftest_flatten_list():
26+
withpytest.raises(TypeError):
27+
rest_helpers.flatten_query_params(["abc","def"])
28+
29+
30+
deftest_flatten_none():
31+
assertrest_helpers.flatten_query_params(None)== []
32+
33+
34+
deftest_flatten_empty_dict():
35+
assertrest_helpers.flatten_query_params({})== []
36+
37+
38+
deftest_flatten_simple_dict():
39+
assertrest_helpers.flatten_query_params({"a":"abc","b":"def"})== [
40+
("a","abc"),
41+
("b","def"),
42+
]
43+
44+
45+
deftest_flatten_repeated_field():
46+
assertrest_helpers.flatten_query_params({"a": ["x","y","z",None]})== [
47+
("a","x"),
48+
("a","y"),
49+
("a","z"),
50+
]
51+
52+
53+
deftest_flatten_nested_dict():
54+
obj= {"a": {"b": {"c": ["x","y","z"]}},"d": {"e":"uvw"}}
55+
expected_result= [("a.b.c","x"), ("a.b.c","y"), ("a.b.c","z"), ("d.e","uvw")]
56+
57+
assertrest_helpers.flatten_query_params(obj)==expected_result
58+
59+
60+
deftest_flatten_repeated_dict():
61+
obj= {
62+
"a": {"b": {"c": [{"v":1}, {"v":2}]}},
63+
"d":"uvw",
64+
}
65+
66+
withpytest.raises(ValueError):
67+
rest_helpers.flatten_query_params(obj)
68+
69+
70+
deftest_flatten_repeated_list():
71+
obj= {
72+
"a": {"b": {"c": [["e","f"], ["g","h"]]}},
73+
"d":"uvw",
74+
}
75+
76+
withpytest.raises(ValueError):
77+
rest_helpers.flatten_query_params(obj)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp