Subversion Repositories SmartDukaan

Rev

Rev 100 | Rev 384 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 100 Rev 122
Line 1... Line 1...
1
'''
1
'''
2
Created on 25-Mar-2010
2
Created on 25-Mar-2010
3
 
3
 
4
@author: ashish
4
@author: ashish
5
'''
5
'''
-
 
6
import urlparse
6
from shop2020.config.client.ConfigClient import ConfigClient
7
from shop2020.thriftpy.config.ttypes import ConfigException
7
from thrift.transport import TSocket
8
from thrift.transport import TSocket
8
from thrift.transport.TTransport import TFramedTransport
9
from thrift.transport.TTransport import TFramedTransport
9
from thrift.protocol.TBinaryProtocol import TBinaryProtocol
10
from thrift.protocol.TBinaryProtocol import TBinaryProtocol
-
 
11
from shop2020.thriftpy.config import Configuration
10
from shop2020.thriftpy.model.v1.catalog import CatalogService
12
from shop2020.thriftpy.model.v1.catalog import InventoryService
11
from shop2020.thriftpy.model.v1.catalog.ttypes import CatalogServiceException,\
13
from shop2020.thriftpy.model.v1.catalog.ttypes import Item, status, Warehouse
-
 
14
from shop2020.config.client.ConfigClient import ConfigClient
12
    Category, Vendor
15
import datetime
-
 
16
from shop2020.utils.Utils import to_java_date
13
 
17
 
14
class TestClient:
18
class InventoryClient:
-
 
19
    file_path = '/tmp/config.properties'
15
    
20
    
16
    def __init__(self):
21
    def __init__(self):
17
        config_client = ConfigClient()
22
        config_client = ConfigClient()
18
        self.host = config_client.get_property('catalog_service_server_host')
23
        self.host = config_client.get_property("catalog_service_server_host")
19
        self.port = config_client.get_property('catalog_service_server_port')
24
        self.port = config_client.get_property("catalog_service_server_port")
-
 
25
        
20
        self.start()
26
        self.start_client()
-
 
27
    
-
 
28
    
21
    def start(self):
29
    def start_client(self):
22
        try:
30
        try:
23
            self.transport = TSocket.TSocket(self.host, self.port)
31
            self.transport = TSocket.TSocket(self.host, self.port)
24
            self.transport = TFramedTransport(self.transport)
32
            self.transport = TFramedTransport(self.transport)
25
            self.protocol = TBinaryProtocol(self.transport)
33
            self.protocol = TBinaryProtocol(self.transport)
26
            self.client = CatalogService.Client(self.protocol)
34
            self.client = InventoryService.Client(self.protocol)
27
            self.transport.open()
35
            self.transport.open()
28
        except:
36
        except:
29
            raise CatalogServiceException(101, 'unable to load the client')
37
            raise ConfigException(101, 'unable to load the client')
-
 
38
    
-
 
39
    def add_item(self, item):
-
 
40
        return self.client.addItem(item)
30
        
41
        
31
    def addCategory(self):
-
 
32
        cat = Category()
-
 
33
        cat.name = 'Entertainment'
-
 
34
        cat.addedOn = 12345678
-
 
35
        self.client.addCategory(cat)
-
 
36
        cats = self.client.getAllCategories()
-
 
37
        for category in cats:
-
 
38
            print category.name
-
 
39
        
-
 
40
        
-
 
41
    def addVendor(self):
-
 
42
        vend = Vendor()
-
 
43
        vend.name = 'Nokia'
-
 
44
        self.client.addVendor(vend)    
-
 
45
 
-
 
46
if __name__=='__main__':
-
 
47
    test = TestClient()
-
 
48
    test.addCategory()    
-
 
49
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)
-
 
83
        
-
 
84
        warehouse = Warehouse()
-
 
85
        warehouse.location = "Delhi"
-
 
86
        #print inventory_client.add_warehouse(warehouse)
-
 
87
        
-
 
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
        
-
 
111
50
112