forked from salesforce-marketingcloud/FuelSDK-Python
/
ET_Client.py
1016 lines (819 loc) · 34.2 KB
/
ET_Client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import requests
import et_suds.client
import et_suds.wsse
from et_suds.sax.element import Element
import logging
import os
import ConfigParser
import time
import json
import traceback
import jwt
########
##
## Setup web service connectivity by getting need config data, security tokens etc.
##
########
class ET_Client(object):
debug = False
client_id = None
client_secret = None
appsignature = None
wsdl_file_url = None
authToken = None
internalAuthToken = None
authTokenExpiration = None #seconds since epoch that the current jwt token will expire
refreshKey = None
endpoint = None
authObj = None
soap_client = None
## get_server_wsdl - if True and a newer WSDL is on the server than the local filesystem retrieve it
def __init__(self, client_id, client_secret, appsignature='none',
wsdl_server_url = 'https://webservice.exacttarget.com/etframework.wsdl',
get_server_wsdl = False, debug = False, params = None):
self.debug = debug
if(debug):
logging.getLogger('et_suds.client').setLevel(logging.DEBUG)
logging.getLogger('et_suds.transport').setLevel(logging.DEBUG)
logging.getLogger('et_suds.xsd.schema').setLevel(logging.DEBUG)
logging.getLogger('et_suds.wsdl').setLevel(logging.DEBUG)
else:
logging.getLogger('et_suds').setLevel(logging.ERROR)
logging.getLogger('et_suds.client').setLevel(logging.ERROR)
logging.getLogger('et_suds.transport').setLevel(logging.ERROR)
logging.getLogger('et_suds.xsd.schema').setLevel(logging.ERROR)
logging.getLogger('et_suds.wsdl').setLevel(logging.ERROR)
self.client_id = client_id
self.client_secret = client_secret
self.appsignature = appsignature
self.wsdl_file_url = self.load_wsdl(wsdl_server_url, get_server_wsdl)
## get the JWT from the params if passed in...or go to the server to get it
if(params is not None and 'jwt' in params):
decodedJWT = jwt.decode(params['jwt'], self.appsignature)
self.authToken = decodedJWT['request']['user']['oauthToken']
self.authTokenExpiration = time.time() + decodedJWT['request']['user']['expiresIn']
self.internalAuthToken = decodedJWT['request']['user']['internalOauthToken']
if 'refreshToken' in decodedJWT:
self.refreshKey = tokenResponse['request']['user']['refreshToken']
self.build_soap_client()
else:
self.refresh_token()
## retrieve the url of the ExactTarget wsdl...either file: or http:
## depending on if it already exists locally or server flag is set and
## server has a newer copy
def load_wsdl(self, wsdl_url, get_server_wsdl = False):
path = os.path.dirname(os.path.abspath(__file__))
file_location = os.path.join(path, 'ExactTargetWSDL.xml')
file_url = 'file:///' + file_location
if not os.path.exists(file_location): #if there is no local copy then go get it...
self.retrieve_server_wsdl(wsdl_url, file_location)
elif get_server_wsdl:
r = requests.head(wsdl_url)
if r is not None and 'last-modified' in r.headers:
server_wsdl_updated = time.strptime(r.headers['last-modified'], '%a, %d %b %Y %H:%M:%S %Z')
file_wsdl_updated = time.gmtime(os.path.getmtime(file_location))
if server_wsdl_updated > file_wsdl_updated:
self.retrieve_server_wsdl(wsdl_url, file_location)
return file_url
### get the WSDL from the server and save it locally
def retrieve_server_wsdl(self, wsdl_url, file_location):
r = requests.get(wsdl_url)
f = open(file_location, 'w')
f.write(r.text)
def build_soap_client(self):
if self.endpoint is None:
self.endpoint = self.determineStack()
self.authObj = {'oAuth' : {'oAuthToken' : self.internalAuthToken}}
self.authObj['attributes'] = { 'oAuth' : { 'xmlns' : 'http://exacttarget.com' }}
self.soap_client = et_suds.client.Client(self.wsdl_file_url, faults=False, cachingpolicy=1)
self.soap_client.set_options(location=self.endpoint)
element_oAuth = Element('oAuth', ns=('etns', 'http://exacttarget.com'))
element_oAuthToken = Element('oAuthToken').setText(self.internalAuthToken)
element_oAuth.append(element_oAuthToken)
self.soap_client.set_options(soapheaders=(element_oAuth))
security = et_suds.wsse.Security()
token = et_suds.wsse.UsernameToken('*', '*')
security.tokens.append(token)
self.soap_client.set_options(wsse=security)
## Called from many different places right before executing a SOAP call
def refresh_token(self, force_refresh = False):
#If we don't already have a token or the token expires within 5 min(300 seconds), get one
if (force_refresh or self.authToken is None or (self.authTokenExpiration is not None and time.time() + 300 > self.authTokenExpiration)):
headers = {'content-type' : 'application/json'}
if (self.authToken is None):
payload = {'clientId' : self.client_id, 'clientSecret' : self.client_secret, 'accessType': 'offline'}
else:
payload = {'clientId' : self.client_id, 'clientSecret' : self.client_secret, 'refreshToken' : self.refreshKey, 'accessType': 'offline', 'scope':'cas:'+ self.internalAuthToken}
if self.refreshKey:
payload['refreshToken'] = self.refreshKey
r = requests.post('https://auth.exacttargetapis.com/v1/requestToken?legacy=1', data=json.dumps(payload), headers=headers)
tokenResponse = r.json
if 'accessToken' not in tokenResponse:
raise Exception('Unable to validate App Keys(ClientID/ClientSecret) provided: ' + repr(r.json))
self.authToken = tokenResponse['accessToken']
self.authTokenExpiration = time.time() + tokenResponse['expiresIn']
self.internalAuthToken = tokenResponse['legacyToken']
if 'refreshToken' in tokenResponse:
self.refreshKey = tokenResponse['refreshToken']
self.build_soap_client()
##find the correct url that data request web calls should go against for the token we have.
def determineStack(self):
try:
r = requests.get('https://www.exacttargetapis.com/platform/v1/endpoints/soap?access_token=' + self.authToken)
contextResponse = r.json
if('url' in contextResponse):
return str(contextResponse['url'])
except Exception as e:
raise Exception('Unable to determine stack using /platform/v1/tokenContext: ' + e.message)
##add or update a subscriber with a list
def AddSubscriberToList(self, emailAddress, listIDs, subscriberKey = None):
newSub = ET_Subscriber()
newSub.auth_stub = self
lists = []
for p in listIDs:
lists.append({"ID" : p})
newSub.props = {"EmailAddress" : emailAddress, "Lists" : lists}
if subscriberKey is not None:
newSub.props['SubscriberKey'] = subscriberKey
# Try to add the subscriber
postResponse = newSub.post()
if not postResponse.status:
# If the subscriber already exists in the account then we need to do an update.
# Update Subscriber On List
if postResponse.results[0]['ErrorCode'] == 12014:
patchResponse = newSub.patch()
return patchResponse
return postResponse
##write the data extension props to the web service
def CreateDataExtensions(self, dataExtensionDefinitions):
newDEs = ET_DataExtension()
newDEs.auth_stub = self
newDEs.props = dataExtensionDefinitions
postResponse = newDEs.post()
return postResponse
########
##
## Parent class used to determine what status we are in depending on web service call results
##
########
class ET_Constructor(object):
results = []
code = None
status = False
message = None
more_results = False
request_id = None
def __init__(self, response = None, rest = False):
if response is not None: #if a response was returned from the web service call
if rest: # result is from a REST web service call...
self.code = response.status_code
if self.code == 200:
self.status = True
else:
self.status = False
try:
self.results = response.json
except:
self.message = response.json
#additional parsing will happen in the child object that called in to here.
else: #soap call
self.code = response[0] #et_suds puts the code in tuple position 0
body = response[1] #and the result in tuple position 1
# Store the Last Request ID for use with continue
if 'RequestID' in body:
self.request_id = body['RequestID']
if self.code == 200:
self.status = True
if 'OverallStatus' in body:
self.message = body['OverallStatus']
if body['OverallStatus'] == "MoreDataAvailable":
self.more_results = True
elif body['OverallStatus'] != "OK":
self.status = False
body_container_tag = None
if 'Results' in body: #most SOAP responses are wrapped in 'Results'
body_container_tag = 'Results'
elif 'ObjectDefinition' in body: #Describe SOAP response is in 'ObjectDefinition'
body_container_tag = 'ObjectDefinition'
if body_container_tag is not None:
self.results = body[body_container_tag]
else:
self.status = False
def parse_props_dict_into_ws_object(self, obj_type, ws_object, props_dict):
for k, v in props_dict.iteritems():
if k in ws_object:
ws_object[k] = v
else:
message = k + ' is not a property of ' + obj_type
print message
raise Exception(message)
return ws_object
def parse_props_into_ws_object(self, auth_stub, obj_type, props):
if props is not None and type(props) is dict:
ws_create = auth_stub.soap_client.factory.create(obj_type)
ws_create = self.parse_props_dict_into_ws_object(obj_type, ws_create, props)
return ws_create
elif props is not None and type(props) is list:
ws_create_list = []
for prop_dict in props:
ws_create = auth_stub.soap_client.factory.create(obj_type)
ws_create = self.parse_props_dict_into_ws_object(obj_type, ws_create, prop_dict)
ws_create_list.append(ws_create)
return ws_create_list
else:
message = 'Can not post properties to ' + obj_type + ' without a dict or list of properties'
print message
raise Exception(message)
########
##
## Used to Describe Objects via web service call
##
########
class ET_Describe(ET_Constructor):
def __init__(self, auth_stub, obj_type):
auth_stub.refresh_token()
ws_describeRequest = auth_stub.soap_client.factory.create('ArrayOfObjectDefinitionRequest')
ObjectDefinitionRequest = { 'ObjectType' : obj_type}
ws_describeRequest.ObjectDefinitionRequest = [ObjectDefinitionRequest]
response = auth_stub.soap_client.service.Describe(ws_describeRequest)
if response is not None:
self.message = 'Describe: ' + obj_type
super(ET_Describe, self).__init__(response)
########
##
## Get call to a web service
##
########
class ET_Get(ET_Constructor):
def __init__(self, auth_stub, obj_type, props = None, search_filter = None):
auth_stub.refresh_token()
if props is None: #if there are no properties to retrieve for the obj_type then return a Description of obj_type
describe = ET_Describe(auth_stub, obj_type)
props = []
for prop in describe.results[0].Properties:
if prop.IsRetrievable:
props.append(prop.Name)
ws_retrieveRequest = auth_stub.soap_client.factory.create('RetrieveRequest')
if props is not None:
if type(props) is dict: # If the properties is a hash, then we just want to use the keys
ws_retrieveRequest.Properties = props.keys
else:
ws_retrieveRequest.Properties = props
if search_filter is not None:
if search_filter.has_key('LogicalOperator'):
ws_simpleFilterPartLeft = auth_stub.soap_client.factory.create('SimpleFilterPart')
for prop in ws_simpleFilterPartLeft:
print prop[0]
if prop[0] in search_filter['LeftOperand']:
ws_simpleFilterPartLeft[prop[0]] = search_filter['LeftOperand'][prop[0]]
ws_simpleFilterPartRight = auth_stub.soap_client.factory.create('SimpleFilterPart')
for prop in ws_simpleFilterPartRight:
if prop[0] in search_filter['RightOperand']:
ws_simpleFilterPartRight[prop[0]] = search_filter['RightOperand'][prop[0]]
ws_complexFilterPart = auth_stub.soap_client.factory.create('ComplexFilterPart')
ws_complexFilterPart.LeftOperand = ws_simpleFilterPartLeft
ws_complexFilterPart.RightOperand = ws_simpleFilterPartRight
ws_complexFilterPart.LogicalOperator = search_filter['LogicalOperator']
ws_retrieveRequest.Filter = ws_complexFilterPart
else:
ws_simpleFilterPart = auth_stub.soap_client.factory.create('SimpleFilterPart')
for prop in ws_simpleFilterPart:
if prop[0] in search_filter:
ws_simpleFilterPart[prop[0]] = search_filter[prop[0]]
ws_retrieveRequest.Filter = ws_simpleFilterPart
ws_retrieveRequest.ObjectType = obj_type
response = auth_stub.soap_client.service.Retrieve(ws_retrieveRequest)
if response is not None:
super(ET_Get, self).__init__(response)
########
##
## Call the Exact Target web service Create method
##
########
class ET_Post(ET_Constructor):
def __init__(self, auth_stub, obj_type, props = None):
auth_stub.refresh_token()
response = auth_stub.soap_client.service.Create(None, self.parse_props_into_ws_object(auth_stub, obj_type, props))
if(response is not None):
super(ET_Post, self).__init__(response)
########
##
## Call the Exact Target web service Update method
##
########
class ET_Patch(ET_Constructor):
def __init__(self, auth_stub, obj_type, props = None):
auth_stub.refresh_token()
response = auth_stub.soap_client.service.Update(None, self.parse_props_into_ws_object(auth_stub, obj_type, props))
if(response is not None):
super(ET_Patch, self).__init__(response)
########
##
## Call the Exact Target web service Delete method
##
########
class ET_Delete(ET_Constructor):
def __init__(self, auth_stub, obj_type, props = None):
auth_stub.refresh_token()
response = auth_stub.soap_client.service.Delete(None, self.parse_props_into_ws_object(auth_stub, obj_type, props))
if(response is not None):
super(ET_Delete, self).__init__(response)
########
##
## Call the Exact Target web service RetrieveRequest passing in ContinueRequest param
##
########
class ET_Continue(ET_Constructor):
def __init__(self, auth_stub, request_id):
auth_stub.refresh_token()
ws_continueRequest = auth_stub.soap_client.factory.create('RetrieveRequest')
ws_continueRequest.ContinueRequest = request_id
response = auth_stub.soap_client.service.Retrieve(ws_continueRequest)
if response is not None:
super(ET_Continue, self).__init__(response)
########
##
## set up variables for children objects to share
##
########
class ET_BaseObject(object):
auth_stub = None
obj = None
last_request_id = None
endpoint = None
props = None
extProps = None
search_filter = None
########
##
## make sure needed information is available and then make the call to ET_Get to call the webservice
##
########
class ET_GetSupport(ET_BaseObject):
obj_type = 'ET_GetSupport' #should be overwritten by inherited class
def get(self, m_props = None, m_filter = None):
props = self.props
search_filter = self.search_filter
if m_props is not None and type(m_props) is list:
props = m_props
elif self.props is not None and type(self.props) is dict:
props = self.props.keys()
if m_filter is not None and type(m_filter) is dict:
search_filter = m_filter
obj = ET_Get(self.auth_stub, self.obj_type, props, search_filter)
if obj is not None:
self.last_request_id = obj.request_id
return obj
def info(self):
obj = ET_Describe(self.auth_stub, self.obj_type)
if obj is not None:
self.last_request_id = obj.request_id
return obj
def getMoreResults(self):
obj = ET_Continue(self.auth_stub, self.last_request_id)
if obj is not None:
self.last_request_id = obj.request_id
return obj
########
##
## Restful webservice to Get data
##
########
class ET_GetRest(ET_Constructor):
def __init__(self, auth_stub, endpoint, qs = None):
auth_stub.refresh_token()
fullendpoint = endpoint + '?access_token=' + auth_stub.authToken
for qStringValue in qs:
fullendpoint += '&'+ qStringValue + '=' + str(qs[qStringValue])
r = requests.get(fullendpoint)
self.more_results = False
obj = super(ET_GetRest, self).__init__(r, True)
return obj
########
##
## Restful webservice to Get data
##
########
class ET_PostRest(ET_Constructor):
def __init__(self, auth_stub, endpoint, payload):
auth_stub.refresh_token()
headers = {'content-type' : 'application/json'}
r = requests.post(endpoint + '?access_token=' + auth_stub.authToken , data=json.dumps(payload), headers=headers)
obj = super(ET_PostRest, self).__init__(r, True)
return obj
########
##
## Restful webservice to Get data
##
########
class ET_PatchRest(ET_Constructor):
def __init__(self, auth_stub, endpoint, payload):
auth_stub.refresh_token()
headers = {'content-type' : 'application/json'}
r = requests.patch(endpoint + '?access_token=' + auth_stub.authToken , data=json.dumps(payload), headers=headers)
obj = super(ET_PatchRest, self).__init__(r, True)
return obj
########
##
## Restful webservice to Get data
##
########
class ET_DeleteRest(ET_Constructor):
def __init__(self, auth_stub, endpoint):
auth_stub.refresh_token()
r = requests.delete(endpoint + '?access_token=' + auth_stub.authToken)
obj = super(ET_DeleteRest, self).__init__(r, True)
return obj
########
##
## Get data
##
########
class ET_CUDSupport(ET_GetSupport):
def __init__(self):
super(ET_CUDSupport, self).__init__()
def post(self):
if self.extProps is not None:
for k, v in self.extProps.iteritems():
self.props[k.capitalize] = v
obj = ET_Post(self.auth_stub, self.obj_type, self.props)
if obj is not None:
self.last_request_id = obj.request_id
return obj
def patch(self):
obj = ET_Patch(self.auth_stub, self.obj_type, self.props)
if obj is not None:
self.last_request_id = obj.request_id
return obj
def delete(self):
obj = ET_Delete(self.auth_stub, self.obj_type, self.props)
if obj is not None:
self.last_request_id = obj.request_id
return obj
########
##
## Get data using a REST call
##
########
class ET_GetSupportRest(ET_BaseObject):
urlProps = None
urlPropsRequired = None
lastPageNumber = None
def __init__(self):
super
def get(self, props = None):
if props is not None and type(props) is dict:
self.props = props
completeURL = self.endpoint
additionalQS = {}
if self.props is not None and type(self.props) is dict:
for k, v in self.props.iteritems():
if k in self.urlProps:
completeURL = completeURL.replace('{{{0}}}'.format(k), v)
else:
additionalQS[k] = v
for value in self.urlPropsRequired:
if self.props is None or value not in self.props:
raise "Unable to process request due to missing required prop: #{value}"
for value in self.urlProps:
completeURL = completeURL.replace('/{{{0}}}'.format(value), '')
obj = ET_GetRest(self.auth_stub, completeURL, additionalQS)
results = obj.results
if 'page' in obj.results:
self.lastPageNumber = obj.results['page']
pageSize = obj.results['pageSize']
if 'count' in obj.results:
count = obj.results['count']
elif 'totalCount' in obj.results:
count = obj.results['totalCount']
if count is not None and count > (self.lastPageNumber * pageSize):
obj.more_results = True
else:
obj.more_results = False
return obj
def getMoreResults(self):
props = None #where should it come from?
if props is not None and type(props) is dict:
self.props = props
originalPageValue = "1"
removePageFromProps = False
if self.props is not None and '$page' in self.props:
originalPageValue = self.props['page']
else:
removePageFromProps = True
if self.props is None:
self.props = {}
self.props['$page'] = self.lastPageNumber + 1
obj = self.get()
if removePageFromProps:
del self.props['$page']
else:
self.props['$page'] = originalPageValue
return obj
########
##
## Create, Update and Delete using a REST call
##
########
class ET_CUDSupportRest(ET_GetSupportRest):
endpoint = None
urlProps = None
urlPropsRequired = None
def __init__(self):
super
def post(self):
completeURL = self.endpoint
if self.props is not None and type(self.props) is dict:
for k, v in self.props.iteritems():
if k in self.urlProps:
completeURL = completeURL.replace('{{{0}}}'.format(k), v)
for value in self.urlPropsRequired:
if self.props is None or value not in self.props:
raise "Unable to process request due to missing required prop: #{value}"
# Clean Optional Parameters from Endpoint URL first
for value in self.urlProps:
completeURL = completeURL.replace('/{{{0}}}'.format(value), '')
obj = ET_PostRest(self.auth_stub, completeURL, self.props)
return obj
def patch(self):
completeURL = self.endpoint
# All URL Props are required when doing Patch
for value in self.urlProps:
if self.props is None or value not in self.props:
raise "Unable to process request due to missing required prop: #{value}"
if self.props is not None and type(self.props) is dict:
for k, v in self.props.iteritems():
if k in self.urlProps:
completeURL = completeURL.replace('{{{0}}}'.format(k), v)
obj = ET_PatchRest(self.auth_stub, completeURL, self.props)
return obj
def delete(self):
completeURL = self.endpoint
# All URL Props are required when doing Patch
for value in self.urlProps:
if self.props is None or value not in self.props:
raise "Unable to process request due to missing required prop: #{value}"
if self.props is not None and type(self.props) is dict:
for k, v in self.props.iteritems():
if k in self.urlProps:
completeURL = completeURL.replace('{{{0}}}'.format(k), v)
obj = ET_DeleteRest(self.auth_stub, completeURL)
return obj
########
##
## wrap an Exact Target Content Area
##
########
class ET_ContentArea(ET_CUDSupport):
def __init__(self):
super(ET_ContentArea, self).__init__()
self.obj_type = 'ContentArea'
########
##
## wrap an Exact Target Content Area
##
########
class ET_Folder(ET_CUDSupport):
def __init__(self):
super(ET_Folder, self).__init__()
self.obj_type = 'DataFolder'
########
##
## wrap an Exact Target Bounce Event
##
########
class ET_BounceEvent(ET_GetSupport):
def __init__(self):
self.obj_type = 'BounceEvent'
########
##
## wrap an Exact Target Campaign and associated Assets
##
########
class ET_Campaign(ET_CUDSupportRest):
def __init__(self):
super(ET_Campaign, self).__init__()
self.endpoint = 'https://www.exacttargetapis.com/hub/v1/campaigns/{id}'
self.urlProps = ["id"]
self.urlPropsRequired = []
##the patch rest service is not implemented for campaigns yet. use post instead and remove this when patch is implemented on the back end
def patch(self):
self.endpoint = 'https://www.exacttargetapis.com/hub/v1/campaigns' #don't put the id on the url when patching via post
obj = super(ET_Campaign, self).post()
self.endpoint = 'https://www.exacttargetapis.com/hub/v1/campaigns/{id}' #but set it back to the url with id for other operations to continue working
return obj
class ET_Campaign_Asset(ET_CUDSupportRest):
def __init__(self):
super(ET_Campaign_Asset, self).__init__()
self.endpoint = 'https://www.exacttargetapis.com/hub/v1/campaigns/{id}/assets/{assetId}'
self.urlProps = ["id", "assetId"]
self.urlPropsRequired = ["id"]
########
##
## wrap an Exact Target Click Event
##
########
class ET_ClickEvent(ET_GetSupport):
def __init__(self):
super(ET_ClickEvent, self).__init__()
self.obj_type = 'ClickEvent'
########
##
## wrap an Exact Target List and List Subscriber
##
########
class ET_List(ET_CUDSupport):
def __init__(self):
super(ET_List, self).__init__()
self.obj_type = 'List'
class ET_List_Subscriber(ET_GetSupport):
def __init__(self):
super(ET_List_Subscriber, self).__init__()
self.obj_type = 'ListSubscriber'
class ET_SentEvent(ET_GetSupport):
def __init__(self):
super(ET_SentEvent, self).__init__()
self.obj_type = 'SentEvent'
class ET_OpenEvent(ET_GetSupport):
def __init__(self):
super(ET_OpenEvent, self).__init__()
self.obj_type = 'OpenEvent'
class ET_UnsubEvent(ET_GetSupport):
def __init__(self):
super(ET_UnsubEvent, self).__init__()
self.obj_type = 'UnsubEvent'
class ET_Email(ET_CUDSupport):
def __init__(self):
super(ET_Email, self).__init__()
self.obj_type = 'Email'
class ET_TriggeredSend(ET_CUDSupport):
subscribers = None
def __init__(self):
super(ET_TriggeredSend, self).__init__()
self.obj_type = 'TriggeredSendDefinition'
def send(self):
tscall = {"TriggeredSendDefinition" : self.props, "Subscribers" : self.subscribers}
self.obj = ET_Post(self.auth_stub, "TriggeredSend", tscall)
return self.obj
class ET_Subscriber(ET_CUDSupport):
def __init__(self):
super(ET_Subscriber, self).__init__()
self.obj_type = 'Subscriber'
class ET_DataExtension(ET_CUDSupport):
columns = None
def __init__(self):
super(ET_DataExtension, self).__init__()
self.obj_type = 'DataExtension'
def post(self):
originalProps = self.props
if type(self.props) is list:
multiDE = []
for currentDE in self.props:
currentDE['Fields'] = {}
currentDE['Fields']['Field'] = []
for key in currentDE['columns']:
currentDE['Fields']['Field'].append(key)
del currentDE['columns']
multiDE.append(currentDE.copy())
self.props = multiDE
else:
self.props['Fields'] = {}
self.props['Fields']['Field'] = []
for key in self.columns:
self.props['Fields']['Field'].append(key)
obj = super(ET_DataExtension, self).post()
self.props = originalProps
return obj
def patch(self):
self.props['Fields'] = {}
self.props['Fields']['Field'] = []
for key in self.columns:
self.props['Fields']['Field'].append(key)
obj = super(ET_DataExtension, self).patch()
del self.props["Fields"]
return obj
class ET_DataExtension_Column(ET_GetSupport):
def __init__(self):
super(ET_DataExtension_Column, self).__init__()
self.obj = 'DataExtensionField'
def get(self):
'''
if props and props.is_a? Array then
@props = props
end
'''
if self.props is not None and type(self.props) is dict:
self.props = self.props.keys()
'''
if filter and filter.is_a? Hash then
@filter = filter
end
'''
'''
fixCustomerKey = False
if filter and filter.is_a? Hash then
@filter = filter
if @filter.has_key?("Property") && @filter["Property"] == "CustomerKey" then
@filter["Property"] = "DataExtension.CustomerKey"
fixCustomerKey = true
end
end
'''
obj = ET_Get(self.auth_stub, self.obj, self.props, self.search_filter)
self.last_request_id = obj.request_id
'''
if fixCustomerKey then
@filter["Property"] = "CustomerKey"
end
'''
return obj
class ET_DataExtension_Row(ET_CUDSupport):
Name = None
CustomerKey = None
def __init__(self):
super(ET_DataExtension_Row, self).__init__()
self.obj_type = "DataExtensionObject"
def get(self):
self.getName()
'''
if props and props.is_a? Array then
@props = props
end
'''
if self.props is not None and type(self.props) is dict:
self.props = self.props.keys()
'''
if filter and filter.is_a? Hash then
@filter = filter
end
'''
obj = ET_Get(self.auth_stub, "DataExtensionObject[{0}]".format(self.Name), self.props, self.search_filter)
self.last_request_id = obj.request_id
return obj
def post(self):
self.getCustomerKey()
originalProps = self.props
## FIX THIS
if type(self.props) is list:
pass ##THIS SHOULD DO SOMETHING
else:
currentFields = []
currentProp = {}
for key, value in self.props.iteritems():
currentFields.append({"Name" : key, "Value" : value})
currentProp['CustomerKey'] = self.CustomerKey
currentProp['Properties'] = {}
currentProp['Properties']['Property'] = currentFields
obj = ET_Post(self.auth_stub, self.obj_type, currentProp)
self.props = originalProps
return obj
def patch(self):
self.getCustomerKey()
currentFields = []
currentProp = {}
for key, value in self.props.iteritems():
currentFields.append({"Name" : key, "Value" : value})
currentProp['CustomerKey'] = self.CustomerKey
currentProp['Properties'] = {}
currentProp['Properties']['Property'] = currentFields
obj = ET_Patch(self.auth_stub, self.obj_type, currentProp)
return obj
def delete(self):
self.getCustomerKey()
currentFields = []
currentProp = {}
for key, value in self.props.iteritems():
currentFields.append({"Name" : key, "Value" : value})
currentProp['CustomerKey'] = self.CustomerKey
currentProp['Keys'] = {}
currentProp['Keys']['Key'] = currentFields
obj = ET_Delete(self.auth_stub, self.obj_type, currentProp)
return obj
def getCustomerKey(self):
if self.CustomerKey is None:
if self.Name is None:
raise Exception('Unable to process DataExtension::Row request due to CustomerKey and Name not being defined on ET_DatExtension::row')
else:
de = ET_DataExtension()
de.auth_stub = self.auth_stub
de.props = ["Name","CustomerKey"]
de.search_filter = {'Property' : 'CustomerKey','SimpleOperator' : 'equals','Value' : self.Name}
getResponse = de.get()
if getResponse.status and len(getResponse.results) == 1 and 'CustomerKey' in getResponse.results[0]:
self.CustomerKey = getResponse.results[0]['CustomerKey']
else:
raise Exception('Unable to process DataExtension::Row request due to unable to find DataExtension based on Name')