Skip to content

Commit

Permalink
refactor common code
Browse files Browse the repository at this point in the history
  • Loading branch information
parthea committed Jun 11, 2024
1 parent 583b8ce commit 5a49a53
Showing 1 changed file with 36 additions and 62 deletions.
98 changes: 36 additions & 62 deletions google/api_core/operations_v1/transports/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,7 @@ def _list_operations(
"google.longrunning.Operations.ListOperations"
]

# For backwards compatibility with protobuf 3.x 4.x
# Remove once support for protobuf 3.x and 4.x is dropped
# https://github.com/googleapis/python-api-core/issues/643
if PROTOBUF_VERSION[0:2] in ["3.", "4."]:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
including_default_value_fields=True, # type: ignore # backward compatibility
)
else:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
always_print_fields_with_no_presence=True,
)

request_kwargs = self._convert_protobuf_message_to_dict(request)
transcoded_request = path_template.transcode(http_options, **request_kwargs)

uri = transcoded_request["uri"]
Expand Down Expand Up @@ -279,22 +264,7 @@ def _get_operation(
"google.longrunning.Operations.GetOperation"
]

# For backwards compatibility with protobuf 3.x 4.x
# Remove once support for protobuf 3.x and 4.x is dropped
# https://github.com/googleapis/python-api-core/issues/643
if PROTOBUF_VERSION[0:2] in ["3.", "4."]:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
including_default_value_fields=True, # type: ignore # backward compatibility
)
else:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
always_print_fields_with_no_presence=True,
)

request_kwargs = self._convert_protobuf_message_to_dict(request)
transcoded_request = path_template.transcode(http_options, **request_kwargs)

uri = transcoded_request["uri"]
Expand Down Expand Up @@ -363,21 +333,7 @@ def _delete_operation(
"google.longrunning.Operations.DeleteOperation"
]

# For backwards compatibility with protobuf 3.x 4.x
# Remove once support for protobuf 3.x and 4.x is dropped
# https://github.com/googleapis/python-api-core/issues/643
if PROTOBUF_VERSION[0:2] in ["3.", "4."]:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
including_default_value_fields=True, # type: ignore # backward compatibility
)
else:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
always_print_fields_with_no_presence=True,
)
request_kwargs = self._convert_protobuf_message_to_dict(request)
transcoded_request = path_template.transcode(http_options, **request_kwargs)

uri = transcoded_request["uri"]
Expand Down Expand Up @@ -444,21 +400,7 @@ def _cancel_operation(
"google.longrunning.Operations.CancelOperation"
]

# For backwards compatibility with protobuf 3.x 4.x
# Remove once support for protobuf 3.x and 4.x is dropped
# https://github.com/googleapis/python-api-core/issues/643
if PROTOBUF_VERSION[0:2] in ["3.", "4."]:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
including_default_value_fields=True, # type: ignore # backward compatibility
)
else:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
always_print_fields_with_no_presence=True,
)
request_kwargs = self._convert_protobuf_message_to_dict(request)
transcoded_request = path_template.transcode(http_options, **request_kwargs)

# Jsonify the request body
Expand Down Expand Up @@ -499,6 +441,38 @@ def _cancel_operation(

return empty_pb2.Empty()

def _convert_protobuf_message_to_dict(
self, message: google.protobuf.message.Message
):
r"""Converts protobuf message to a dictionary.
When the dictionary is encoded to JSON, it conforms to proto3 JSON spec.
Args:
message(google.protobuf.message.Message): The protocol buffers message
instance to serialize.
Returns:
A dict representation of the protocol buffer message.
"""
# For backwards compatibility with protobuf 3.x 4.x
# Remove once support for protobuf 3.x and 4.x is dropped
# https://github.com/googleapis/python-api-core/issues/643
if PROTOBUF_VERSION[0:2] in ["3.", "4."]:
result = json_format.MessageToDict(
message,
preserving_proto_field_name=True,
including_default_value_fields=True, # type: ignore # backward compatibility
)
else:
result = json_format.MessageToDict(
message,
preserving_proto_field_name=True,
always_print_fields_with_no_presence=True,
)

return result

@property
def list_operations(
self,
Expand Down

0 comments on commit 5a49a53

Please sign in to comment.