Subversion Repositories SmartDukaan

Rev

Rev 100 | Rev 384 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
100 ashish 1
'''
2
Created on 25-Mar-2010
3
 
4
@author: ashish
5
'''
122 ashish 6
import urlparse
7
from shop2020.thriftpy.config.ttypes import ConfigException
100 ashish 8
from thrift.transport import TSocket
9
from thrift.transport.TTransport import TFramedTransport
10
from thrift.protocol.TBinaryProtocol import TBinaryProtocol
122 ashish 11
from shop2020.thriftpy.config import Configuration
12
from shop2020.thriftpy.model.v1.catalog import InventoryService
13
from shop2020.thriftpy.model.v1.catalog.ttypes import Item, status, Warehouse
14
from shop2020.config.client.ConfigClient import ConfigClient
15
import datetime
16
from shop2020.utils.Utils import to_java_date
100 ashish 17
 
122 ashish 18
class InventoryClient:
19
    file_path = '/tmp/config.properties'
100 ashish 20
 
21
    def __init__(self):
22
        config_client = ConfigClient()
122 ashish 23
        self.host = config_client.get_property("catalog_service_server_host")
24
        self.port = config_client.get_property("catalog_service_server_port")
25
 
26
        self.start_client()
27
 
28
 
29
    def start_client(self):
100 ashish 30
        try:
31
            self.transport = TSocket.TSocket(self.host, self.port)
32
            self.transport = TFramedTransport(self.transport)
33
            self.protocol = TBinaryProtocol(self.transport)
122 ashish 34
            self.client = InventoryService.Client(self.protocol)
100 ashish 35
            self.transport.open()
36
        except:
122 ashish 37
            raise ConfigException(101, 'unable to load the client')
38
 
39
    def add_item(self, item):
40
        return self.client.addItem(item)
100 ashish 41
 
122 ashish 42
    def add_warehouse(self, warehouse):
43
        return self.client.addWarehouse(warehouse)
44
 
45
    def update_inventory(self, item_id, warehouse_id, quantity, timestamp):
46
        return self.client.updateInventory(item_id, warehouse_id, quantity, timestamp)
47
 
48
    def get_item(self, item_id):
49
        return self.client.getItem(item_id)
50
 
51
    def get_all_active_items(self):
52
        return self.client.getAllItems(True)
53
 
54
    def get_all_active_items_by_status(self, status):
55
        return self.client.getAllItemsByStatus(status)
56
 
57
    def get_item_inventory(self, item_id):
58
        return self.client.getItemInventory(item_id)
59
 
60
    def get_all_warehouses(self, status):
61
        return self.client.getAllWarehouses(status)
62
    def get_all_warehouses_for_item(self,item_id):
63
        return self.client.getAllWarehousesForItem(item_id)
64
    def get_all_items_for_warehouse(self, warehouse_id):
65
        return self.client.getAllItemsForWarehouse(warehouse_id)        
66
 
67
 
68
if __name__ == '__main__':
69
    #test config client 
70
    try:
71
        inventory_client = InventoryClient()
72
        item = Item()
73
        item.vendorItemId = 12
74
        item.featureDescription = "Black colour"
75
        item.featureId = 12
76
        item.itemStatus = status.IN_PROCESS
77
        item.weight = 14.2
78
        price_map = {}
79
        price_map[1] = 23.5
80
        price_map[2] = 32.3
81
        item.price = price_map
82
        #print inventory_client.add_item(item)
100 ashish 83
 
122 ashish 84
        warehouse = Warehouse()
85
        warehouse.location = "Delhi"
86
        #print inventory_client.add_warehouse(warehouse)
100 ashish 87
 
122 ashish 88
        timestamp = datetime.datetime.now()
89
        print inventory_client.update_inventory(2, 2, 10, to_java_date(timestamp))
90
    #    item = inventory_client.get_item(2)
91
     #   print item
92
 
93
        #items = inventory_client.get_all_active_items()
94
        inventory = inventory_client.get_item_inventory(2)
95
        print inventory
96
 
97
        warehouses = inventory_client.get_all_warehouses(True)
98
 
99
        print warehouses
100
 
101
        warehouses = inventory_client.get_all_warehouses_for_item(2)
102
 
103
        print warehouses
104
 
105
        items = inventory_client.get_all_items_for_warehouse(2)
106
 
107
        print items
108
    except:
109
        print "error while putting up config client"
110