def amend_order(self, stub, id_, destination, date, add_products,
                    remove_products, is_paid, is_shipped):
        added_temp_list = []
        if not add_products:
            add_products = added_temp_list
        for product_added, stock in add_products:
            added_temp_list += [
                InventorySystem_pb2.Product(name=product_added,
                                            stock=int(stock))
            ]

        removed_temp_list = []
        if not remove_products:
            remove_products = removed_temp_list
        for product_removed, stock in remove_products:
            removed_temp_list += [
                InventorySystem_pb2.Product(name=product_removed,
                                            stock=int(stock))
            ]
        try:
            response = stub.amendOrder(
                InventorySystem_pb2.UpdateOrder(
                    id_=id_,
                    destination=destination,
                    date=date,
                    add_products=added_temp_list,
                    remove_products=removed_temp_list,
                    is_paid=is_paid,
                    is_shipped=is_shipped))
        except grpc.RpcError as e:
            print(e.details())
        else:
            print(str(response).split(" ", 1)[1][1:-2], end="\n\n")
 def updateProduct(self, request, context):
     description = request.description
     manufacturer = request.manufacturer
     product_updated = helpers.update_product((request.id_, request.name), description, manufacturer,
                     request.wholesale_cost, request.sale_cost, request.stock)
     if product_updated:
         return InventorySystem_pb2.UpdateResult(result="Product has been successfully updated!")
     return InventorySystem_pb2.UpdateResult(result="Failure to update product: make sure both ID and name are correct.")
 def addNewProduct(self, request, context):
     id_ = helpers.add_product(request.name, request.description, request.manufacturer, request.wholesale_cost, 
         request.sale_cost, request.stock)
     if id_ == -1:
         context.set_details("Invalid name '" + request.name + "': this product already exists!")
         context.set_code(grpc.StatusCode.ALREADY_EXISTS)
         return InventorySystem_pb2.ProductID() 
     return InventorySystem_pb2.ProductID(product_id=id_)
 def getUnshippedAndOrUnpaidOrders(self, request, context):
     orders = helpers.get_unshipped_unpaid_orders(request.query_unshipped, request.query_unpaid)
     for order in orders:
         products = []
         for product, stock in order["ordered_products"].items():
             products.append(InventorySystem_pb2.Product(name=product, stock=stock))
         yield InventorySystem_pb2.Order(id_=order["id"], destination=order["destination"], date=order["date"], products=products, 
                                     is_paid=order["is_paid"], is_shipped=order["is_shipped"])
 def getOrder(self, request, context):
     order, message = helpers.get_order(request.value)
     products = []
     if order == -1:
         context.set_details(message)
         context.set_code(grpc.StatusCode.NOT_FOUND)
         return InventorySystem_pb2.Order()
     for product, stock in order["ordered_products"].items():
         products.append(InventorySystem_pb2.Product(name=product, stock=stock))
     return InventorySystem_pb2.Order(id_=order["id"], destination=order["destination"], date=order["date"], products=products, 
                                     is_paid=order["is_paid"], is_shipped=order["is_shipped"])
 def createOrder(self, request, context):
     products = {}
     for product in request.products:
         products[product.name] = product.stock
     id_, message = helpers.create_order(request.destination, request.date, products, request.is_paid, request.is_shipped)
     if id_ == -1:
         context.set_details(message)
         context.set_code(grpc.StatusCode.NOT_FOUND)
         return InventorySystem_pb2.OrderID()
     elif id_ == -2:
         context.set_details(message)
         context.set_code(grpc.StatusCode.PERMISSION_DENIED)
         return InventorySystem_pb2.OrderID()
     return InventorySystem_pb2.OrderID(value=id_)
 def get_order(self, stub, order_id):
     try:
         response = stub.getOrder(
             InventorySystem_pb2.OrderID(value=order_id))
     except grpc.RpcError as e:
         print(e.details())
     else:
         print(response, end="\n\n")
 def get_product(self, stub, id_, name):
     try:
         response = stub.getProduct(
             InventorySystem_pb2.ProductQuery(product_id=id_,
                                              product_name=name))
     except grpc.RpcError as e:
         print(e.details())
     else:
         print(response)
 def getProduct(self, request, context):
     product = helpers.get_product(request.product_id, request.product_name)
     if product == -1 or product == -2:
         if product == -2:
             context.set_details("Must provide a product ID or name.")
         else:
             context.set_details("The product you are searching for does not exist!")
         context.set_code(grpc.StatusCode.UNKNOWN)
         return InventorySystem_pb2.Product() 
     return self.getProductRPCMessage(product)
    def create_order(self, stub, destination, date, product_list, is_paid,
                     is_shipped):
        temp_list = []
        for product, stock in product_list:
            temp_list += [
                InventorySystem_pb2.Product(name=product, stock=int(stock))
            ]

        try:
            response = stub.createOrder(
                InventorySystem_pb2.Order(destination=destination,
                                          date=date,
                                          products=temp_list,
                                          is_paid=is_paid,
                                          is_shipped=is_shipped))
        except grpc.RpcError as e:
            print(e.details())
        else:
            print("New order's ID:",
                  str(response).split(" ")[1][1:-2],
                  end="\n\n")
 def get_unshipped_and_or_unpaid_orders(self, stub, query_unshipped,
                                        query_unpaid):
     response = stub.getUnshippedAndOrUnpaidOrders(
         InventorySystem_pb2.UnshippedAndOrUnpaidQuery(
             query_unshipped=query_unshipped, query_unpaid=query_unpaid))
     if query_unshipped and query_unpaid:
         print("Unshipped and Unpaid Orders:", end="\n\n")
     if query_unshipped:
         print("Unshipped Orders:", end="\n\n")
     if query_unpaid:
         print("Unpaid Orders:", end="\n\n")
     for order in response:
         print(order)
 def update_product(self, stub, id_, name, description, manufacturer,
                    wholesale_cost, sale_cost, stock):
     try:
         response = stub.updateProduct(
             InventorySystem_pb2.Product(id_=id_,
                                         name=name,
                                         description=description,
                                         manufacturer=manufacturer,
                                         wholesale_cost=wholesale_cost,
                                         sale_cost=sale_cost,
                                         stock=stock))
     except grpc.RpcError as e:
         print(e.details())
     else:
         print(str(response).split(": ", 1)[1][1:-2], end="\n\n")
    def amendOrder(self, request, context):
        destination = request.destination
        date = request.date

        add_products = {}
        if request.add_products:
            for product in request.add_products:
                add_products[product.name] = product.stock
        remove_products = {}
        if request.remove_products:
            for product in request.remove_products:
                remove_products[product.name] = product.stock
        result = helpers.amend_order(request.id_, destination, date, add_products, remove_products, request.is_paid,
                request.is_shipped)
        return InventorySystem_pb2.UpdateResult(result=result)
 def add_product(self, stub, name, description, manufacturer,
                 wholesale_cost, sale_cost, stock):
     try:
         response = stub.addNewProduct(
             InventorySystem_pb2.Product(name=name,
                                         description=description,
                                         manufacturer=manufacturer,
                                         wholesale_cost=wholesale_cost,
                                         sale_cost=sale_cost,
                                         stock=stock))
     except grpc.RpcError as e:
         print(e.details())
     else:
         print("New product's ID:",
               str(response).split(" ")[1][1:-2],
               end="\n\n")
import InventorySystem_pb2_grpc
import xmlrpc.client
import time
import string
import random
import statistics

with grpc.insecure_channel('3.17.130.209:50051') as channel:
    stub = InventorySystem_pb2_grpc.InventorySystemStub(channel)

    manufacturers = [
        'Rio Dan', 'Jonah', 'jeff', 'CS dep', 'moravian food', 'Pepsi', 'Mac',
        'KFC M', 'NY street', 'Pepsico Inc.', '1st in the line Co.',
        'Kraft Foods Inc.', 'General Electric', 'fastFood', 'Tyson Foods Inc.'
    ]

    for i in range(500):
        name = ''.join(
            random.choices(string.ascii_uppercase + string.digits, k=20))
        start = time.monotonic()
        wholesale = random.uniform(0.10, 70.99)
        response = stub.addNewProduct(
            InventorySystem_pb2.Product(
                name=name,
                description=''.join(
                    random.choices(string.ascii_uppercase + string.digits,
                                   k=40)),
                manufacturer=random.choices(manufacturers)[0],
                wholesale_cost=wholesale,
                sale_cost=random.uniform(wholesale, wholesale + 14.99),
                stock=random.randint(100, 200)))
 def get_all_products(self, stub):
     response = stub.getAllProducts(InventorySystem_pb2.Empty())
     for product in response:
         print(product)
 def get_instock_items(self, stub):
     response = stub.getInStockProducts(InventorySystem_pb2.Empty())
     for product in response:
         print(product)
 def get_manufacturer_products(self, stub, manufacturer):
     response = stub.getManufacturerProducts(
         InventorySystem_pb2.Manufacturer(manufacturer=manufacturer))
     for product in response:
         print(product)
 def getProductRPCMessage(self, product):
     product = helpers.get_product_from_DB(product)
     return InventorySystem_pb2.Product(id_=product["id"], name=product["name"], description=product["description"],
                                             manufacturer=product["manufacturer"], wholesale_cost=product["wholesale_cost"],
                                             sale_cost=product["sale_cost"], stock=product["stock"]) 
 def getAllProducts(self, request, context):
     products = helpers.get_all_products()
     for product in products:
         yield InventorySystem_pb2.Product(id_=product["id"], name=product["name"], description=product["description"],
                                             manufacturer=product["manufacturer"], wholesale_cost=product["wholesale_cost"],
                                             sale_cost=product["sale_cost"], stock=product["stock"]) 
 def getInStockProducts(self, request, context):
     products = helpers.get_instock_products()
     for product in products:
         yield InventorySystem_pb2.ProductCount(product_id=product["id"], product_name=product["name"], 
                                                 quantity=product["stock"])