29
29
from google .longrunning import operations_pb2 # type: ignore
30
30
from google .protobuf import empty_pb2 # type: ignore
31
31
from google .protobuf import json_format # type: ignore
32
+ import google .protobuf
33
+
32
34
import grpc
33
35
from .base import DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO , OperationsTransport
34
36
37
+ PROTOBUF_VERSION = google .protobuf .__version__
38
+
35
39
OptionalRetry = Union [retries .Retry , object ]
36
40
37
41
DEFAULT_CLIENT_INFO = gapic_v1 .client_info .ClientInfo (
@@ -184,11 +188,22 @@ def _list_operations(
184
188
"google.longrunning.Operations.ListOperations"
185
189
]
186
190
187
- request_kwargs = json_format .MessageToDict (
188
- request ,
189
- preserving_proto_field_name = True ,
190
- including_default_value_fields = True ,
191
- )
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
+
192
207
transcoded_request = path_template .transcode (http_options , ** request_kwargs )
193
208
194
209
uri = transcoded_request ["uri" ]
@@ -199,7 +214,6 @@ def _list_operations(
199
214
json_format .ParseDict (transcoded_request ["query_params" ], query_params_request )
200
215
query_params = json_format .MessageToDict (
201
216
query_params_request ,
202
- including_default_value_fields = False ,
203
217
preserving_proto_field_name = False ,
204
218
use_integers_for_enums = False ,
205
219
)
@@ -265,11 +279,22 @@ def _get_operation(
265
279
"google.longrunning.Operations.GetOperation"
266
280
]
267
281
268
- request_kwargs = json_format .MessageToDict (
269
- request ,
270
- preserving_proto_field_name = True ,
271
- including_default_value_fields = True ,
272
- )
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
+
273
298
transcoded_request = path_template .transcode (http_options , ** request_kwargs )
274
299
275
300
uri = transcoded_request ["uri" ]
@@ -280,7 +305,6 @@ def _get_operation(
280
305
json_format .ParseDict (transcoded_request ["query_params" ], query_params_request )
281
306
query_params = json_format .MessageToDict (
282
307
query_params_request ,
283
- including_default_value_fields = False ,
284
308
preserving_proto_field_name = False ,
285
309
use_integers_for_enums = False ,
286
310
)
@@ -339,11 +363,21 @@ def _delete_operation(
339
363
"google.longrunning.Operations.DeleteOperation"
340
364
]
341
365
342
- request_kwargs = json_format .MessageToDict (
343
- request ,
344
- preserving_proto_field_name = True ,
345
- including_default_value_fields = True ,
346
- )
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
+ )
347
381
transcoded_request = path_template .transcode (http_options , ** request_kwargs )
348
382
349
383
uri = transcoded_request ["uri" ]
@@ -354,7 +388,6 @@ def _delete_operation(
354
388
json_format .ParseDict (transcoded_request ["query_params" ], query_params_request )
355
389
query_params = json_format .MessageToDict (
356
390
query_params_request ,
357
- including_default_value_fields = False ,
358
391
preserving_proto_field_name = False ,
359
392
use_integers_for_enums = False ,
360
393
)
@@ -411,19 +444,28 @@ def _cancel_operation(
411
444
"google.longrunning.Operations.CancelOperation"
412
445
]
413
446
414
- request_kwargs = json_format .MessageToDict (
415
- request ,
416
- preserving_proto_field_name = True ,
417
- including_default_value_fields = True ,
418
- )
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
+ )
419
462
transcoded_request = path_template .transcode (http_options , ** request_kwargs )
420
463
421
464
# Jsonify the request body
422
465
body_request = operations_pb2 .CancelOperationRequest ()
423
466
json_format .ParseDict (transcoded_request ["body" ], body_request )
424
467
body = json_format .MessageToDict (
425
468
body_request ,
426
- including_default_value_fields = False ,
427
469
preserving_proto_field_name = False ,
428
470
use_integers_for_enums = False ,
429
471
)
@@ -435,7 +477,6 @@ def _cancel_operation(
435
477
json_format .ParseDict (transcoded_request ["query_params" ], query_params_request )
436
478
query_params = json_format .MessageToDict (
437
479
query_params_request ,
438
- including_default_value_fields = False ,
439
480
preserving_proto_field_name = False ,
440
481
use_integers_for_enums = False ,
441
482
)
0 commit comments