Subversion Repositories SmartDukaan

Rev

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 urlparse
from shop2020.thriftpy.config.ttypes import ConfigException
from thrift.transport import TSocket
from thrift.transport.TTransport import TFramedTransport
from thrift.protocol.TBinaryProtocol import TBinaryProtocol
from shop2020.thriftpy.config import Configuration
from shop2020.thriftpy.model.v1.catalog import InventoryService
from shop2020.thriftpy.model.v1.catalog.ttypes import Item, status, Warehouse
from shop2020.config.client.ConfigClient import ConfigClient
import datetime
from shop2020.utils.Utils import to_java_date

class 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 client 
    try:
        inventory_client = InventoryClient()
        item = Item()
        item.vendorItemId = 12
        item.featureDescription = "Black colour"
        item.featureId = 12
        item.itemStatus = status.IN_PROCESS
        item.weight = 14.2
        price_map = {}
        price_map[1] = 23.5
        price_map[2] = 32.3
        item.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 inventory
        
        warehouses = inventory_client.get_all_warehouses(True)
        
        print warehouses
        
        warehouses = inventory_client.get_all_warehouses_for_item(2)
        
        print warehouses
        
        items = inventory_client.get_all_items_for_warehouse(2)
        
        print items
    except:
        print "error while putting up config client"