Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.

Commit 7949300

Browse files
author
Charlie Moad
committed
added bulk operations for post/patch/delete as seen in https://code.exacttarget.com/question/batch-operation-fuelsdk-python
1 parent 705d95c commit 7949300

File tree

2 files changed

+76
-23
lines changed

2 files changed

+76
-23
lines changed

FuelSDK/objects.py

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,23 @@ def post(self):
251251
self.getCustomerKey()
252252
originalProps = self.props
253253

254-
## FIX THIS
255254
if type(self.props) is list:
256-
pass ##THIS SHOULD DO SOMETHING
255+
currentPropList = []
256+
for rec in self.props:
257+
currentFields = []
258+
currentProp = {}
259+
260+
for key, value in rec.iteritems():
261+
currentFields.append({"Name" : key, "Value" : value})
262+
263+
currentProp['CustomerKey'] = self.CustomerKey
264+
currentProp['Properties'] = {}
265+
currentProp['Properties']['Property'] = currentFields
266+
267+
currentPropList.append(currentProp)
268+
269+
currentProp = currentPropList
270+
257271
else:
258272
currentFields = []
259273
currentProp = {}
@@ -271,30 +285,66 @@ def post(self):
271285

272286
def patch(self):
273287
self.getCustomerKey()
274-
currentFields = []
275-
currentProp = {}
276-
277-
for key, value in self.props.iteritems():
278-
currentFields.append({"Name" : key, "Value" : value})
279288

280-
currentProp['CustomerKey'] = self.CustomerKey
281-
currentProp['Properties'] = {}
282-
currentProp['Properties']['Property'] = currentFields
289+
if type(self.props) is list:
290+
currentPropList = []
291+
for rec in self.props:
292+
currentFields = []
293+
currentProp = {}
294+
295+
for key, value in rec.iteritems():
296+
currentFields.append({"Name" : key, "Value" : value})
297+
298+
currentProp['CustomerKey'] = self.CustomerKey
299+
currentProp['Properties'] = {}
300+
currentProp['Properties']['Property'] = currentFields
301+
302+
currentPropList.append(currentProp)
303+
304+
currentProp = currentPropList
305+
else:
306+
currentFields = []
307+
currentProp = {}
308+
309+
for key, value in self.props.iteritems():
310+
currentFields.append({"Name" : key, "Value" : value})
311+
312+
currentProp['CustomerKey'] = self.CustomerKey
313+
currentProp['Properties'] = {}
314+
currentProp['Properties']['Property'] = currentFields
283315

284316
obj = ET_Patch(self.auth_stub, self.obj_type, currentProp)
285317
return obj
286318

287319
def delete(self):
288320
self.getCustomerKey()
289-
currentFields = []
290-
currentProp = {}
291-
292-
for key, value in self.props.iteritems():
293-
currentFields.append({"Name" : key, "Value" : value})
294321

295-
currentProp['CustomerKey'] = self.CustomerKey
296-
currentProp['Keys'] = {}
297-
currentProp['Keys']['Key'] = currentFields
322+
if type(self.props) is list:
323+
currentPropList = []
324+
for rec in self.props:
325+
currentFields = []
326+
currentProp = {}
327+
328+
for key, value in rec.iteritems():
329+
currentFields.append({"Name" : key, "Value" : value})
330+
331+
currentProp['CustomerKey'] = self.CustomerKey
332+
currentProp['Keys'] = {}
333+
currentProp['Keys']['Key'] = currentFields
334+
335+
currentPropList.append(currentProp)
336+
337+
currentProp = currentPropList
338+
else:
339+
currentFields = []
340+
currentProp = {}
341+
342+
for key, value in self.props.iteritems():
343+
currentFields.append({"Name" : key, "Value" : value})
344+
345+
currentProp['CustomerKey'] = self.CustomerKey
346+
currentProp['Keys'] = {}
347+
currentProp['Keys']['Key'] = currentFields
298348

299349
obj = ET_Delete(self.auth_stub, self.obj_type, currentProp)
300350
return obj

FuelSDK/rest.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,25 @@ def parse_props_dict_into_ws_object(self, obj_type, ws_object, props_dict):
7373
return ws_object
7474

7575
def parse_props_into_ws_object(self, auth_stub, obj_type, props):
76+
empty_obj = auth_stub.soap_client.factory.create(obj_type)
7677
if props is not None and type(props) is dict:
77-
ws_create = auth_stub.soap_client.factory.create(obj_type)
78+
ws_create = copy.copy(empty_obj)
7879
ws_create = self.parse_props_dict_into_ws_object(obj_type, ws_create, props)
79-
return ws_create
80+
return ws_create
8081
elif props is not None and type(props) is list:
8182
ws_create_list = []
8283
for prop_dict in props:
83-
ws_create = auth_stub.soap_client.factory.create(obj_type)
84+
#~ print str(datetime.now())+" - start"
85+
ws_create = copy.copy(empty_obj)
8486
ws_create = self.parse_props_dict_into_ws_object(obj_type, ws_create, prop_dict)
87+
#~ print str(datetime.now())+" - start"
8588
ws_create_list.append(ws_create)
8689
return ws_create_list
8790
else:
8891
message = 'Can not post properties to ' + obj_type + ' without a dict or list of properties'
8992
print message
90-
raise Exception(message)
91-
93+
raise Exception(message)
94+
9295
########
9396
##
9497
## Used to Describe Objects via web service call

0 commit comments

Comments
 (0)