|
3 | 3 | import logging |
4 | 4 |
|
5 | 5 | from abc import ABC, abstractmethod |
6 | | -from collections.abc import AsyncIterable, Awaitable, Sequence |
7 | | -from typing import TYPE_CHECKING |
| 6 | +from collections.abc import AsyncIterable, Awaitable |
8 | 7 |
|
9 | 8 |
|
10 | 9 | try: |
11 | 10 | import grpc |
12 | 11 | import grpc.aio |
13 | | - |
14 | | - if TYPE_CHECKING: |
15 | | - from grpc.aio._typing import MetadataType |
16 | | - from grpc.aio import Metadata |
17 | 12 | except ImportError as e: |
18 | 13 | raise ImportError( |
19 | 14 | 'GrpcHandler requires grpcio and grpcio-tools to be installed. ' |
@@ -56,14 +51,16 @@ def build(self, context: grpc.aio.ServicerContext) -> ServerCallContext: |
56 | 51 | def _get_metadata_value( |
57 | 52 | context: grpc.aio.ServicerContext, key: str |
58 | 53 | ) -> list[str]: |
59 | | - md: MetadataType | None = context.invocation_metadata() |
60 | | - raw_values: list[str | bytes] = [] |
| 54 | + md = context.invocation_metadata() |
| 55 | + if md is None: |
| 56 | + return [] |
| 57 | + |
61 | 58 | lower_key = key.lower() |
62 | | - if isinstance(md, Metadata): |
63 | | - raw_values = md.get_all(lower_key) |
64 | | - elif isinstance(md, Sequence): |
65 | | - raw_values = [e for (k, e) in md if k.lower() == lower_key] |
66 | | - return [e if isinstance(e, str) else e.decode('utf-8') for e in raw_values] |
| 59 | + return [ |
| 60 | + e if isinstance(e, str) else e.decode('utf-8') |
| 61 | + for k, e in md |
| 62 | + if k.lower() == lower_key |
| 63 | + ] |
67 | 64 |
|
68 | 65 |
|
69 | 66 | class DefaultCallContextBuilder(CallContextBuilder): |
|
0 commit comments