11import uuid
22
33from client import QuickBooks
4+ from exceptions import QuickbooksException
45from objects .batchrequest import IntuitBatchRequest , BatchItemRequest , BatchOperation , BatchResponse , BatchItemResponse
56
67
7- def batch_create (obj_list ):
8- return process_batch (obj_list , BatchOperation .CREATE )
8+ class BatchManager (object ):
9+ def __init__ (self , operation , max_request_items = 30 ):
10+ self ._max_request_items = max_request_items
911
12+ if operation in ["create" , "update" , "delete" ]:
13+ self ._operation = operation
14+ else :
15+ raise QuickbooksException ("Operation not supported." )
1016
11- def batch_update ( obj_list ):
12- return process_batch ( obj_list , BatchOperation . UPDATE )
17+ def save ( self , obj_list ):
18+ batch_response = BatchResponse ( )
1319
20+ while len (obj_list ) > 0 :
21+ temp_list = obj_list [:self ._max_request_items ]
22+ obj_list = [item for item in obj_list if item not in temp_list ]
23+ result = self .process_batch (temp_list )
1424
15- def batch_delete (obj_list ):
16- return process_batch (obj_list , BatchOperation .DELETE )
25+ batch_response .batch_responses += result .batch_responses
26+ batch_response .original_list += result .original_list
27+ batch_response .successes += result .successes
28+ batch_response .faults += result .faults
1729
30+ return batch_response
1831
19- def process_batch (obj_list , operation ):
20- qb = QuickBooks ()
32+ def process_batch (self , obj_list ):
33+ qb = QuickBooks ()
2134
22- batch = list_to_batch_request (obj_list , operation )
23- json_data = qb .batch_operation (batch .to_json ())
35+ batch = self . list_to_batch_request (obj_list )
36+ json_data = qb .batch_operation (batch .to_json ())
2437
25- batch_response = batch_results_to_list (json_data , batch , obj_list )
38+ batch_response = self . batch_results_to_list (json_data , batch , obj_list )
2639
27- return batch_response
40+ return batch_response
2841
42+ def list_to_batch_request (self , obj_list ):
43+ batch = IntuitBatchRequest ()
2944
30- def list_to_batch_request (obj_list , operation ):
31- batch = IntuitBatchRequest ()
45+ for obj in obj_list :
46+ batch_item = BatchItemRequest ()
47+ batch_item .bId = str (uuid .uuid1 ())
48+ batch_item .operation = self ._operation
49+ batch_item .set_object (obj )
3250
33- for obj in obj_list :
34- batch_item = BatchItemRequest ()
35- batch_item .bId = str (uuid .uuid1 ())
36- batch_item .operation = operation
37- batch_item .set_object (obj )
51+ batch .BatchItemRequest .append (batch_item )
3852
39- batch . BatchItemRequest . append ( batch_item )
53+ return batch
4054
41- return batch
55+ def batch_results_to_list (self , json_data , batch , original_list ):
56+ response = BatchResponse ()
57+ response .original_list = original_list
4258
59+ for data in json_data ['BatchItemResponse' ]:
60+ response_item = BatchItemResponse .from_json (data )
4361
44- def batch_results_to_list (json_data , batch , original_list ):
45- response = BatchResponse ()
46- response .original_list = original_list
62+ batch_item = [obj for obj in batch .BatchItemRequest if obj .bId == response_item .bId ][0 ]
63+ response_item .set_object (batch_item .get_object ())
4764
48- for data in json_data ['BatchItemResponse' ]:
49- response_item = BatchItemResponse .from_json (data )
65+ response .batch_responses .append (response_item )
5066
51- batch_item = [obj for obj in batch .BatchItemRequest if obj .bId == response_item .bId ][0 ]
52- response_item .set_object (batch_item .get_object ())
67+ if response_item .Fault :
68+ response_item .Fault .original_object = response_item .get_object ()
69+ response .faults .append (response_item .Fault )
5370
54- response .batch_responses .append (response_item )
71+ else :
72+ response .successes .append (response_item .get_object ())
5573
56- if response_item .Fault :
57- response_item .Fault .original_object = response_item .get_object ()
58- response .faults .append (response_item .Fault )
74+ return response
75+
76+
77+ def batch_create (obj_list ):
78+ batch_mgr = BatchManager (BatchOperation .CREATE )
79+ return batch_mgr .save (obj_list )
80+
81+
82+ def batch_update (obj_list ):
83+ batch_mgr = BatchManager (BatchOperation .UPDATE )
84+ return batch_mgr .save (obj_list )
5985
60- else :
61- response .successes .append (response_item .get_object ())
6286
63- return response
87+ def batch_delete (obj_list ):
88+ batch_mgr = BatchManager (BatchOperation .DELETE )
89+ return batch_mgr .save (obj_list )
0 commit comments