Rev 100 | Rev 384 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 25-Mar-2010@author: ashish'''import urlparsefrom shop2020.thriftpy.config.ttypes import ConfigExceptionfrom thrift.transport import TSocketfrom thrift.transport.TTransport import TFramedTransportfrom thrift.protocol.TBinaryProtocol import TBinaryProtocolfrom shop2020.thriftpy.config import Configurationfrom shop2020.thriftpy.model.v1.catalog import InventoryServicefrom shop2020.thriftpy.model.v1.catalog.ttypes import Item, status, Warehousefrom shop2020.config.client.ConfigClient import ConfigClientimport datetimefrom shop2020.utils.Utils import to_java_dateclass InventoryClient:file_path = '/tmp/config.properties'def __init__(self):config_client = ConfigClient()self.host = config_client.get_property("catalog_service_server_host")self.port = config_client.get_property("catalog_service_server_port")self.start_client()def start_client(self):try:self.transport = TSocket.TSocket(self.host, self.port)self.transport = TFramedTransport(self.transport)self.protocol = TBinaryProtocol(self.transport)self.client = InventoryService.Client(self.protocol)self.transport.open()except:raise ConfigException(101, 'unable to load the client')def add_item(self, item):return self.client.addItem(item)def add_warehouse(self, warehouse):return self.client.addWarehouse(warehouse)def update_inventory(self, item_id, warehouse_id, quantity, timestamp):return self.client.updateInventory(item_id, warehouse_id, quantity, timestamp)def get_item(self, item_id):return self.client.getItem(item_id)def get_all_active_items(self):return self.client.getAllItems(True)def get_all_active_items_by_status(self, status):return self.client.getAllItemsByStatus(status)def get_item_inventory(self, item_id):return self.client.getItemInventory(item_id)def get_all_warehouses(self, status):return self.client.getAllWarehouses(status)def get_all_warehouses_for_item(self,item_id):return self.client.getAllWarehousesForItem(item_id)def get_all_items_for_warehouse(self, warehouse_id):return self.client.getAllItemsForWarehouse(warehouse_id)if __name__ == '__main__':#test config clienttry:inventory_client = InventoryClient()item = Item()item.vendorItemId = 12item.featureDescription = "Black colour"item.featureId = 12item.itemStatus = status.IN_PROCESSitem.weight = 14.2price_map = {}price_map[1] = 23.5price_map[2] = 32.3item.price = price_map#print inventory_client.add_item(item)warehouse = Warehouse()warehouse.location = "Delhi"#print inventory_client.add_warehouse(warehouse)timestamp = datetime.datetime.now()print inventory_client.update_inventory(2, 2, 10, to_java_date(timestamp))# item = inventory_client.get_item(2)# print item#items = inventory_client.get_all_active_items()inventory = inventory_client.get_item_inventory(2)print inventorywarehouses = inventory_client.get_all_warehouses(True)print warehouseswarehouses = inventory_client.get_all_warehouses_for_item(2)print warehousesitems = inventory_client.get_all_items_for_warehouse(2)print itemsexcept:print "error while putting up config client"