def get_todo(request, context): """ Lists a todo based on the id of the todo Args: request(todo_pb2.Todo object): for getting id context(grpc.ServicerContext): for setting error code and details Returns: todo(todo_pb2.Todo object): returns a todo object with given id returns a empty todo object if id not found in the database Raises: sqlalchemy.orm.exc.NoResultFound: If no todo object is found with given id """ try: get_todo_ = session.query(Todo).filter(Todo.id == request.id).one() todo = todo_pb2.Todo(id=get_todo_.id, content=get_todo_.content, status=get_todo_.status) return todo except NoResultFound: msg = "No task found with the given id" context.set_details(msg) context.set_code(grpc.StatusCode.NOT_FOUND) return todo_pb2.Todo()
def edit_todo(stub, option): """ Edits an existing todo Args: stub: grpc stub option(str): Option to choose between editing content and status Returns: todo_pb2.Todo object Raises: grpc.RpcError: If content is more than 200 characters If id entered is not present in database """ edit_id = input("Please enter the id of the todo to edit: ") todo = todo_pb2.Todo(id=int(edit_id)) try: todo = stub.GetTodo(todo) if option == "2": edit_content = input("Please enter the new content for todo: ") todo.content = edit_content else: edit_status = input("1 for complete 0 for incomplete: ") todo.status = int(edit_status) try: edit_todo_response = stub.EditTodo(todo) print(edit_todo_response) except grpc.RpcError as e: print(e.details()) except grpc.RpcError as e: print(e.details())
def list_all_todos(): """ Lists all the todos from the databases Args: No args Returns: all_todos: A todo_pb2.AllTodos object """ all_todos = todo_pb2.AllTodos() todos = session.query(Todo).order_by(Todo.id).all() for todo in todos: all_todos.todo.append( todo_pb2.Todo(id=todo.id, content=todo.content, status=todo.status)) return all_todos
def add_todo(stub): """ Add a todo to the database Args: stub: a grpc stub Returns: todo_pb2.Todo object Raises: grpc.RpcError: If content is more than 200 characters """ content = input("Please enter the task todo: ") todo = todo_pb2.Todo(content=content) try: response = stub.AddTodo(todo) print(response) except grpc.RpcError as e: print(e.details())
def list_all_stream(): """ Lists all the todos from the databases Args: No args Returns: all_todos(list): A list of todo_pb2.Todo objects """ todos = session.query(Todo).order_by(Todo.id) all_todos = [] for todo in todos: all_todos.append( todo_pb2.Todo( id=todo.id, content=todo.content, status=todo.status, )) return all_todos
def remove_todo(stub): """ Removes an existing todo Args: stub: grpc stub option(str): Option to choose between editing content and status Returns: todo_pb2.Todo object Raises: grpc.RpcError: If id entered is not present in database """ del_id = input("Please enter the id of the task to delete: ") todo = todo_pb2.Todo(id=int(del_id)) try: resp = stub.RemoveTodo(todo) print(f"{resp.id} removed succesfully") except grpc.RpcError as e: print(e.details())