Skip to content

Commit ca0d3d2

Browse files
committed
refactor: simplify metadata reading in grpc_handler.py
`Metadata` is a `Collection` so iterating over key-value tuples is possible, there are existing tests in `test_grpc_handler.py`.
1 parent c91d4fb commit ca0d3d2

1 file changed

Lines changed: 5 additions & 12 deletions

File tree

src/a2a/server/request_handlers/grpc_handler.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@
33
import logging
44

55
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
87

98

109
try:
1110
import grpc
1211
import grpc.aio
13-
14-
if TYPE_CHECKING:
15-
from grpc.aio._typing import MetadataType
16-
from grpc.aio import Metadata
1712
except ImportError as e:
1813
raise ImportError(
1914
'GrpcHandler requires grpcio and grpcio-tools to be installed. '
@@ -56,13 +51,11 @@ def build(self, context: grpc.aio.ServicerContext) -> ServerCallContext:
5651
def _get_metadata_value(
5752
context: grpc.aio.ServicerContext, key: str
5853
) -> 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 []
6157
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]
58+
raw_values = [e for (k, e) in md if k.lower() == lower_key]
6659
return [e if isinstance(e, str) else e.decode('utf-8') for e in raw_values]
6760

6861

0 commit comments

Comments
 (0)