|
3 | 3 | from typing import Any, Dict, List, Optional, Tuple |
4 | 4 |
|
5 | 5 | try: |
6 | | - from google.api_core import rest_helpers |
7 | | - # Trigger fallback if rest_helpers is an older version missing 'transcode' |
8 | | - if not hasattr(rest_helpers, "transcode"): |
9 | | - raise ImportError |
10 | | -except ImportError: |
| 6 | + from google.api_core.rest_helpers import ( |
| 7 | + flatten_query_params, |
| 8 | + transcode_request, |
| 9 | + ) |
| 10 | +except ImportError: # pragma: NO COVER |
11 | 11 | # TODO: Remove these fallbacks when google-api-core >= 2.18.0 is the minimum required version. |
12 | 12 | import functools |
13 | 13 | import json |
14 | 14 | import operator |
15 | 15 | from google.protobuf import json_format # type: ignore |
16 | 16 | from google.api_core import path_template # type: ignore |
17 | 17 |
|
18 | | - class _FallbackRestHelpers: |
19 | | - @staticmethod |
20 | | - def flatten_query_params(obj, strict=False): |
21 | | - if obj is not None and not isinstance(obj, dict): |
22 | | - raise TypeError("flatten_query_params must be called with dict object") |
23 | | - return _FallbackRestHelpers._flatten(obj, key_path=[], strict=strict) |
24 | | - |
25 | | - @staticmethod |
26 | | - def _flatten(obj, key_path, strict=False): |
27 | | - if obj is None: |
28 | | - return [] |
29 | | - if isinstance(obj, dict): |
30 | | - return _FallbackRestHelpers._flatten_dict(obj, key_path=key_path, strict=strict) |
31 | | - if isinstance(obj, list): |
32 | | - return _FallbackRestHelpers._flatten_list(obj, key_path=key_path, strict=strict) |
33 | | - return _FallbackRestHelpers._flatten_value(obj, key_path=key_path, strict=strict) |
34 | | - |
35 | | - @staticmethod |
36 | | - def _is_primitive_value(obj): |
37 | | - if obj is None: |
38 | | - return False |
39 | | - if isinstance(obj, (list, dict)): |
40 | | - raise ValueError("query params may not contain repeated dicts or lists") |
41 | | - return True |
42 | | - |
43 | | - @staticmethod |
44 | | - def _flatten_value(obj, key_path, strict=False): |
45 | | - return [(".".join(key_path), _FallbackRestHelpers._canonicalize(obj, strict=strict))] |
46 | | - |
47 | | - @staticmethod |
48 | | - def _flatten_dict(obj, key_path, strict=False): |
49 | | - items = ( |
50 | | - _FallbackRestHelpers._flatten(value, key_path=key_path + [key], strict=strict) |
51 | | - for key, value in obj.items() |
52 | | - ) |
53 | | - return functools.reduce(operator.concat, items, []) |
54 | | - |
55 | | - @staticmethod |
56 | | - def _flatten_list(elems, key_path, strict=False): |
57 | | - items = ( |
58 | | - _FallbackRestHelpers._flatten_value(elem, key_path=key_path, strict=strict) |
59 | | - for elem in elems |
60 | | - if _FallbackRestHelpers._is_primitive_value(elem) |
| 18 | + def flatten_query_params(obj, strict=False): # pragma: NO COVER |
| 19 | + if obj is not None and not isinstance(obj, dict): |
| 20 | + raise TypeError("flatten_query_params must be called with dict object") |
| 21 | + return _flatten(obj, key_path=[], strict=strict) |
| 22 | + |
| 23 | + def _flatten(obj, key_path, strict=False): # pragma: NO COVER |
| 24 | + if obj is None: |
| 25 | + return [] |
| 26 | + if isinstance(obj, dict): |
| 27 | + return _flatten_dict(obj, key_path=key_path, strict=strict) |
| 28 | + if isinstance(obj, list): |
| 29 | + return _flatten_list(obj, key_path=key_path, strict=strict) |
| 30 | + return _flatten_value(obj, key_path=key_path, strict=strict) |
| 31 | + |
| 32 | + def _is_primitive_value(obj): # pragma: NO COVER |
| 33 | + if obj is None: |
| 34 | + return False |
| 35 | + if isinstance(obj, (list, dict)): |
| 36 | + raise ValueError("query params may not contain repeated dicts or lists") |
| 37 | + return True |
| 38 | + |
| 39 | + def _flatten_value(obj, key_path, strict=False): # pragma: NO COVER |
| 40 | + return [(".".join(key_path), _canonicalize(obj, strict=strict))] |
| 41 | + |
| 42 | + def _flatten_dict(obj, key_path, strict=False): # pragma: NO COVER |
| 43 | + items = ( |
| 44 | + _flatten(value, key_path=key_path + [key], strict=strict) |
| 45 | + for key, value in obj.items() |
| 46 | + ) |
| 47 | + return functools.reduce(operator.concat, items, []) |
| 48 | + |
| 49 | + def _flatten_list(elems, key_path, strict=False): # pragma: NO COVER |
| 50 | + items = ( |
| 51 | + _flatten_value(elem, key_path=key_path, strict=strict) |
| 52 | + for elem in elems |
| 53 | + if _is_primitive_value(elem) |
| 54 | + ) |
| 55 | + return functools.reduce(operator.concat, items, []) |
| 56 | + |
| 57 | + def _canonicalize(obj, strict=False): # pragma: NO COVER |
| 58 | + if strict: |
| 59 | + value = str(obj) |
| 60 | + if isinstance(obj, bool): |
| 61 | + value = value.lower() |
| 62 | + return value |
| 63 | + return obj |
| 64 | + |
| 65 | + def transcode_request( # pragma: NO COVER |
| 66 | + http_options: List[Dict[str, str]], |
| 67 | + request: Any, |
| 68 | + required_fields_default_values: Optional[Dict[str, Any]] = None, |
| 69 | + rest_numeric_enums: bool = False, |
| 70 | + ) -> Tuple[Dict[str, Any], Optional[str], Dict[str, Any]]: |
| 71 | + pb_request = getattr(request, "_pb", request) |
| 72 | + transcoded_request = path_template.transcode(http_options, pb_request) |
| 73 | + |
| 74 | + body_json = None |
| 75 | + if transcoded_request.get("body") is not None: |
| 76 | + body_json = json_format.MessageToJson( |
| 77 | + transcoded_request["body"], |
| 78 | + use_integers_for_enums=rest_numeric_enums, |
61 | 79 | ) |
62 | | - return functools.reduce(operator.concat, items, []) |
63 | | - |
64 | | - @staticmethod |
65 | | - def _canonicalize(obj, strict=False): |
66 | | - if strict: |
67 | | - value = str(obj) |
68 | | - if isinstance(obj, bool): |
69 | | - value = value.lower() |
70 | | - return value |
71 | | - return obj |
72 | | - |
73 | | - @staticmethod |
74 | | - def transcode( |
75 | | - http_options: List[Dict[str, str]], |
76 | | - request: Any, |
77 | | - required_fields_default_values: Optional[Dict[str, Any]] = None, |
78 | | - rest_numeric_enums: bool = False, |
79 | | - ) -> Tuple[Dict[str, Any], Optional[str], Dict[str, Any]]: |
80 | | - pb_request = getattr(request, "_pb", request) |
81 | | - transcoded_request = path_template.transcode(http_options, pb_request) |
82 | | - |
83 | | - body_json = None |
84 | | - if transcoded_request.get("body") is not None: |
85 | | - body_json = json_format.MessageToJson( |
86 | | - transcoded_request["body"], |
87 | | - use_integers_for_enums=rest_numeric_enums, |
88 | | - ) |
89 | | - |
90 | | - query_params_json = {} |
91 | | - if transcoded_request.get("query_params") is not None: |
92 | | - query_params_json = json.loads(json_format.MessageToJson( |
93 | | - transcoded_request["query_params"], |
94 | | - use_integers_for_enums=rest_numeric_enums, |
95 | | - )) |
96 | 80 |
|
97 | | - if required_fields_default_values: |
98 | | - for k, v in required_fields_default_values.items(): |
99 | | - if k not in query_params_json: |
100 | | - query_params_json[k] = v |
| 81 | + query_params_json = {} |
| 82 | + if transcoded_request.get("query_params") is not None: |
| 83 | + query_params_json = json.loads(json_format.MessageToJson( |
| 84 | + transcoded_request["query_params"], |
| 85 | + use_integers_for_enums=rest_numeric_enums, |
| 86 | + )) |
101 | 87 |
|
102 | | - if rest_numeric_enums: |
103 | | - query_params_json["$alt"] = "json;enum-encoding=int" |
| 88 | + if required_fields_default_values: |
| 89 | + for k, v in required_fields_default_values.items(): |
| 90 | + if k not in query_params_json: |
| 91 | + query_params_json[k] = v |
104 | 92 |
|
105 | | - return transcoded_request, body_json, query_params_json |
| 93 | + if rest_numeric_enums: |
| 94 | + query_params_json["$alt"] = "json;enum-encoding=int" |
106 | 95 |
|
107 | | - rest_helpers = _FallbackRestHelpers |
| 96 | + return transcoded_request, body_json, query_params_json |
0 commit comments