def FindAllExtensionNumbers(self, extendee_name: str) -> Iterable[int]: """ Find the field numbers used by all known extensions of `extendee_name`. This function implements a DescriptorDatabase interface, and is typically not called directly; prefer using a DescriptorPool instead. Args: extendee_name: fully-qualified name of the extended message type. Returns: A list of field numbers used by all known extensions. Raises: KeyError: The message type `extendee_name` was not found. """ if extendee_name in self._cached_extension_numbers: return self._cached_extension_numbers[extendee_name] request = ServerReflectionRequest( all_extension_numbers_of_type=extendee_name) response = self._do_one_request(request, key=extendee_name) all_extension_numbers: ExtensionNumberResponse = ( response.all_extension_numbers_response) numbers = list(all_extension_numbers.extension_number) self._cached_extension_numbers[extendee_name] = numbers return numbers
def FindFileByName(self, name: str) -> FileDescriptorProto: """ Find a file descriptor by file name. This function implements a DescriptorDatabase interface, and is typically not called directly; prefer using a DescriptorPool instead. Args: name: The name of the file. Typically this is a relative path ending in ".proto". Returns: A FileDescriptorProto for the file. Raises: KeyError: the file was not found. """ try: return super().FindFileByName(name) except KeyError: pass assert name not in self._known_files request = ServerReflectionRequest(file_by_filename=name) response = self._do_one_request(request, key=name) self._add_file_from_response(response.file_descriptor_response) return super().FindFileByName(name)
def FindFileContainingSymbol(self, symbol: str) -> FileDescriptorProto: """ Find the file containing the symbol, and return its file descriptor. The symbol should be a fully qualified name including the file descriptor's package and any containing messages. Some examples: * "some.package.name.Message" * "some.package.name.Message.NestedEnum" * "some.package.name.Message.some_field" This function implements a DescriptorDatabase interface, and is typically not called directly; prefer using a DescriptorPool instead. Args: symbol: The fully-qualified name of the symbol. Returns: FileDescriptorProto for the file containing the symbol. Raises: KeyError: the symbol was not found. """ try: return super().FindFileContainingSymbol(symbol) except KeyError: pass # Query the server request = ServerReflectionRequest(file_containing_symbol=symbol) response = self._do_one_request(request, key=symbol) self._add_file_from_response(response.file_descriptor_response) return super().FindFileContainingSymbol(symbol)
def get_services(self) -> Iterable[str]: """ Get list of full names of the registered services. Returns: A list of strings corresponding to the names of the services. """ request = ServerReflectionRequest(list_services="") response = self._do_one_request(request, key="") list_services: ListServiceResponse = response.list_services_response services: List[ServiceResponse] = list_services.service return [service.name for service in services]
async def get_available_services(channel) -> List[str]: """ Lists available services by name, exposed at target address :param channel: the channel to use :returns: List of services offered """ reflection_stub = ServerReflectionStub(channel) response = reflection_stub.ServerReflectionInfo( iter([ServerReflectionRequest(list_services="")])) service_names = [] async for res in response: service_names.append([ service.name for service in res.list_services_response.service if service.name != 'grpc.reflection.v1alpha.ServerReflection' ]) return service_names[0]
def FindFileContainingExtension( self, extendee_name: str, extension_number: int) -> FileDescriptorProto: """ Find the file which defines an extension for the given message type and field number. This function implements a DescriptorDatabase interface, and is typically not called directly; prefer using a DescriptorPool instead. Args: extendee_name: fully-qualified name of the extended message type. extension_number: the number of the extension field. Returns: FileDescriptorProto for the file containing the extension. Raises: KeyError: The message or the extension number were not found. """ try: return super().FindFileContainingExtension(extendee_name, extension_number) except KeyError: pass request = ServerReflectionRequest( file_containing_extension=ExtensionRequest( containing_type=extendee_name, extension_number=extension_number)) response = self._do_one_request(request, key=(extendee_name, extension_number)) file_desc = response.file_descriptor_response self._add_file_from_response(file_desc) return super().FindFileContainingExtension(extendee_name, extension_number)
def test_server(self): from protos.blueprintservice_pb2_grpc import BlueprintServiceStub from protos.testmessage_pb2 import TestMessage from protos.user_pb2 import User with insecure_channel("0.0.0.0:50051") as channel: response = BlueprintServiceStub(channel).UnaryUnary( TestMessage(user=User(name="unary_unary"))) self.assertEqual("unary_unary", response.user.name) self.assertEqual(1, self.app_process_request.call_count) self.assertEqual(1, self.app_process_response.call_count) self.assertEqual(1, self.blueprint_after_request.call_count) self.assertEqual(1, self.blueprint_before_request.call_count) for response in BlueprintServiceStub(channel).UnaryStream( TestMessage(user=User(name="unary_stream"))): pass self.assertEqual("unary_stream", response.user.name) self.assertEqual(2, self.app_process_request.call_count) self.assertEqual(1, self.app_process_response.call_count) self.assertEqual(1, self.blueprint_after_request.call_count) self.assertEqual(2, self.blueprint_before_request.call_count) response = BlueprintServiceStub(channel).StreamUnary( iter([TestMessage(user=User(name="stream_unary"))])) self.assertEqual("stream_unary", response.user.name) self.assertEqual(2, self.app_process_request.call_count) self.assertEqual(2, self.app_process_response.call_count) self.assertEqual(2, self.blueprint_after_request.call_count) self.assertEqual(2, self.blueprint_before_request.call_count) for response in BlueprintServiceStub(channel).StreamStream( iter([TestMessage(user=User(name="stream_stream"))])): pass self.assertEqual("stream_stream", response.user.name) self.assertEqual(2, self.app_process_request.call_count) self.assertEqual(2, self.app_process_response.call_count) self.assertEqual(2, self.blueprint_after_request.call_count) self.assertEqual(2, self.blueprint_before_request.call_count) self.assertEqual( 1, HealthStub(channel).Check(HealthCheckRequest()).status) for response in ServerReflectionStub(channel).ServerReflectionInfo( iter([ServerReflectionRequest(list_services="")])): self.assertEqual( { "service": [ { "name": "AppService" }, { "name": "BlueprintService" }, { "name": "grpc.health.v1.Health" }, { "name": "grpc.reflection.v1alpha.ServerReflection" }, ] }, MessageToDict(response.list_services_response), ) self.assertEqual(4, self.enter_context.call_count)