Exemplo n.º 1
0
 def GetByID(self, request, context):
     account = getUserByID(request.id)
     if account:
         return accounts_pb2.Account(id=str(account['id']),
                                     name=str(account['name']),
                                     email=str(account['email']))
     context.set_code(grpc.StatusCode.NOT_FOUND)
Exemplo n.º 2
0
 def AuthenticateByEmail(self, request, context):
     user = getUserByEmailAndPassword(request.email, request.password)
     if user:
         print("Authenticated: {}".format(request.email))
         return accounts_pb2.Account(id=str(user['id']),
                                     name=str(user['name']),
                                     email=str(user['email']))
     context.set_code(grpc.StatusCode.UNAUTHENTICATED)
Exemplo n.º 3
0
    def List(self, request, context):
        serialized_accounts = []
        for user in USERS:
            serialized_accounts.append(
                accounts_pb2.Account(id=str(user['id']),
                                     name=str(user['name']),
                                     email=str(user['email'])))

        return accounts_pb2.ListAccountsResponse(accounts=serialized_accounts)
Exemplo n.º 4
0
    def Create(self, request, context):
        # Check to see if they already exist:
        if getUserByEmail(request.account.email):
            context.set_code(grpc.StatusCode.ALREADY_EXISTS)
            return

        USERS.append({
            "id": random.randint(2, 100),
            "name": request.account.name,
            "email": request.account.email,
            "password": request.password
        })
        user = getUserByEmail(request.account.email)
        print("User Created: {}".format(user['email']))
        return accounts_pb2.Account(id=str(user['id']),
                                    name=str(user['name']),
                                    email=str(user['email']))
Exemplo n.º 5
0
    def Update(self, request, context):
        # Check to see if they already exist:
        user = getUserByID(request.id)
        if not user:
            context.set_code(grpc.StatusCode.NOT_FOUND)
            return
        if request.account.name:
            user["name"] = request.account.name
        if request.account.email:
            user["email"] = request.account.email
        if request.password:
            user["password"] = request.password

        user = getUserByEmail(request.account.email)
        print("User Updated: {}".format(user['id']))
        return accounts_pb2.Account(id=str(user['id']),
                                    name=str(user['name']),
                                    email=str(user['email']))
Exemplo n.º 6
0
    def put(self, id):
        """Update a specific account."""
        params = request.get_json(force=True)
        email = params.get('email', None)
        name = params.get('name', None)
        password = params.get('password', None)

        with grpc.insecure_channel(HOST + ':' + PORT) as channel:
            stub = accounts_pb2_grpc.AccountServiceStub(channel)
            try:
                account = accounts_pb2.Account(name=name, email=email)

                updateAccountRequest = accounts_pb2.UpdateAccountRequest(
                    id=id, account=account, password=password)
                account = stub.Update(updateAccountRequest)
            except Exception as e:
                print(e)
                raise
            return jsonify(json.loads(MessageToJson(account)))
Exemplo n.º 7
0
    def post(self):
        """Signup API endpoint"""
        params = request.get_json()
        email = params.get('email', None)
        name = params.get('name', None)
        password = params.get('password', None)

        with grpc.insecure_channel(HOST + ':' + PORT) as channel:
            stub = accounts_pb2_grpc.AccountServiceStub(channel)
            try:
                account = accounts_pb2.Account(name="Leslie Knope",
                                               email='*****@*****.**')

                createAccountRequest = accounts_pb2.CreateAccountRequest(
                    account=account, password='******')
                account = stub.Create(createAccountRequest)
            except Exception as e:
                print(e)
                raise
            return jsonify(json.loads(MessageToJson(account)))
Exemplo n.º 8
0
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('0.0.0.0:22222') as channel:
        stub = accounts_pb2_grpc.AccountServiceStub(channel)

        # Test authentication.
        try:
            response = stub.AuthenticateByEmail(accounts_pb2.AuthenticateByEmailRequest(email='*****@*****.**', password='******'))
            print("Account ID received: " + response.id)
        except grpc.RpcError as e:
            status_code = e.code()
            if status_code.name == "UNAUTHENTICATED":
                print("Sorry you are UNAUTHENTICATED")
            elif status_code.name == "UNAVAILABLE":
                print("Server is unavailable.")
            else:
                print(e)

        print('-'*20)
        print("LIST:")

        # Test the list endpoint.
        response = stub.List(accounts_pb2.ListAccountsRequest(page_size=1, page_token="1"))
        for account in response.accounts:
            print(account)

        print('-'*20)
        print("CREATE:")

        account = accounts_pb2.Account(
            name="Leslie Knope",
            email='*****@*****.**'
        )

        createAccountRequest = accounts_pb2.CreateAccountRequest(
            account=account,
            password='******'
        )

        # Test the list endpoint.
        response = stub.Create(createAccountRequest)
        print(response)

        print('-'*20)
        print("LIST:")

        # Test the list endpoint.
        response = stub.List(accounts_pb2.ListAccountsRequest(page_size=1, page_token="1"))
        id_to_update = None
        for account in response.accounts:
            print(account)
            id_to_update = account.id

        print('-'*20)
        print("Update:")

        account = accounts_pb2.Account(
            name="Andy Dwyr",
            email='*****@*****.**'
        )

        updateAccountRequest = accounts_pb2.UpdateAccountRequest(
            id=id_to_update,
            account=account,
            password='******',
        )

        # Test the list endpoint.
        response = stub.Update(updateAccountRequest)
        print(response)

        print('-'*20)
        print("LIST:")

        # Test the list endpoint.
        response = stub.List(accounts_pb2.ListAccountsRequest(page_size=1, page_token="1"))
        id_to_update = None
        for account in response.accounts:
            print(account)
            id_to_update = account.id

        print('-'*20)
        print("Delete:")

        deleteAccountRequest = accounts_pb2.DeleteAccountRequest(
            id=id_to_update,
        )

        # Test the list endpoint.
        response = stub.Delete(deleteAccountRequest)
        print(response)
        print('-'*20)
        print("LIST:")

        # Test the list endpoint.
        response = stub.List(accounts_pb2.ListAccountsRequest(page_size=1, page_token="1"))
        id_to_update = None
        for account in response.accounts:
            print(account)
            id_to_update = account.id

        print('-'*20)