@@ -188,22 +188,7 @@ def _list_operations(
188
188
"google.longrunning.Operations.ListOperations"
189
189
]
190
190
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 )
207
192
transcoded_request = path_template .transcode (http_options , ** request_kwargs )
208
193
209
194
uri = transcoded_request ["uri" ]
@@ -279,22 +264,7 @@ def _get_operation(
279
264
"google.longrunning.Operations.GetOperation"
280
265
]
281
266
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 )
298
268
transcoded_request = path_template .transcode (http_options , ** request_kwargs )
299
269
300
270
uri = transcoded_request ["uri" ]
@@ -363,21 +333,7 @@ def _delete_operation(
363
333
"google.longrunning.Operations.DeleteOperation"
364
334
]
365
335
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 )
381
337
transcoded_request = path_template .transcode (http_options , ** request_kwargs )
382
338
383
339
uri = transcoded_request ["uri" ]
@@ -444,21 +400,7 @@ def _cancel_operation(
444
400
"google.longrunning.Operations.CancelOperation"
445
401
]
446
402
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 )
462
404
transcoded_request = path_template .transcode (http_options , ** request_kwargs )
463
405
464
406
# Jsonify the request body
@@ -499,6 +441,38 @@ def _cancel_operation(
499
441
500
442
return empty_pb2 .Empty ()
501
443
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
+
502
476
@property
503
477
def list_operations (
504
478
self ,
0 commit comments