@@ -176,18 +176,20 @@ def get_field(request, field): |
176 | 176 | """Get the value of a field from a given dictionary. |
177 | 177 |
|
178 | 178 | Args: |
179 | | - request (dict): A dictionary object. |
| 179 | + request (dict | Message): A dictionary or a Message object. |
180 | 180 | field (str): The key to the request in dot notation. |
181 | 181 |
|
182 | 182 | Returns: |
183 | 183 | The value of the field. |
184 | 184 | """ |
185 | 185 | parts = field.split(".") |
186 | 186 | value = request |
| 187 | + |
187 | 188 | for part in parts: |
188 | 189 | if not isinstance(value, dict): |
189 | | - return |
190 | | - value = value.get(part) |
| 190 | + value = getattr(value, part, None) |
| 191 | + else: |
| 192 | + value = value.get(part) |
191 | 193 | if isinstance(value, dict): |
192 | 194 | return |
193 | 195 | return value |
@@ -197,19 +199,27 @@ def delete_field(request, field): |
197 | 199 | """Delete the value of a field from a given dictionary. |
198 | 200 |
|
199 | 201 | Args: |
200 | | - request (dict): A dictionary object. |
| 202 | + request (dict | Message): A dictionary object or a Message. |
201 | 203 | field (str): The key to the request in dot notation. |
202 | 204 | """ |
203 | 205 | parts = deque(field.split(".")) |
204 | 206 | while len(parts) > 1: |
205 | | - if not isinstance(request, dict): |
206 | | - return |
207 | 207 | part = parts.popleft() |
208 | | - request = request.get(part) |
| 208 | + if not isinstance(request, dict): |
| 209 | + if hasattr(request, part): |
| 210 | + request = getattr(request, part, None) |
| 211 | + else: |
| 212 | + return |
| 213 | + else: |
| 214 | + request = request.get(part) |
209 | 215 | part = parts.popleft() |
210 | 216 | if not isinstance(request, dict): |
211 | | - return |
212 | | - request.pop(part, None) |
| 217 | + if hasattr(request, part): |
| 218 | + request.ClearField(part) |
| 219 | + else: |
| 220 | + return |
| 221 | + else: |
| 222 | + request.pop(part, None) |
213 | 223 |
|
214 | 224 |
|
215 | 225 | def validate(tmpl, path): |
@@ -237,7 +247,7 @@ def validate(tmpl, path): |
237 | 247 | return True if re.match(pattern, path) is not None else False |
238 | 248 |
|
239 | 249 |
|
240 | | -def transcode(http_options, **request_kwargs): |
| 250 | +def transcode(http_options, message=None, **request_kwargs): |
241 | 251 | """Transcodes a grpc request pattern into a proper HTTP request following the rules outlined here, |
242 | 252 | https://github.com/googleapis/googleapis/blob/master/google/api/http.proto#L44-L312 |
243 | 253 |
|
@@ -248,18 +258,20 @@ def transcode(http_options, **request_kwargs): |
248 | 258 | 'body' (str): The body field name (optional) |
249 | 259 | (This is a simplified representation of the proto option `google.api.http`) |
250 | 260 |
|
| 261 | + message (Message) : A request object (optional) |
251 | 262 | request_kwargs (dict) : A dict representing the request object |
252 | 263 |
|
253 | 264 | Returns: |
254 | 265 | dict: The transcoded request with these keys, |
255 | 266 | 'method' (str) : The http method |
256 | 267 | 'uri' (str) : The expanded uri |
257 | | - 'body' (dict) : A dict representing the body (optional) |
258 | | - 'query_params' (dict) : A dict mapping query parameter variables and values |
| 268 | + 'body' (dict | Message) : A dict or a Message representing the body (optional) |
| 269 | + 'query_params' (dict | Message) : A dict or Message mapping query parameter variables and values |
259 | 270 |
|
260 | 271 | Raises: |
261 | 272 | ValueError: If the request does not match the given template. |
262 | 273 | """ |
| 274 | + transcoded_value = message or request_kwargs |
263 | 275 | for http_option in http_options: |
264 | 276 | request = {} |
265 | 277 |
|
@@ -268,27 +280,35 @@ def transcode(http_options, **request_kwargs): |
268 | 280 | path_fields = [ |
269 | 281 | match.group("name") for match in _VARIABLE_RE.finditer(uri_template) |
270 | 282 | ] |
271 | | - path_args = {field: get_field(request_kwargs, field) for field in path_fields} |
| 283 | + path_args = {field: get_field(transcoded_value, field) for field in path_fields} |
272 | 284 | request["uri"] = expand(uri_template, **path_args) |
273 | | - # Remove fields used in uri path from request |
274 | | - leftovers = copy.deepcopy(request_kwargs) |
275 | | - for path_field in path_fields: |
276 | | - delete_field(leftovers, path_field) |
277 | 285 |
|
278 | 286 | if not validate(uri_template, request["uri"]) or not all(path_args.values()): |
279 | 287 | continue |
280 | 288 |
|
| 289 | + # Remove fields used in uri path from request |
| 290 | + leftovers = copy.deepcopy(transcoded_value) |
| 291 | + for path_field in path_fields: |
| 292 | + delete_field(leftovers, path_field) |
| 293 | + |
281 | 294 | # Assign body and query params |
282 | 295 | body = http_option.get("body") |
283 | 296 |
|
284 | 297 | if body: |
285 | 298 | if body == "*": |
286 | 299 | request["body"] = leftovers |
287 | | - request["query_params"] = {} |
| 300 | + if message: |
| 301 | + request["query_params"] = message.__class__() |
| 302 | + else: |
| 303 | + request["query_params"] = {} |
288 | 304 | else: |
289 | 305 | try: |
290 | | - request["body"] = leftovers.pop(body) |
291 | | - except KeyError: |
| 306 | + if message: |
| 307 | + request["body"] = getattr(leftovers, body) |
| 308 | + delete_field(leftovers, body) |
| 309 | + else: |
| 310 | + request["body"] = leftovers.pop(body) |
| 311 | + except (KeyError, AttributeError): |
292 | 312 | continue |
293 | 313 | request["query_params"] = leftovers |
294 | 314 | else: |
|