def service(self) -> purerpc.Service: service_obj = purerpc.Service("Greeter") service_obj.add_method( "SayHello", self.SayHello, purerpc.RPCSignature( purerpc.Cardinality.UNARY_UNARY, generated.greeter_pb2.HelloRequest, generated.greeter_pb2.HelloReply, )) service_obj.add_method( "SayHelloGoodbye", self.SayHelloGoodbye, purerpc.RPCSignature( purerpc.Cardinality.UNARY_STREAM, generated.greeter_pb2.HelloRequest, generated.greeter_pb2.HelloReply, )) service_obj.add_method( "SayHelloToMany", self.SayHelloToMany, purerpc.RPCSignature( purerpc.Cardinality.STREAM_STREAM, generated.greeter_pb2.HelloRequest, generated.greeter_pb2.HelloReply, )) service_obj.add_method( "SayHelloToManyAtOnce", self.SayHelloToManyAtOnce, purerpc.RPCSignature( purerpc.Cardinality.STREAM_UNARY, generated.greeter_pb2.HelloRequest, generated.greeter_pb2.HelloReply, )) return service_obj
def service(self) -> purerpc.Service: service_obj = purerpc.Service("challenges.Challenge2") service_obj.add_method( "create", self.create, purerpc.RPCSignature( purerpc.Cardinality.UNARY_UNARY, api_pb2.ChallengeRequest2, api_pb2.ChallengeResponse, )) return service_obj
def purerpc_server_wrong_method_name_port(greeter_pb2): service = purerpc.Service("Greeter") @service.rpc("SomeOtherMethod") async def say_hello( message: greeter_pb2.HelloRequest) -> greeter_pb2.HelloReply: return greeter_pb2.HelloReply(message="Hello, " + message.name) with run_purerpc_service_in_process(service) as port: yield port
def purerpc_server_wrong_service_name_port(greeter_pb2): service = purerpc.Service("some_package.SomeWrongServiceName") @service.rpc("SayHello") async def say_hello( message: greeter_pb2.HelloRequest) -> greeter_pb2.HelloReply: return greeter_pb2.HelloReply(message="Hello, " + message.name) with run_purerpc_service_in_process(service) as port: yield port
def purerpc_port(greeter_pb2): service = purerpc.Service("Greeter") @service.rpc("SayHello") async def say_hello( message: greeter_pb2.HelloRequest) -> greeter_pb2.HelloReply: status_code_tuple = pickle.loads(base64.b64decode(message.name)) raise status_code_tuple[0](status_code_tuple[2]) with run_purerpc_service_in_process(service) as port: yield port
def purerpc_simple_greeter_port(greeter_pb2): service = purerpc.Service("Greeter") @service.rpc("SayHello") async def say_hello( message: greeter_pb2.HelloRequest) -> greeter_pb2.HelloReply: return greeter_pb2.HelloReply(message="Hello, " + message.name) @service.rpc("SayHelloGoodbye") @async_generator async def say_hello_goodbye( message: greeter_pb2.HelloRequest ) -> purerpc.Stream[greeter_pb2.HelloReply]: await yield_(greeter_pb2.HelloReply(message="Hello, " + message.name)) await yield_(greeter_pb2.HelloReply(message="Goodbye, " + message.name) ) @service.rpc("SayHelloToManyAtOnce") async def say_hello_to_many_at_once( messages: purerpc.Stream[greeter_pb2.HelloRequest] ) -> greeter_pb2.HelloReply: names = [] async for message in messages: names.append(message.name) return greeter_pb2.HelloReply(message="Hello, " + ', '.join(names)) @service.rpc("SayHelloToMany") @async_generator async def say_hello_to_many( messages: purerpc.Stream[greeter_pb2.HelloRequest] ) -> purerpc.Stream[greeter_pb2.HelloReply]: async for message in messages: await yield_( greeter_pb2.HelloReply(message="Hello, " + message.name)) with run_purerpc_service_in_process(service) as port: yield port
def service(self) -> purerpc.Service: service_obj = purerpc.Service("challenges.Challenge") service_obj.add_method( "create", self.create, purerpc.RPCSignature( purerpc.Cardinality.UNARY_UNARY, api_pb2.ChallengeRequest, api_pb2.ChallengeResponse, )) service_obj.add_method( "bulk_create", self.bulk_create, purerpc.RPCSignature( purerpc.Cardinality.STREAM_STREAM, api_pb2.ChallengeRequest, api_pb2.ChallengeResponse, )) service_obj.add_method( "list", self.list, purerpc.RPCSignature( purerpc.Cardinality.UNARY_STREAM, google.protobuf.empty_pb2.Empty, api_pb2.ChallengeResponse, )) return service_obj
def dummy_server_port(): with run_purerpc_service_in_process(purerpc.Service("Greeter")) as port: yield port