Skip to content

Commit 5a49a53

Browse files
committed
refactor common code
1 parent 583b8ce commit 5a49a53

File tree

1 file changed

+36
-62
lines changed
  • google/api_core/operations_v1/transports

1 file changed

+36
-62
lines changed

google/api_core/operations_v1/transports/rest.py

Lines changed: 36 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -188,22 +188,7 @@ def _list_operations(
188188
"google.longrunning.Operations.ListOperations"
189189
]
190190

191-
# For backwards compatibility with protobuf 3.x 4.x
192-
# Remove once support for protobuf 3.x and 4.x is dropped
193-
# https://github.com/googleapis/python-api-core/issues/643
194-
if PROTOBUF_VERSION[0:2] in ["3.", "4."]:
195-
request_kwargs = json_format.MessageToDict(
196-
request,
197-
preserving_proto_field_name=True,
198-
including_default_value_fields=True, # type: ignore # backward compatibility
199-
)
200-
else:
201-
request_kwargs = json_format.MessageToDict(
202-
request,
203-
preserving_proto_field_name=True,
204-
always_print_fields_with_no_presence=True,
205-
)
206-
191+
request_kwargs = self._convert_protobuf_message_to_dict(request)
207192
transcoded_request = path_template.transcode(http_options, **request_kwargs)
208193

209194
uri = transcoded_request["uri"]
@@ -279,22 +264,7 @@ def _get_operation(
279264
"google.longrunning.Operations.GetOperation"
280265
]
281266

282-
# For backwards compatibility with protobuf 3.x 4.x
283-
# Remove once support for protobuf 3.x and 4.x is dropped
284-
# https://github.com/googleapis/python-api-core/issues/643
285-
if PROTOBUF_VERSION[0:2] in ["3.", "4."]:
286-
request_kwargs = json_format.MessageToDict(
287-
request,
288-
preserving_proto_field_name=True,
289-
including_default_value_fields=True, # type: ignore # backward compatibility
290-
)
291-
else:
292-
request_kwargs = json_format.MessageToDict(
293-
request,
294-
preserving_proto_field_name=True,
295-
always_print_fields_with_no_presence=True,
296-
)
297-
267+
request_kwargs = self._convert_protobuf_message_to_dict(request)
298268
transcoded_request = path_template.transcode(http_options, **request_kwargs)
299269

300270
uri = transcoded_request["uri"]
@@ -363,21 +333,7 @@ def _delete_operation(
363333
"google.longrunning.Operations.DeleteOperation"
364334
]
365335

366-
# For backwards compatibility with protobuf 3.x 4.x
367-
# Remove once support for protobuf 3.x and 4.x is dropped
368-
# https://github.com/googleapis/python-api-core/issues/643
369-
if PROTOBUF_VERSION[0:2] in ["3.", "4."]:
370-
request_kwargs = json_format.MessageToDict(
371-
request,
372-
preserving_proto_field_name=True,
373-
including_default_value_fields=True, # type: ignore # backward compatibility
374-
)
375-
else:
376-
request_kwargs = json_format.MessageToDict(
377-
request,
378-
preserving_proto_field_name=True,
379-
always_print_fields_with_no_presence=True,
380-
)
336+
request_kwargs = self._convert_protobuf_message_to_dict(request)
381337
transcoded_request = path_template.transcode(http_options, **request_kwargs)
382338

383339
uri = transcoded_request["uri"]
@@ -444,21 +400,7 @@ def _cancel_operation(
444400
"google.longrunning.Operations.CancelOperation"
445401
]
446402

447-
# For backwards compatibility with protobuf 3.x 4.x
448-
# Remove once support for protobuf 3.x and 4.x is dropped
449-
# https://github.com/googleapis/python-api-core/issues/643
450-
if PROTOBUF_VERSION[0:2] in ["3.", "4."]:
451-
request_kwargs = json_format.MessageToDict(
452-
request,
453-
preserving_proto_field_name=True,
454-
including_default_value_fields=True, # type: ignore # backward compatibility
455-
)
456-
else:
457-
request_kwargs = json_format.MessageToDict(
458-
request,
459-
preserving_proto_field_name=True,
460-
always_print_fields_with_no_presence=True,
461-
)
403+
request_kwargs = self._convert_protobuf_message_to_dict(request)
462404
transcoded_request = path_template.transcode(http_options, **request_kwargs)
463405

464406
# Jsonify the request body
@@ -499,6 +441,38 @@ def _cancel_operation(
499441

500442
return empty_pb2.Empty()
501443

444+
def _convert_protobuf_message_to_dict(
445+
self, message: google.protobuf.message.Message
446+
):
447+
r"""Converts protobuf message to a dictionary.
448+
449+
When the dictionary is encoded to JSON, it conforms to proto3 JSON spec.
450+
451+
Args:
452+
message(google.protobuf.message.Message): The protocol buffers message
453+
instance to serialize.
454+
455+
Returns:
456+
A dict representation of the protocol buffer message.
457+
"""
458+
# For backwards compatibility with protobuf 3.x 4.x
459+
# Remove once support for protobuf 3.x and 4.x is dropped
460+
# https://github.com/googleapis/python-api-core/issues/643
461+
if PROTOBUF_VERSION[0:2] in ["3.", "4."]:
462+
result = json_format.MessageToDict(
463+
message,
464+
preserving_proto_field_name=True,
465+
including_default_value_fields=True, # type: ignore # backward compatibility
466+
)
467+
else:
468+
result = json_format.MessageToDict(
469+
message,
470+
preserving_proto_field_name=True,
471+
always_print_fields_with_no_presence=True,
472+
)
473+
474+
return result
475+
502476
@property
503477
def list_operations(
504478
self,

0 commit comments

Comments
 (0)