|
61 | 61 | except: |
62 | 62 | HAS_ASYNC_REST_IDENTITY_TRANSPORT = False |
63 | 63 |
|
| 64 | + _GRPC_VERSION = grpc.__version__ |
| 65 | + |
64 | 66 | # TODO: use async auth anon credentials by default once the minimum version of google-auth is upgraded. |
65 | 67 | # See related issue: https://github.com/googleapis/gapic-generator-python/issues/2107. |
66 | 68 | def async_anonymous_credentials(): |
@@ -366,11 +368,20 @@ def __init__(self, key, value): |
366 | 368 |
|
367 | 369 | async def _add_request_metadata(self, client_call_details): |
368 | 370 | if client_call_details.metadata is not None: |
| 371 | + # As of gRPC 1.75.0 and newer, |
369 | 372 | # https://grpc.github.io/grpc/python/grpc_asyncio.html#grpc.aio.Metadata |
370 | 373 | # Note that for async, `ClientCallDetails.metadata` is a mapping. |
371 | 374 | # Whereas for sync, `ClientCallDetails.metadata` is a list. |
372 | 375 | # https://grpc.github.io/grpc/python/glossary.html#term-metadata. |
373 | | - client_call_details.metadata[self._key] = self._value |
| 376 | + # Prior to gRPC 1.75.0, `ClientCallDetails.metadata` is a list |
| 377 | + # for both sync and async. |
| 378 | + grpc_major, grpc_minor = [ |
| 379 | + int(part) for part in _GRPC_VERSION.split(".")[0:2] |
| 380 | + ] |
| 381 | + if grpc_major == 1 and grpc_minor < 75: |
| 382 | + client_call_details.metadata.append((self._key, self._value)) |
| 383 | + else: |
| 384 | + client_call_details.metadata[self._key] = self._value |
374 | 385 | self.request_metadata = list(client_call_details.metadata) |
375 | 386 |
|
376 | 387 | async def intercept_unary_unary(self, continuation, client_call_details, request): |
|
0 commit comments