Beispiel #1
0
 def GetUsers(self, request, context):
     users = session.query(User).all()
     userslist_pb = []
     for user in users:
         userslist_pb.append(
             todo_pb2.User(id=user.id, name=user.name, email=user.email))
     return todo_pb2.UsersList(users=userslist_pb)
Beispiel #2
0
 def list_todos_by_user(self, username):
     username = todo_pb2.User(name=username)
     todos = self.stub.ListTodoByUser(username)
     for todo in todos:
         if todo.status == todo_pb2.SUCCESS:
             print("todo with %s, %s, %s" %
                   (todo.id, todo.text, todo.user.name))
         else:
             print("todo not found with username: %s" % (username))
Beispiel #3
0
 def add_one_todo(self, input_action, username):
     todo = self.stub.AddTodo(
         todo_pb2.Todo(text=input_action,
                       user=todo_pb2.User(name=username),
                       isdone=False,
                       status=todo_pb2.SUCCESS))
     if todo.status == todo_pb2.FAILED:
         print("FAILED: todo not added!", todo)
     else:
         print("todo added with %s " % (todo.text))
Beispiel #4
0
 def CreateUser(self, request, context):
     user = User(name=request.name, email=request.name)
     try:
         session.add(user)
         session.commit()
     except Exception:
         logging.error("Didn't create user")
     finally:
         # if success returns with id else the request user sent
         return todo_pb2.User(**self._row2dict(user))
Beispiel #5
0
    def GetUser(self, request, context):
        user_id = request.id
        user = session.query(User).filter(User.id == user_id).first()
        user_pb2 = todo_pb2.User()

        if user:
            user_pb2.id = user.id
            user_pb2.name = user.name
            user_pb2.email = user.email

        return user_pb2
Beispiel #6
0
 def CreateTodo(self, request, context):
     todo = Todo(text=request.text,
                 user_id=request.user_id,
                 state=request.state)
     try:
         session.add(todo)
         session.commit()
     except Exception:
         logging.error("Didn't create todo")
     finally:
         # if success returns with id else the request user sent
         return todo_pb2.User(**self._row2dict(todo))
Beispiel #7
0
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)
Beispiel #8
0
def read_todo_database():
    """Reads the to-do database.
  Returns:
    The full contents of the to-do database as a sequence of
      todo_pb2.todos.
  """
    todo_list = []

    with open("todo_db.json") as todo_db_file:
        for item in json.load(todo_db_file):
            todo = todo_pb2.Todo(
                id=item["id"],
                text=item["text"],
                user=todo_pb2.User(name=item["user"]),
                isdone=item["isdone"],
            )
            todo_list.append(todo)

    return todo_list
Beispiel #9
0
 def ConvertTodoModelTogRPC(self, todo):
     todo = todo_pb2.Todo(text=todo.text,
                          id=todo.id,
                          user=todo_pb2.User(name=todo.user.name),
                          isdone=todo.isdone)
     return todo