Skip to content

Commit 00bda8c

Browse files
committed
v0.3.3: bug fix to make sure long-running processes refresh the access token correctly; also small change to better catch rate limit errors
1 parent 0289c22 commit 00bda8c

File tree

3 files changed

+106
-18
lines changed

3 files changed

+106
-18
lines changed

marketorestpython/client.py

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def execute(self, method, *args, **kargs):
7676
'get_lead_activities': self.get_lead_activities,
7777
'get_lead_activities_yield': self.get_lead_activities_yield,
7878
'get_lead_changes': self.get_lead_changes,
79+
'get_lead_changes_yield': self.get_lead_changes_yield,
7980
'add_custom_activities': self.add_custom_activities,
8081
'get_daily_usage': self.get_daily_usage,
8182
'get_last_7_days_usage': self.get_last_7_days_usage,
@@ -228,16 +229,16 @@ def execute(self, method, *args, **kargs):
228229
602 -> auth token expired
229230
'''
230231
if e.code in ['601', '602']:
231-
continue
232+
self.authenticate()
233+
continue
232234
else:
233235
raise Exception({'message':e.message, 'code':e.code})
234236
break
235237
return result
236238

237-
238239
def authenticate(self):
239-
if self.valid_until is not None and\
240-
self.valid_until > time.time():
240+
if self.valid_until is not None and \
241+
self.valid_until - time.time() >= 60:
241242
return
242243
args = {
243244
'grant_type': 'client_credentials',
@@ -286,6 +287,8 @@ def get_multiple_leads_by_filter_type(self, filterType, filterValues, fields=Non
286287
}
287288
result_list = []
288289
while True:
290+
self.authenticate()
291+
args['access_token'] = self.token # for long-running processes, this updates the access token
289292
result = HttpLib().post(self.host + "/rest/v1/leads.json", args, data, mode='nojsondumps')
290293
if result is None: raise Exception("Empty Response")
291294
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -309,6 +312,8 @@ def get_multiple_leads_by_list_id(self, listId, fields=None, batchSize=None):
309312
data.append(('batchSize',batchSize))
310313
result_list = []
311314
while True:
315+
self.authenticate()
316+
args['access_token'] = self.token # for long-running processes, this updates the access token
312317
result = HttpLib().post(self.host + "/rest/v1/list/" + str(listId)+ "/leads.json", args, data, mode='nojsondumps')
313318
if result is None: raise Exception("Empty Response")
314319
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -331,6 +336,8 @@ def get_multiple_leads_by_list_id_yield(self, listId, fields=None, batchSize=Non
331336
if batchSize is not None:
332337
data.append(('batchSize',batchSize))
333338
while True:
339+
self.authenticate()
340+
args['access_token'] = self.token # for long-running processes, this updates the access token
334341
result = HttpLib().post(self.host + "/rest/v1/list/" + str(listId)+ "/leads.json", args, data, mode='nojsondumps')
335342
if result is None: raise Exception("Empty Response")
336343
if not result['success']: raise MarketoException(result['errors'][0])
@@ -354,6 +361,8 @@ def get_multiple_leads_by_program_id(self, programId, fields=None, batchSize=Non
354361
data.append(('batchSize',batchSize))
355362
result_list = []
356363
while True:
364+
self.authenticate()
365+
args['access_token'] = self.token # for long-running processes, this updates the access token
357366
result = HttpLib().post(self.host + "/rest/v1/leads/programs/" + str(programId)+ ".json", args, data,
358367
mode='nojsondumps')
359368
if result is None: raise Exception("Empty Response")
@@ -376,6 +385,8 @@ def get_multiple_leads_by_program_id_yield(self, programId, fields=None, batchSi
376385
if batchSize is not None:
377386
data.append(('batchSize',batchSize))
378387
while True:
388+
self.authenticate()
389+
args['access_token'] = self.token # for long-running processes, this updates the access token
379390
result = HttpLib().post(self.host + "/rest/v1/leads/programs/" + str(programId)+ ".json", args, data,
380391
mode='nojsondumps')
381392
if result is None: raise Exception("Empty Response")
@@ -500,6 +511,8 @@ def get_multiple_lists(self, id=None, name=None, programName=None, workspaceName
500511
args['batchSize'] = batchSize
501512
result_list = []
502513
while True:
514+
self.authenticate()
515+
args['access_token'] = self.token # for long-running processes, this updates the access token
503516
result = HttpLib().get(self.host + "/rest/v1/lists.json", args)
504517
if result is None: raise Exception("Empty Response")
505518
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -592,6 +605,8 @@ def get_multiple_campaigns(self, id=None, name=None, programName=None, workspace
592605
args['batchSize'] = batchSize
593606
result_list = []
594607
while True:
608+
self.authenticate()
609+
args['access_token'] = self.token # for long-running processes, this updates the access token
595610
result = HttpLib().post(self.host + "/rest/v1/campaigns.json", args, data, mode='nojsondumps')
596611
if result is None: raise Exception("Empty Response")
597612
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -777,6 +792,8 @@ def get_lead_activities(self, activityTypeIds, nextPageToken=None, sinceDatetime
777792
args['nextPageToken'] = nextPageToken
778793
result_list = []
779794
while True:
795+
self.authenticate()
796+
args['access_token'] = self.token # for long-running processes, this updates the access token
780797
result = HttpLib().get(self.host + "/rest/v1/activities.json", args)
781798
if result is None: raise Exception("Empty Response")
782799
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -811,8 +828,9 @@ def get_lead_activities_yield(self, activityTypeIds, nextPageToken=None, sinceDa
811828
if nextPageToken is None:
812829
nextPageToken = self.get_paging_token(sinceDatetime=sinceDatetime)
813830
args['nextPageToken'] = nextPageToken
814-
result_list = []
815831
while True:
832+
self.authenticate()
833+
args['access_token'] = self.token # for long-running processes, this updates the access token
816834
result = HttpLib().get(self.host + "/rest/v1/activities.json", args)
817835
if result is None: raise Exception("Empty Response")
818836
if not result['success']: raise MarketoException(result['errors'][0])
@@ -829,7 +847,6 @@ def get_lead_activities_yield(self, activityTypeIds, nextPageToken=None, sinceDa
829847
break
830848
args['nextPageToken'] = result['nextPageToken']
831849

832-
833850
def get_lead_changes(self, fields, nextPageToken=None, sinceDatetime=None, batchSize=None, listId=None):
834851
self.authenticate()
835852
if fields is None: raise ValueError("Invalid argument: required argument fields is none.")
@@ -848,6 +865,8 @@ def get_lead_changes(self, fields, nextPageToken=None, sinceDatetime=None, batch
848865
args['nextPageToken'] = nextPageToken
849866
result_list = []
850867
while True:
868+
self.authenticate()
869+
args['access_token'] = self.token # for long-running processes, this updates the access token
851870
result = HttpLib().get(self.host + "/rest/v1/activities/leadchanges.json", args)
852871
if result is None: raise Exception("Empty Response")
853872
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -858,6 +877,34 @@ def get_lead_changes(self, fields, nextPageToken=None, sinceDatetime=None, batch
858877
args['nextPageToken'] = result['nextPageToken']
859878
return result_list
860879

880+
def get_lead_changes_yield(self, fields, nextPageToken=None, sinceDatetime=None, batchSize=None, listId=None):
881+
self.authenticate()
882+
if fields is None: raise ValueError("Invalid argument: required argument fields is none.")
883+
if nextPageToken is None and sinceDatetime is None: raise ValueError("Either nextPageToken or sinceDatetime needs to be specified.")
884+
fields = fields.split() if type(fields) is str else fields
885+
args = {
886+
'access_token' : self.token,
887+
'fields' : ",".join(fields),
888+
}
889+
if listId is not None:
890+
args['listId'] = listId
891+
if batchSize is not None:
892+
args['batchSize'] = batchSize
893+
if nextPageToken is None:
894+
nextPageToken = self.get_paging_token(sinceDatetime=sinceDatetime)
895+
args['nextPageToken'] = nextPageToken
896+
while True:
897+
self.authenticate()
898+
args['access_token'] = self.token # for long-running processes, this updates the access token
899+
result = HttpLib().get(self.host + "/rest/v1/activities/leadchanges.json", args)
900+
if result is None: raise Exception("Empty Response")
901+
if not result['success']: raise MarketoException(result['errors'][0])
902+
if 'result' in result:
903+
yield result['result']
904+
if result['moreResult'] is False:
905+
break
906+
args['nextPageToken'] = result['nextPageToken']
907+
861908
def add_custom_activities(self, input):
862909
self.authenticate()
863910
if input is None: raise ValueError("Invalid argument: required argument input is none.")
@@ -944,6 +991,8 @@ def get_deleted_leads(self, nextPageToken=None, sinceDatetime=None, batchSize =
944991
args['nextPageToken'] = nextPageToken
945992
result_list = []
946993
while True:
994+
self.authenticate()
995+
args['access_token'] = self.token # for long-running processes, this updates the access token
947996
result = HttpLib().get(self.host + "/rest/v1/activities/deletedleads.json", args)
948997
if result is None: raise Exception("Empty Response")
949998
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -1034,6 +1083,8 @@ def get_folder_contents(self, id, type, maxReturn=None):
10341083
result_list = []
10351084
offset = 0
10361085
while True:
1086+
self.authenticate()
1087+
args['access_token'] = self.token # for long-running processes, this updates the access token
10371088
result = HttpLib().get(self.host + "/rest/asset/v1/folder/" + str(id) + "/content.json", args)
10381089
if result is None: raise Exception("Empty Response")
10391090
if not result['success']: raise MarketoException(result['errors'][0])
@@ -1096,6 +1147,8 @@ def browse_folders(self, root, maxDepth=None, maxReturn=None, workSpace=None):
10961147
result_list = []
10971148
offset = 0
10981149
while True:
1150+
self.authenticate()
1151+
args['access_token'] = self.token # for long-running processes, this updates the access token
10991152
result = HttpLib().get(self.host + "/rest/asset/v1/folders.json", args)
11001153
if result is None: raise Exception("Empty Response")
11011154
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -1248,6 +1301,8 @@ def get_email_templates(self, maxReturn=None, status=None):
12481301
result_list = []
12491302
offset = 0
12501303
while True:
1304+
self.authenticate()
1305+
args['access_token'] = self.token # for long-running processes, this updates the access token
12511306
result = HttpLib().get(self.host + "/rest/asset/v1/emailTemplates.json", args)
12521307
if result is None: raise Exception("Empty Response")
12531308
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -1440,6 +1495,8 @@ def get_emails(self, maxReturn=None, status=None, folderId=None, folderType=None
14401495
result_list = []
14411496
offset = 0
14421497
while True:
1498+
self.authenticate()
1499+
args['access_token'] = self.token # for long-running processes, this updates the access token
14431500
result = HttpLib().get(self.host + "/rest/asset/v1/emails.json", args)
14441501
if result is None: raise Exception("Empty Response")
14451502
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -1745,6 +1802,8 @@ def get_landing_pages(self, maxReturn=None, status=None, folderId=None, folderTy
17451802
result_list = []
17461803
offset = 0
17471804
while True:
1805+
self.authenticate()
1806+
args['access_token'] = self.token # for long-running processes, this updates the access token
17481807
result = HttpLib().get(self.host + "/rest/asset/v1/landingPages.json", args)
17491808
if result is None: raise Exception("Empty Response")
17501809
#if not result['success']: raise MarketoException(result['errors'][0] + ". Request ID: " + result['requestId'])
@@ -2143,6 +2202,8 @@ def get_forms(self, maxReturn=None, status=None, folderId=None, folderType=None)
21432202
result_list = []
21442203
offset = 0
21452204
while True:
2205+
self.authenticate()
2206+
args['access_token'] = self.token # for long-running processes, this updates the access token
21462207
result = HttpLib().get(self.host + "/rest/asset/v1/forms.json", args)
21472208
if result is None: raise Exception("Empty Response")
21482209
if not result['success']: raise MarketoException(result['errors'][0])
@@ -2395,6 +2456,8 @@ def list_files(self, folder=None, maxReturn=None):
23952456
result_list = []
23962457
offset = 0
23972458
while True:
2459+
self.authenticate()
2460+
args['access_token'] = self.token # for long-running processes, this updates the access token
23982461
result = HttpLib().get(self.host + "/rest/asset/v1/files.json", args)
23992462
if result is None: raise Exception("Empty Response")
24002463
if not result['success']: raise MarketoException(result['errors'][0])
@@ -2494,6 +2557,8 @@ def get_snippets(self, maxReturn=None, status=None):
24942557
result_list = []
24952558
offset = 0
24962559
while True:
2560+
self.authenticate()
2561+
args['access_token'] = self.token # for long-running processes, this updates the access token
24972562
result = HttpLib().get(self.host + "/rest/asset/v1/snippets.json", args)
24982563
if result is None: raise Exception("Empty Response")
24992564
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -2710,6 +2775,8 @@ def get_landing_page_templates(self, maxReturn=None, status=None, folderId=None,
27102775
result_list = []
27112776
offset = 0
27122777
while True:
2778+
self.authenticate()
2779+
args['access_token'] = self.token # for long-running processes, this updates the access token
27132780
result = HttpLib().get(self.host + "/rest/asset/v1/landingPageTemplates.json", args)
27142781
if result is None: raise Exception("Empty Response")
27152782
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -2941,6 +3008,8 @@ def browse_programs(self, status=None, maxReturn=None):
29413008
result_list = []
29423009
offset = 0
29433010
while True:
3011+
self.authenticate()
3012+
args['access_token'] = self.token # for long-running processes, this updates the access token
29443013
result = HttpLib().get(self.host + "/rest/asset/v1/programs.json", args)
29453014
if result is None: raise Exception("Empty Response")
29463015
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -3007,6 +3076,8 @@ def get_channels(self, maxReturn=None):
30073076
result_list = []
30083077
offset = 0
30093078
while True:
3079+
self.authenticate()
3080+
args['access_token'] = self.token # for long-running processes, this updates the access token
30103081
result = HttpLib().get(self.host + "/rest/asset/v1/channels.json", args)
30113082
if result is None: raise Exception("Empty Response")
30123083
if not result['success']: raise MarketoException(result['errors'][0])
@@ -3045,6 +3116,8 @@ def get_tags(self, maxReturn=None):
30453116
result_list = []
30463117
offset = 0
30473118
while True:
3119+
self.authenticate()
3120+
args['access_token'] = self.token # for long-running processes, this updates the access token
30483121
result = HttpLib().get(self.host + "/rest/asset/v1/tagTypes.json", args)
30493122
if result is None: raise Exception("Empty Response")
30503123
if not result['success']: raise MarketoException(result['errors'][0])
@@ -3154,6 +3227,8 @@ def get_custom_objects(self, name, input, filterType, fields=None, batchSize=Non
31543227
data['batchSize'] = batchSize
31553228
result_list = []
31563229
while True:
3230+
self.authenticate()
3231+
args['access_token'] = self.token # for long-running processes, this updates the access token
31573232
result = HttpLib().post(self.host + "/rest/v1/customobjects/" + name + ".json", args, data)
31583233
if result is None: raise Exception("Empty Response")
31593234
if not result['success']: raise MarketoException(result['errors'][0])
@@ -3227,6 +3302,8 @@ def get_opportunities(self, filterType, filterValues, fields=None, batchSize=Non
32273302
data.append(('batchSize',batchSize))
32283303
result_list = []
32293304
while True:
3305+
self.authenticate()
3306+
args['access_token'] = self.token # for long-running processes, this updates the access token
32303307
result = HttpLib().post(self.host + "/rest/v1/opportunities.json", args, data, mode='nojsondumps')
32313308
if result is None: raise Exception("Empty Response")
32323309
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -3298,6 +3375,8 @@ def get_opportunity_roles(self, filterType, filterValues, fields=None, batchSize
32983375
data.append(('batchSize',batchSize))
32993376
result_list = []
33003377
while True:
3378+
self.authenticate()
3379+
args['access_token'] = self.token # for long-running processes, this updates the access token
33013380
result = HttpLib().post(self.host + "/rest/v1/opportunities/roles.json", args, data, mode='nojsondumps')
33023381
if result is None: raise Exception("Empty Response")
33033382
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -3374,6 +3453,8 @@ def get_companies(self, filterType, filterValues, fields=None, batchSize=None):
33743453
data.append(('batchSize',batchSize))
33753454
result_list = []
33763455
while True:
3456+
self.authenticate()
3457+
args['access_token'] = self.token # for long-running processes, this updates the access token
33773458
result = HttpLib().post(self.host + "/rest/v1/companies.json", args, data, mode='nojsondumps')
33783459
if result is None: raise Exception("Empty Response")
33793460
if not result['success'] : raise MarketoException(result['errors'][0])
@@ -3447,6 +3528,8 @@ def get_sales_persons(self, filterType, filterValues, fields=None, batchSize=Non
34473528
data.append(('batchSize',batchSize))
34483529
result_list = []
34493530
while True:
3531+
self.authenticate()
3532+
args['access_token'] = self.token # for long-running processes, this updates the access token
34503533
result = HttpLib().post(self.host + "/rest/v1/salespersons.json", args, data, mode='nojsondumps')
34513534
if result is None: raise Exception("Empty Response")
34523535
if not result['success'] : raise MarketoException(result['errors'][0])

0 commit comments

Comments
 (0)