Use metadata types in the service context

Replace the signature to allow methods to use the metadata object.
Internally, they'll still wrap the data in a tuple, but the interface
makes it clear that the ``aio.Metadata()`` object is supported.

Remove the ``tuple(<metadata>)`` conversions done in the tests.
pull/23045/head
Mariano Anaya 5 years ago
parent 5a5a578446
commit a6bf093af8
  1. 12
      src/python/grpcio/grpc/_cython/_cygrpc/aio/server.pyx.pxi
  2. 16
      src/python/grpcio_tests/tests_aio/unit/metadata_test.py

@ -143,7 +143,7 @@ cdef class _ServicerContext:
self._loop) self._loop)
self._rpc_state.metadata_sent = True self._rpc_state.metadata_sent = True
async def send_initial_metadata(self, tuple metadata): async def send_initial_metadata(self, object metadata):
self._rpc_state.raise_for_termination() self._rpc_state.raise_for_termination()
if self._rpc_state.metadata_sent: if self._rpc_state.metadata_sent:
@ -151,7 +151,7 @@ cdef class _ServicerContext:
else: else:
await _send_initial_metadata( await _send_initial_metadata(
self._rpc_state, self._rpc_state,
_augment_metadata(metadata, self._rpc_state.compression_algorithm), _augment_metadata(tuple(metadata), self._rpc_state.compression_algorithm),
_EMPTY_FLAG, _EMPTY_FLAG,
self._loop self._loop
) )
@ -192,8 +192,8 @@ cdef class _ServicerContext:
async def abort_with_status(self, object status): async def abort_with_status(self, object status):
await self.abort(status.code, status.details, status.trailing_metadata) await self.abort(status.code, status.details, status.trailing_metadata)
def set_trailing_metadata(self, tuple metadata): def set_trailing_metadata(self, object metadata):
self._rpc_state.trailing_metadata = metadata self._rpc_state.trailing_metadata = tuple(metadata)
def invocation_metadata(self): def invocation_metadata(self):
return self._rpc_state.invocation_metadata() return self._rpc_state.invocation_metadata()
@ -233,13 +233,13 @@ cdef class _SyncServicerContext:
# Abort should raise an AbortError # Abort should raise an AbortError
future.exception() future.exception()
def send_initial_metadata(self, tuple metadata): def send_initial_metadata(self, object metadata):
future = asyncio.run_coroutine_threadsafe( future = asyncio.run_coroutine_threadsafe(
self._context.send_initial_metadata(metadata), self._context.send_initial_metadata(metadata),
self._loop) self._loop)
future.result() future.result()
def set_trailing_metadata(self, tuple metadata): def set_trailing_metadata(self, object metadata):
self._context.set_trailing_metadata(metadata) self._context.set_trailing_metadata(metadata)
def invocation_metadata(self): def invocation_metadata(self):

@ -104,13 +104,13 @@ class _TestGenericHandlerForMethods(grpc.GenericRpcHandler):
async def _test_server_to_client(request, context): async def _test_server_to_client(request, context):
assert _REQUEST == request assert _REQUEST == request
await context.send_initial_metadata( await context.send_initial_metadata(
tuple(_INITIAL_METADATA_FROM_SERVER_TO_CLIENT)) _INITIAL_METADATA_FROM_SERVER_TO_CLIENT)
return _RESPONSE return _RESPONSE
@staticmethod @staticmethod
async def _test_trailing_metadata(request, context): async def _test_trailing_metadata(request, context):
assert _REQUEST == request assert _REQUEST == request
context.set_trailing_metadata(tuple(_TRAILING_METADATA)) context.set_trailing_metadata(_TRAILING_METADATA)
return _RESPONSE return _RESPONSE
@staticmethod @staticmethod
@ -119,21 +119,21 @@ class _TestGenericHandlerForMethods(grpc.GenericRpcHandler):
assert _common.seen_metadata(_INITIAL_METADATA_FROM_CLIENT_TO_SERVER, assert _common.seen_metadata(_INITIAL_METADATA_FROM_CLIENT_TO_SERVER,
context.invocation_metadata()) context.invocation_metadata())
await context.send_initial_metadata( await context.send_initial_metadata(
tuple(_INITIAL_METADATA_FROM_SERVER_TO_CLIENT)) _INITIAL_METADATA_FROM_SERVER_TO_CLIENT)
yield _RESPONSE yield _RESPONSE
context.set_trailing_metadata(tuple(_TRAILING_METADATA)) context.set_trailing_metadata(_TRAILING_METADATA)
@staticmethod @staticmethod
async def _test_stream_unary(request_iterator, context): async def _test_stream_unary(request_iterator, context):
assert _common.seen_metadata(_INITIAL_METADATA_FROM_CLIENT_TO_SERVER, assert _common.seen_metadata(_INITIAL_METADATA_FROM_CLIENT_TO_SERVER,
context.invocation_metadata()) context.invocation_metadata())
await context.send_initial_metadata( await context.send_initial_metadata(
tuple(_INITIAL_METADATA_FROM_SERVER_TO_CLIENT)) _INITIAL_METADATA_FROM_SERVER_TO_CLIENT)
async for request in request_iterator: async for request in request_iterator:
assert _REQUEST == request assert _REQUEST == request
context.set_trailing_metadata(tuple(_TRAILING_METADATA)) context.set_trailing_metadata(_TRAILING_METADATA)
return _RESPONSE return _RESPONSE
@staticmethod @staticmethod
@ -141,13 +141,13 @@ class _TestGenericHandlerForMethods(grpc.GenericRpcHandler):
assert _common.seen_metadata(_INITIAL_METADATA_FROM_CLIENT_TO_SERVER, assert _common.seen_metadata(_INITIAL_METADATA_FROM_CLIENT_TO_SERVER,
context.invocation_metadata()) context.invocation_metadata())
await context.send_initial_metadata( await context.send_initial_metadata(
tuple(_INITIAL_METADATA_FROM_SERVER_TO_CLIENT)) _INITIAL_METADATA_FROM_SERVER_TO_CLIENT)
async for request in request_iterator: async for request in request_iterator:
assert _REQUEST == request assert _REQUEST == request
yield _RESPONSE yield _RESPONSE
context.set_trailing_metadata(tuple(_TRAILING_METADATA)) context.set_trailing_metadata(_TRAILING_METADATA)
def service(self, handler_call_details): def service(self, handler_call_details):
return self._routing_table.get(handler_call_details.method) return self._routing_table.get(handler_call_details.method)

Loading…
Cancel
Save