diff options
author | Mehrdad Afshari <mmx@google.com> | 2018-07-26 11:18:48 -0700 |
---|---|---|
committer | Mehrdad Afshari <mmx@google.com> | 2018-07-26 13:05:21 -0700 |
commit | ae8ab0f74921046ffdcaff84e10b0a140b4ef2f8 (patch) | |
tree | 40619177527c23d0337ce33122d2d39acbcca1f0 | |
parent | 768e8ea89c9d27ea05b86cd83b0bfd18643bce12 (diff) |
Add SERVICE_NAME to reflection/health
4 files changed, 31 insertions, 20 deletions
diff --git a/src/python/grpcio_health_checking/grpc_health/v1/health.py b/src/python/grpcio_health_checking/grpc_health/v1/health.py index c8498104b1..0583659428 100644 --- a/src/python/grpcio_health_checking/grpc_health/v1/health.py +++ b/src/python/grpcio_health_checking/grpc_health/v1/health.py @@ -17,11 +17,13 @@ import threading import grpc -from grpc_health.v1 import health_pb2 -from grpc_health.v1 import health_pb2_grpc +from grpc_health.v1 import health_pb2 as _health_pb2 +from grpc_health.v1 import health_pb2_grpc as _health_pb2_grpc +SERVICE_NAME = _health_pb2.DESCRIPTOR.services_by_name['Health'].full_name -class HealthServicer(health_pb2_grpc.HealthServicer): + +class HealthServicer(_health_pb2_grpc.HealthServicer): """Servicer handling RPCs for service statuses.""" def __init__(self): @@ -33,9 +35,9 @@ class HealthServicer(health_pb2_grpc.HealthServicer): status = self._server_status.get(request.service) if status is None: context.set_code(grpc.StatusCode.NOT_FOUND) - return health_pb2.HealthCheckResponse() + return _health_pb2.HealthCheckResponse() else: - return health_pb2.HealthCheckResponse(status=status) + return _health_pb2.HealthCheckResponse(status=status) def set(self, service, status): """Sets the status of a service. diff --git a/src/python/grpcio_reflection/grpc_reflection/v1alpha/reflection.py b/src/python/grpcio_reflection/grpc_reflection/v1alpha/reflection.py index 0c564f10e5..6df1a36426 100644 --- a/src/python/grpcio_reflection/grpc_reflection/v1alpha/reflection.py +++ b/src/python/grpcio_reflection/grpc_reflection/v1alpha/reflection.py @@ -17,15 +17,17 @@ import grpc from google.protobuf import descriptor_pb2 from google.protobuf import descriptor_pool -from grpc_reflection.v1alpha import reflection_pb2 -from grpc_reflection.v1alpha import reflection_pb2_grpc +from grpc_reflection.v1alpha import reflection_pb2 as _reflection_pb2 +from grpc_reflection.v1alpha import reflection_pb2_grpc as _reflection_pb2_grpc _POOL = descriptor_pool.Default() +SERVICE_NAME = _reflection_pb2.DESCRIPTOR.services_by_name[ + 'ServerReflection'].full_name def _not_found_error(): - return reflection_pb2.ServerReflectionResponse( - error_response=reflection_pb2.ErrorResponse( + return _reflection_pb2.ServerReflectionResponse( + error_response=_reflection_pb2.ErrorResponse( error_code=grpc.StatusCode.NOT_FOUND.value[0], error_message=grpc.StatusCode.NOT_FOUND.value[1].encode(), )) @@ -35,12 +37,12 @@ def _file_descriptor_response(descriptor): proto = descriptor_pb2.FileDescriptorProto() descriptor.CopyToProto(proto) serialized_proto = proto.SerializeToString() - return reflection_pb2.ServerReflectionResponse( - file_descriptor_response=reflection_pb2.FileDescriptorResponse( + return _reflection_pb2.ServerReflectionResponse( + file_descriptor_response=_reflection_pb2.FileDescriptorResponse( file_descriptor_proto=(serialized_proto,)),) -class ReflectionServicer(reflection_pb2_grpc.ServerReflectionServicer): +class ReflectionServicer(_reflection_pb2_grpc.ServerReflectionServicer): """Servicer handling RPCs for service statuses.""" def __init__(self, service_names, pool=None): @@ -94,17 +96,17 @@ class ReflectionServicer(reflection_pb2_grpc.ServerReflectionServicer): except KeyError: return _not_found_error() else: - return reflection_pb2.ServerReflectionResponse( - all_extension_numbers_response=reflection_pb2. + return _reflection_pb2.ServerReflectionResponse( + all_extension_numbers_response=_reflection_pb2. ExtensionNumberResponse( base_type_name=message_descriptor.full_name, extension_number=extension_numbers)) def _list_services(self): - return reflection_pb2.ServerReflectionResponse( - list_services_response=reflection_pb2.ListServiceResponse( + return _reflection_pb2.ServerReflectionResponse( + list_services_response=_reflection_pb2.ListServiceResponse( service=[ - reflection_pb2.ServiceResponse(name=service_name) + _reflection_pb2.ServiceResponse(name=service_name) for service_name in self._service_names ])) @@ -126,8 +128,8 @@ class ReflectionServicer(reflection_pb2_grpc.ServerReflectionServicer): elif request.HasField('list_services'): yield self._list_services() else: - yield reflection_pb2.ServerReflectionResponse( - error_response=reflection_pb2.ErrorResponse( + yield _reflection_pb2.ServerReflectionResponse( + error_response=_reflection_pb2.ErrorResponse( error_code=grpc.StatusCode.INVALID_ARGUMENT.value[0], error_message=grpc.StatusCode.INVALID_ARGUMENT.value[1] .encode(), @@ -142,5 +144,5 @@ def enable_server_reflection(service_names, server, pool=None): server: grpc.Server to which reflection service will be added. pool: DescriptorPool object to use (descriptor_pool.Default() if None). """ - reflection_pb2_grpc.add_ServerReflectionServicer_to_server( + _reflection_pb2_grpc.add_ServerReflectionServicer_to_server( ReflectionServicer(service_names, pool=pool), server) diff --git a/src/python/grpcio_tests/tests/health_check/_health_servicer_test.py b/src/python/grpcio_tests/tests/health_check/_health_servicer_test.py index 3cbbb8de33..350b5eebe5 100644 --- a/src/python/grpcio_tests/tests/health_check/_health_servicer_test.py +++ b/src/python/grpcio_tests/tests/health_check/_health_servicer_test.py @@ -73,6 +73,9 @@ class HealthServicerTest(unittest.TestCase): self.assertEqual(grpc.StatusCode.NOT_FOUND, context.exception.code()) + def test_health_service_name(self): + self.assertEqual(health.SERVICE_NAME, 'grpc.health.v1.Health') + if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/src/python/grpcio_tests/tests/reflection/_reflection_servicer_test.py b/src/python/grpcio_tests/tests/reflection/_reflection_servicer_test.py index 7ffdba6a67..bcd9e14a38 100644 --- a/src/python/grpcio_tests/tests/reflection/_reflection_servicer_test.py +++ b/src/python/grpcio_tests/tests/reflection/_reflection_servicer_test.py @@ -171,6 +171,10 @@ class ReflectionServicerTest(unittest.TestCase): for name in _SERVICE_NAMES))),) self.assertSequenceEqual(expected_responses, responses) + def testReflectionServiceName(self): + self.assertEqual(reflection.SERVICE_NAME, + 'grpc.reflection.v1alpha.ServerReflection') + if __name__ == '__main__': unittest.main(verbosity=2) |