def get_history(): print("Getting History") data = json.loads(request.data) username = data["username"] resp = check_logged_in(username) if not resp["status"] or not resp["logged_in"]: print("## ERROR ##") print("Not logged in") return {"status": False, "items": []} while True: try: channel = grpc.insecure_channel(get_customer_db_ip()) stub = customer_pb2_grpc.CustomerStub(channel) response = stub.GetHistory( customer_pb2.CheckLoginRequest(username=username)) break except grpc._channel._InactiveRpcError as grpc_exc: print("Server not available, trying a different one") if not response.status: print("## ERROR ##") print(response.error) return {"status": False, "items": []} ids = response.item_ids quants = response.quantities items = [[ids[i], quants[i]] for i in range(len(ids))] return {"status": True, "items": items}
def clear_shopping_cart(): print("Clearing shopping cart") data = json.loads(request.data) username = data["username"] resp = check_logged_in(username) if not resp["status"] or not resp["logged_in"]: print("## ERROR ##") print("Not logged in") return {"status": False} while True: try: channel = grpc.insecure_channel(get_customer_db_ip()) stub = customer_pb2_grpc.CustomerStub(channel) response = stub.UpdateCart( customer_pb2.UpdateCartRequest(username=username, add=True, key="shopping_cart_clear", item_id=0, quantity=0)) break except grpc._channel._InactiveRpcError as grpc_exc: print("Server not available, trying a different one") if not response.status: print("## ERROR ##") print(response.error) return {"status": response.status}
def run(): channel = grpc.insecure_channel('localhost:50052') stub = customer_pb2_grpc.CustomerStub(channel) create_customer(stub) filter = input("Filter to query the database: ")
def run(): channel = grpc.insecure_channel('localhost:50051') stub = customer_pb2_grpc.CustomerStub(channel) print("-------------- CreateCustomer --------------") customer_create_customer(stub) print("-------------- GetCustomers --------------") customer_get_customers(stub)
def display_shopping_cart(): print("Displaying shopping cart") data = json.loads(request.data) username = data["username"] resp = check_logged_in(username) if not resp["status"] or not resp["logged_in"]: print("## ERROR ##") print("Not logged in") return {"status": False, "items": []} # Get item_ids and quantities while True: try: channel = grpc.insecure_channel(get_customer_db_ip()) stub = customer_pb2_grpc.CustomerStub(channel) response = stub.GetShoppingCart( customer_pb2.CheckLoginRequest(username=username)) break except grpc._channel._InactiveRpcError as grpc_exc: print("Server not available, trying a different one") if not response.status: print("## ERROR ##") print(response.error) return {"status": False, "items": []} print(response.item_ids) print(response.quantities) # return {"status" : response.status} while True: try: channel = grpc.insecure_channel(get_product_db_ip()) stub = product_pb2_grpc.ProductStub(channel) items = [] for i in range(len(response.item_ids)): item_id = response.item_ids[i] quantity = response.quantities[i] response2 = stub.GetItemByID( product_pb2.GetItemByIDRequest(item_id=item_id)) if response2.status: items.append({ "name": response2.items[0].name, "quantity": quantity }) else: print("Unable to locate item") break except grpc._channel._InactiveRpcError as grpc_exc: print("Server not available, trying a different one") return {"status": True, "items": items}
def add_item_shopping_cart(): print("Adding items to shopping cart") data = json.loads(request.data) username = data["username"] resp = check_logged_in(username) if not resp["status"] or not resp["logged_in"]: print("## ERROR ##") print("Not logged in") return {"status": False} item_id = data["item_id"] quantity = data["quantity"] # Check sufficient quantity while True: try: channel = grpc.insecure_channel(get_product_db_ip()) stub = product_pb2_grpc.ProductStub(channel) response = stub.GetItemByID( product_pb2.GetItemByIDRequest(item_id=item_id)) break except grpc._channel._InactiveRpcError as grpc_exc: print("Server not available, trying a different one") if not response.status: print("## ERROR ##") print(response.error) return {"status": response.status} if quantity > response.items[0].quantity: print("## ERROR ##") print( f"Insufficient quantity available: {response.items[0].quantity} requested: {quantity}" ) return {"status": False} # Add item to shopping cart while True: try: channel = grpc.insecure_channel(get_customer_db_ip()) stub = customer_pb2_grpc.CustomerStub(channel) response = stub.UpdateCart( customer_pb2.UpdateCartRequest(username=username, add=True, key="shopping_cart", item_id=item_id, quantity=quantity)) break except grpc._channel._InactiveRpcError as grpc_exc: print("Server not available, trying a different one") if not response.status: print("## ERROR ##") print(response.error) return {"status": response.status}
def check_logged_in(username): while True: try: channel = grpc.insecure_channel(get_customer_db_ip()) stub = customer_pb2_grpc.CustomerStub(channel) response = stub.CheckLogin( customer_pb2.CheckLoginRequest(username=username)) break except grpc._channel._InactiveRpcError as grpc_exc: print("Server not available, trying a different one") if not response.status: print(response.error) return {"status": response.status, "logged_in": response.logged_in}
def leave_feedback(): print("Leaving Feedback") data = json.loads(request.data) username = data["username"] resp = check_logged_in(username) if not resp["status"] or not resp["logged_in"]: print("## ERROR ##") print("Not logged in") return {"status": False} item_id = data["item_id"] feedback_type = data["feedback"] # Check we've purchased this item before while True: try: channel = grpc.insecure_channel(get_customer_db_ip()) stub = customer_pb2_grpc.CustomerStub(channel) response = stub.UpdateCart( customer_pb2.UpdateCartRequest(username=username, add=True, key="feedback", item_id=item_id, quantity=0)) break except grpc._channel._InactiveRpcError as grpc_exc: print("Server not available, trying a different one") if not response.status: print("## ERROR ##") print(response.error) return {"status": response.status} while True: try: channel = grpc.insecure_channel(get_product_db_ip()) stub = product_pb2_grpc.ProductStub(channel) response = stub.LeaveFeedback( product_pb2.LeaveFeedbackRequest(feedback_type=feedback_type, item_id=item_id)) break except grpc._channel._InactiveRpcError as grpc_exc: print("Server not available, trying a different one") if not response.status: print("## ERROR ##") print(response.error) return {"status": response.status}
def logout(): print("logging out") data = json.loads(request.data) username = data["username"] while True: try: channel = grpc.insecure_channel(get_customer_db_ip()) stub = customer_pb2_grpc.CustomerStub(channel) response = stub.ChangeLogin( customer_pb2.ChangeLoginRequest( username=username, password="", # Not needed logging_in=False)) if not response.status: print("## ERROR ##") print(response.error) break except grpc._channel._InactiveRpcError as grpc_exc: print("Server not available, trying a different one") return {"status": response.status}
def create_user(): data = json.loads(request.data) name = data["name"] username = data["username"] password = data["password"] print(f"Creating new user {username}") while True: try: channel = grpc.insecure_channel(get_customer_db_ip()) stub = customer_pb2_grpc.CustomerStub(channel) response = stub.CreateUser( customer_pb2.CreateUserRequest(name=name, username=username, password=password)) if not response.status: print("## ERROR ##") print(response.error) break except grpc._channel._InactiveRpcError as grpc_exc: print("Server not available, trying a different one") return {"status": response.status}
import grpc import customer_pb2 import customer_pb2_grpc channel = grpc.insecure_channel('localhost:50051') stub = customer_pb2_grpc.CustomerStub(channel) def without_address(): request_message1 = customer_pb2.CustomerRequest(id=1, name='Vasya') response1 = stub.CreateCustomer(request_message1) print("Customer received: " + str(response1.id)) print("Customer received: " + str(response1.success)) def with_address(): address_message = customer_pb2.CustomerRequest.Address(street='Пресненская набережная', city='Москва', isShippingAddress=True) request_message2 = customer_pb2.CustomerRequest(id=2, name='Petya', addresses=[address_message]) response2 = stub.CreateCustomer(request_message2) print("Customer received: " + str(response2.id)) print("Customer received: " + str(response2.success))
def make_purchase(self, username, name, cc_number, cc_expiration): """Docstrings for service methods appear as documentation in the wsdl. # <b>What fun!</b> # @param username # @param name the purchaser # @param cc_number credit card number # @param cc_expiration expiration date of the card # @return confirmed purchase # """ print("Making Purchase") # print(name) # print(cc_number) # print(cc_expiration) # Look up the shopping cart # Get item_ids and quantities while True: try: channel = grpc.insecure_channel(get_customer_db_ip()) stub = customer_pb2_grpc.CustomerStub(channel) response = stub.GetShoppingCart( customer_pb2.CheckLoginRequest(username=username)) break except grpc._channel._InactiveRpcError as grpc_exc: print("Server not available, trying a different one") if not response.status: print("## ERROR ##") print(response.error) return u"failure" """ bool status = 1; repeated int32 item_ids = 2; repeated int32 quantities = 3; string error = 4; """ # Make the purchase while True: try: channel = grpc.insecure_channel(get_product_db_ip()) stub = product_pb2_grpc.ProductStub(channel) response = stub.MakePurchase( product_pb2.MakePurchaseRequest( item_ids=response.item_ids, quantities=response.quantities)) break except grpc._channel._InactiveRpcError as grpc_exc: print("Server not available, trying a different one") if not response.status: print("## ERROR ##") print(response.error) return u"failure" # Indicate to customer DB that we've made the purchase while True: try: channel = grpc.insecure_channel(get_customer_db_ip()) stub = customer_pb2_grpc.CustomerStub(channel) response = stub.MakePurchase( customer_pb2.CheckLoginRequest(username=username)) break except grpc._channel._InactiveRpcError as grpc_exc: print("Server not available, trying a different one") if not response.status: print("## ERROR ##") print(response.error) return u"success"