Skip to content

Commit 63ac2d3

Browse files
committed
Added BatchManager.
1 parent e0688c7 commit 63ac2d3

File tree

2 files changed

+71
-41
lines changed

2 files changed

+71
-41
lines changed

quickbooks/batch.py

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,89 @@
11
import uuid
22

33
from client import QuickBooks
4+
from exceptions import QuickbooksException
45
from 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)

tests/unit/test_batch.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from quickbooks import batch, client
44
from quickbooks.objects.customer import Customer
55

6+
67
class BatchTests(unittest.TestCase):
78
def setUp(self):
89
self.qb = client.QuickBooks(
@@ -20,24 +21,26 @@ def setUp(self):
2021
self.object2 = Customer()
2122
self.obj_list = [self.object1, self.object2]
2223

23-
@patch('quickbooks.batch.process_batch')
24+
@patch('quickbooks.batch.BatchManager.process_batch')
2425
def test_batch_create(self, process_batch):
2526
results = batch.batch_create(self.obj_list)
2627
self.assertTrue(process_batch.called)
2728

28-
@patch('quickbooks.batch.process_batch')
29+
@patch('quickbooks.batch.BatchManager.process_batch')
2930
def test_batch_update(self, process_batch):
3031
results = batch.batch_update(self.obj_list)
3132
self.assertTrue(process_batch.called)
3233

33-
@patch('quickbooks.batch.process_batch')
34+
@patch('quickbooks.batch.BatchManager.process_batch')
3435
def test_batch_delete(self, process_batch):
3536
results = batch.batch_delete(self.obj_list)
3637
self.assertTrue(process_batch.called)
3738

3839
def test_list_to_batch_request(self):
40+
batch_mgr = batch.BatchManager("create")
41+
3942
obj_list = [self.object1, self.object2]
40-
batch_request = batch.list_to_batch_request(obj_list, "create")
43+
batch_request = batch_mgr.list_to_batch_request(obj_list)
4144

4245
self.assertEquals(len(batch_request.BatchItemRequest), 2)
4346

@@ -47,18 +50,19 @@ def test_list_to_batch_request(self):
4750
self.assertEquals(batch_item.get_object(), self.object1)
4851

4952
def test_batch_results_to_list(self):
53+
batch_mgr = batch.BatchManager("create")
5054
json_data = {"BatchItemResponse": [{"Customer": {"Id": 164}, "bId": "2"},
5155
{"Fault": {"type": "ValidationFault",
5256
"Error": [{"Message": "Duplicate Name Exists Error",
5357
"code": "6240", "Detail": "detail message",
5458
"element": ""}]}, "bId": "1"}],
5559
"time": "2015-08-10T11:44:02.957-07:00"}
5660

57-
batch_request = batch.list_to_batch_request(self.obj_list, "create")
61+
batch_request = batch_mgr.list_to_batch_request(self.obj_list)
5862
batch_request.BatchItemRequest[0].bId = "1"
5963
batch_request.BatchItemRequest[1].bId = "2"
6064

61-
results = batch.batch_results_to_list(json_data, batch_request, self.obj_list)
65+
results = batch_mgr.batch_results_to_list(json_data, batch_request, self.obj_list)
6266

6367
self.assertEquals(len(results.faults), 1)
6468
self.assertEquals(len(results.successes), 1)

0 commit comments

Comments
 (0)