2929from google .longrunning import operations_pb2 # type: ignore
3030from google .protobuf import empty_pb2 # type: ignore
3131from google .protobuf import json_format # type: ignore
32+ import google .protobuf
33+
3234import grpc
3335from .base import DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO , OperationsTransport
3436
37+ PROTOBUF_VERSION = google .protobuf .__version__
38+
3539OptionalRetry = Union [retries .Retry , object ]
3640
3741DEFAULT_CLIENT_INFO = gapic_v1 .client_info .ClientInfo (
@@ -66,7 +70,7 @@ def __init__(
6670 self ,
6771 * ,
6872 host : str = "longrunning.googleapis.com" ,
69- credentials : ga_credentials .Credentials = None ,
73+ credentials : Optional [ ga_credentials .Credentials ] = None ,
7074 credentials_file : Optional [str ] = None ,
7175 scopes : Optional [Sequence [str ]] = None ,
7276 client_cert_source_for_mtls : Optional [Callable [[], Tuple [bytes , bytes ]]] = None ,
@@ -184,11 +188,7 @@ def _list_operations(
184188 "google.longrunning.Operations.ListOperations"
185189 ]
186190
187- request_kwargs = json_format .MessageToDict (
188- request ,
189- preserving_proto_field_name = True ,
190- including_default_value_fields = True ,
191- )
191+ request_kwargs = self ._convert_protobuf_message_to_dict (request )
192192 transcoded_request = path_template .transcode (http_options , ** request_kwargs )
193193
194194 uri = transcoded_request ["uri" ]
@@ -199,7 +199,6 @@ def _list_operations(
199199 json_format .ParseDict (transcoded_request ["query_params" ], query_params_request )
200200 query_params = json_format .MessageToDict (
201201 query_params_request ,
202- including_default_value_fields = False ,
203202 preserving_proto_field_name = False ,
204203 use_integers_for_enums = False ,
205204 )
@@ -265,11 +264,7 @@ def _get_operation(
265264 "google.longrunning.Operations.GetOperation"
266265 ]
267266
268- request_kwargs = json_format .MessageToDict (
269- request ,
270- preserving_proto_field_name = True ,
271- including_default_value_fields = True ,
272- )
267+ request_kwargs = self ._convert_protobuf_message_to_dict (request )
273268 transcoded_request = path_template .transcode (http_options , ** request_kwargs )
274269
275270 uri = transcoded_request ["uri" ]
@@ -280,7 +275,6 @@ def _get_operation(
280275 json_format .ParseDict (transcoded_request ["query_params" ], query_params_request )
281276 query_params = json_format .MessageToDict (
282277 query_params_request ,
283- including_default_value_fields = False ,
284278 preserving_proto_field_name = False ,
285279 use_integers_for_enums = False ,
286280 )
@@ -339,11 +333,7 @@ def _delete_operation(
339333 "google.longrunning.Operations.DeleteOperation"
340334 ]
341335
342- request_kwargs = json_format .MessageToDict (
343- request ,
344- preserving_proto_field_name = True ,
345- including_default_value_fields = True ,
346- )
336+ request_kwargs = self ._convert_protobuf_message_to_dict (request )
347337 transcoded_request = path_template .transcode (http_options , ** request_kwargs )
348338
349339 uri = transcoded_request ["uri" ]
@@ -354,7 +344,6 @@ def _delete_operation(
354344 json_format .ParseDict (transcoded_request ["query_params" ], query_params_request )
355345 query_params = json_format .MessageToDict (
356346 query_params_request ,
357- including_default_value_fields = False ,
358347 preserving_proto_field_name = False ,
359348 use_integers_for_enums = False ,
360349 )
@@ -411,19 +400,14 @@ def _cancel_operation(
411400 "google.longrunning.Operations.CancelOperation"
412401 ]
413402
414- request_kwargs = json_format .MessageToDict (
415- request ,
416- preserving_proto_field_name = True ,
417- including_default_value_fields = True ,
418- )
403+ request_kwargs = self ._convert_protobuf_message_to_dict (request )
419404 transcoded_request = path_template .transcode (http_options , ** request_kwargs )
420405
421406 # Jsonify the request body
422407 body_request = operations_pb2 .CancelOperationRequest ()
423408 json_format .ParseDict (transcoded_request ["body" ], body_request )
424409 body = json_format .MessageToDict (
425410 body_request ,
426- including_default_value_fields = False ,
427411 preserving_proto_field_name = False ,
428412 use_integers_for_enums = False ,
429413 )
@@ -435,7 +419,6 @@ def _cancel_operation(
435419 json_format .ParseDict (transcoded_request ["query_params" ], query_params_request )
436420 query_params = json_format .MessageToDict (
437421 query_params_request ,
438- including_default_value_fields = False ,
439422 preserving_proto_field_name = False ,
440423 use_integers_for_enums = False ,
441424 )
@@ -458,6 +441,38 @@ def _cancel_operation(
458441
459442 return empty_pb2 .Empty ()
460443
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+
461476 @property
462477 def list_operations (
463478 self ,
0 commit comments