def run(): # NOTE(gRPC Python Team): .close() is possible on a channel and should be # used in circumstances in which the with statement does not fit the needs # of the code. with grpc.insecure_channel('localhost:50051') as channel: stub = todo_pb2_grpc.TodoServiceStub(channel) response = stub.AddTodo(todo_pb2.Todo(body='Hello World')) print("Greeter client received: " + response.body)
def run(): channel = grpc.insecure_channel('localhost:50051') stub = todo_pb2_grpc.TodoServiceStub(channel) response = stub.CreateUser( todo_pb2.User(name='first user', email='*****@*****.**')) print("create user\n", response) if response.status.status == 1: user_id = response.user.id response = stub.GetUser(todo_pb2.User(id=user_id)) print("get user\n", response) response = stub.CreateTodo( todo_pb2.Todo(user_id=user_id, details='first todo', status=1)) print("create todo\n", response) if response.status.status == 1: todo_id = response.todo.id response = stub.GetTodo(todo_pb2.Todo(id=todo_id)) print("get todo\n", response) response = stub.GetUserTodos(todo_pb2.User(id=user_id)) print("get user todos\n", response) response = stub.CreateTodo( todo_pb2.Todo(user_id=user_id, details='second todo', status=1)) print("create second todo\n", response) if response.status.status == 1: todo_id = response.todo.id response = stub.GetTodo(todo_pb2.Todo(id=todo_id)) print("get second todo\n", response) response = stub.GetUserTodos(todo_pb2.User(id=user_id)) print("get user todos\n", response) response = stub.DeleteTodo( todo_pb2.Todo(id=todo_id, user_id=user_id)) print("delete second todo\n", response) response = stub.GetTodo( todo_pb2.Todo(id=todo_id, user_id=user_id)) print("get second todo\n", response) response = stub.GetUserTodos(todo_pb2.User(id=user_id)) print("get user todos\n", response) response = stub.DeleteUser(todo_pb2.User(id=user_id)) print("delete user\n", response) response = stub.GetUser(todo_pb2.User(id=user_id)) print("get user\n", response)
log_format = '%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s' logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=log_format, datefmt="%Y-%m-%d %H:%M:%S") MAX_MESSAGE_LENGTH = 4 * 1024 * 1024 * 10 options = [('grpc.max_send_message_length', MAX_MESSAGE_LENGTH), ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH)] channel = grpc.insecure_channel( 'localhost:50051', options=options, ) stub = todo_pb2_grpc.TodoServiceStub(channel) def send_email(subject, body): logging.info("Stub for sending mail.") def get_todo(todo_id): try: stub.GetTodo(todo_pb2.Todo(id=todo_id)) except grpc.RpcError as e: error_message = e.details() status_code = e.code() logging.error(error_message) send_email(subject="Todo - {} - StatusCode: {}".format( "get_todo", status_code.name),